优化代码import os image_files=os.listdir('./data/imgs') images=[] gts=[] masks=[] def normalize_image(img): return (img - np.min(img)) / (np.max(img) - np.min(img)) for i in image_files: images.append(os.path.join('./data/imgs',i)) gts.append(os.path.join('./data/gt',i)) for i in range(len(images)): ### YOUR CODE HERE # 10 points img = io.imread(images[i]) #kmeans km_mask = kmeans_color(img, 2) #mean shift ms_mask=(segmIm(img, 20) > 0.5).astype(int) # ms_mask = np.mean(io.imread(gts[i]), axis=2) #gt # gt_mask = np.array(io.imread(gts[i]))[:,:,:3] gt_mask = np.mean(io.imread(gts[i]), axis=2) ### END YOUR CODE #kmeans masks.append([normalize_image(x) for x in [km_mask,ms_mask,gt_mask]]) #output three masks
时间: 2024-03-08 13:50:49 浏览: 61
Here are some suggestions to optimize the code:
1. Instead of using `os.listdir` to get a list of files in a directory and then appending the directory path to each file name, you can use `glob.glob` to directly get a list of file paths that match a certain pattern. For example:
```
import glob
image_files = glob.glob('./data/imgs/*.jpg')
```
2. Instead of appending each image path and ground truth path to separate lists, you can use a list comprehension to create a list of tuples that contain both paths:
```
data = [(os.path.join('./data/imgs', i), os.path.join('./data/gt', i)) for i in image_files]
```
3. Instead of appending three normalized masks to the `masks` list, you can use a list comprehension to create a list of tuples that contain the three masks:
```
masks = [(normalize_image(km_mask), normalize_image(ms_mask), normalize_image(gt_mask)) for km_mask, ms_mask, gt_mask in zip(kmeans_masks, ms_masks, gt_masks)]
```
4. You can use `skimage.color.rgb2gray` to convert an RGB image to grayscale instead of computing the mean across color channels:
```
gt_mask = skimage.color.rgb2gray(io.imread(gt_path))
```
5. You can use `skimage.io.imread_collection` to read a collection of images instead of using a loop:
```
images = skimage.io.imread_collection(image_files)
gts = skimage.io.imread_collection(gt_files)
```
Here's the optimized code:
```
import os
import glob
import numpy as np
import skimage.io
import skimage.color
from sklearn.cluster import KMeans
from skimage.segmentation import mean_shift
def normalize_image(img):
return (img - np.min(img)) / (np.max(img) - np.min(img))
image_files = glob.glob('./data/imgs/*.jpg')
data = [(os.path.join('./data/imgs', i), os.path.join('./data/gt', i)) for i in image_files]
masks = []
for img_path, gt_path in data:
# read images
img = skimage.io.imread(img_path)
gt = skimage.io.imread(gt_path)
# k-means segmentation
kmeans = KMeans(n_clusters=2)
kmeans_mask = kmeans.fit_predict(img.reshape(-1, 3)).reshape(img.shape[:2])
# mean shift segmentation
ms_mask = (mean_shift(img, 20) > 0.5).astype(int)
# ground truth mask
gt_mask = skimage.color.rgb2gray(gt)
# normalize masks
km_mask_norm = normalize_image(kmeans_mask)
ms_mask_norm = normalize_image(ms_mask)
gt_mask_norm = normalize_image(gt_mask)
# append masks to list
masks.append((km_mask_norm, ms_mask_norm, gt_mask_norm))
```
阅读全文