【发布时间】:2018-07-12 13:13:18
【问题描述】:
我在 python 中进行图像处理,并使用 skimage rag_mean_color 和分割方法首先分割图像。然后我使用 graph.cut_threshold 来合并具有相似颜色的相邻区域。最后,我想提取一个非常大的单一颜色区域。我一直在使用另一种方法来尝试获得图片中最常见的颜色(kmeans),但没有得到我想要的结果。
我想知道是否有办法直接从图表中获取区域的颜色。谢谢!
RAG 代码:
img = cv2.imread(path)
# convert the image to HSV
img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# apply the RAG thresholding on the image
labels1 = segmentation.slic(img, compactness=10, n_segments=600)
out1 = color.label2rgb(labels1, img, kind='avg')
# create the region adjacency graph from the labels and image
g = graph.rag_mean_color(img, labels1)
# merge similar regions in the image/graph
labels2 = graph.cut_threshold(labels1, g, 20)
out2 = color.label2rgb(labels2, img, kind='avg')
# construct an updated graph for the image from the merged labels
g2 = graph.rag_mean_color(out2, labels2)
# merge similar regions a second time to ensure like regions are merged
labels3 = graph.cut_threshold(labels2, g2, 30)
out3 = color.label2rgb(labels3, out2, kind='avg')
主要颜色代码:
# get the dominant (most common) color in an image using kmeans
# convert the image into HSV color space
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# reshape the photo to be rows of colors
data = img.copy().reshape((img.shape[0]*img.shape[1], 3)).astype(float)
# apply kmeans to the data
centroids, codes = kmeans2(data, 5)
# get the most common centroid
centroid = np.argmax(np.bincount(codes))
d_color = centroids[centroid].astype(np.uint8)
【问题讨论】:
标签: python image image-processing scikit-image