图像分割
根据颜色算出距离矩阵
code:
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import cv2
import matplotlib.pyplot as plt
import numpy as np
# import ot
# import ot.plot
from skimage.segmentation import slic, mark_boundaries # super pixels
from skimage import color
def superpixel_segmentation(image, num_segments):
"""
Segment an image into an approximative number of superpixels.
inputs:
-image: a numpy array to be segmented
-num_segments: the number of of desired segments in the segmentation
returns:
- the number of segments after superpixel segmentation
- the content of each cluster (center coordinates, mean of color)
"""
segments = slic(image, n_segments=num_segments, sigma=5) # segmentation using the k-means clustering, 每个像素赋值为1,2,3,4,5...
clusters = np.zeros((len(np.unique(segments)), 5)) # 每个seg的 质心 和 平均颜色
superpixels = color.label2rgb(segments, image, kind='avg')#
for c in np.unique(segments):
indexes = np.where(segments == c)
mean_color = superpixels[indexes[0][0], indexes[1][
0]] # just access the first element of superpixel since all elements of superpixel are average color of cluster
mean_indexes = np.array([int(np.round(np.mean(indexes[0]))), int(np.round(np.mean(indexes[1])))])
print(c, mean_indexes, mean_color)
clusters[c-1] = np.concatenate((mean_indexes, mean_color), axis=None)
return segments, clusters, superpixels
def cluster_weights(segments):
"""
Compute the weights of each cluster.
inputs:
- segments: a 2D array which is an integer mask indicating segment labels
returns:
- a list of the weights associated to each cluster given the following formula N_pixel_cluster/N_pixel_image
"""
size_image = segments.shape[0] * segments.shape[1]
weights = np.zeros((len(np.unique(segments)),))
for c in np.unique(segments):
indexes = np.where(segments == c)
n_pixels = len(indexes[0])
weights[c] = n_pixels / size_image
return weights
def seg_im(file):
image = cv2.imread(file, 1)[..., ::-1]
num_segments = 120
segments, clusters, superpixels = superpixel_segmentation(image, num_segments)
plt.figure()
plt.subplot(221)
plt.imshow(segments)
plt.subplot(222)
plt.imshow(np.clip(superpixels,0,255).astype(np.uint8))
plt.subplot(223)
plt.imshow(mark_boundaries(image,segments))
plt.show()
return segments, clusters, superpixels
if __name__ == "__main__":
# import os
#
# os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
file = r'D:\code_color\Colorization-optimal-transport-main\examples\fleur-2.jpg'
image = cv2.imread(file, 1)[..., ::-1]
num_segments = 120
segments, clusters, superpixels = superpixel_segmentation(image, num_segments)
weights1 = cluster_weights(segments)
plt.figure()
plt.subplot(221)
plt.imshow(segments)
plt.subplot(222)
plt.imshow(np.clip(superpixels, 0, 255).astype(np.uint8))
plt.subplot(223)
plt.imshow(mark_boundaries(image, segments))
plt.show()
file2 = r'D:\code_color\Colorization-optimal-transport-main\examples\fleur-4.jpg'
image2 = cv2.imread(file2, 1)[..., ::-1]
segments2, clusters2, superpixels2 = superpixel_segmentation(image2, num_segments)
weights2 = cluster_weights(segments2)
plt.figure()
plt.subplot(221)
plt.imshow(segments2)
plt.subplot(222)
plt.imshow(np.clip(superpixels2, 0, 255).astype(np.uint8))
plt.subplot(223)
plt.imshow(mark_boundaries(image2, segments2))
plt.show()
plt.figure()
plt.plot(weights1, 'r')
plt.plot(weights2, 'b')
plt.show()
X = clusters
Y = clusters2
X_norm = X.copy()
X_norm[:, 0] = X[:, 0] / 500
X_norm[:, 1] = X[:, 1] / 500
X_norm[:, 2:] = X[:, 2:] / 255
Y_norm = Y.copy()
Y_norm[:, 0] = Y[:, 0] / 500
Y_norm[:, 1] = Y[:, 1] / 500
Y_norm[:, 2:] = Y[:, 2:] / 255
# import matplotlib.pylab as pl
# M = ot.dist(X_norm.reshape((-1, 5)), Y_norm.reshape((-1, 5)))
# pl.figure(2, figsize=(5, 5))
# ot.plot.plot1D_mat(X_norm, Y_norm, M, 'Cost matrix M')
# pl.show()
#
#
# # Equivalent to
# G0 = ot.emd(weights1, weights2, M)
#
# pl.figure(3, figsize=(5, 5))
# ot.plot.plot1D_mat(X_norm, Y_norm, G0, 'OT matrix G0')
# pl.show()