【发布时间】:2018-03-05 16:43:38
【问题描述】:
我正在尝试使用 scikit 均值偏移算法执行图像分割。我使用 opencv 来显示分割后的图像。 我的问题如下:我使用不同示例中给出的代码,当我显示分割后的图像时,我得到一个黑色图像。我想知道是否有人能看到我的错误是什么...... 非常感谢您的帮助!
这是我的代码:
import numpy as np
import cv2
from sklearn.cluster import MeanShift, estimate_bandwidth
#Loading original image
originImg = cv2.imread('Swimming_Pool.jpg')
# Shape of original image
originShape = originImg.shape
# Converting image into array of dimension [nb of pixels in originImage, 3]
# based on r g b intensities
flatImg=np.reshape(originImg, [-1, 3])
# Estimate bandwidth for meanshift algorithm
bandwidth = estimate_bandwidth(flatImg, quantile=0.1, n_samples=100)
ms = MeanShift(bandwidth = bandwidth, bin_seeding=True)
# Performing meanshift on flatImg
ms.fit(flatImg)
# (r,g,b) vectors corresponding to the different clusters after meanshift
labels=ms.labels_
# Remaining colors after meanshift
cluster_centers = ms.cluster_centers_
# Finding and diplaying the number of clusters
labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)
print("number of estimated clusters : %d" % n_clusters_)
# Displaying segmented image
segmentedImg = np.reshape(labels, originShape[:2])
cv2.imshow('Image',segmentedImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
【问题讨论】:
-
对不起格式代码错误,不知道怎么写正确。
-
编辑器中有一个按钮可以格式化为代码
标签: python image-processing scikit-learn scikit-image mean-shift