的两个部分我有那种图像独立的图像作为在python
如图所示的图像,图像主要有两个部分。 上下。
我想分开它们。
在第一次尝试中,我实现了使用K-meas算法。
In[2]: kmeans = KMeans(n_clusters=2, random_state=0).fit(... np.asarray(np.where(finalImage == 255)).T)
正是在此搜索案例工程是上下有圆点的几乎相同。但是,是不是工作在影像2
后比我用cv2.findContours以获得最大的区域2个轮廓
im, contours, hierarchy = cv2.findContours(Img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# get first contour
maxContour = 0
for contour in contours:
contourSize = cv2.contourArea(contour)
if contourSize > maxContour:
maxContour = contourSize
maxContourData0 = contour
# get second contour
if contours.__len__() != 1:
maxContour = 0
for contour in contours:
contourSize = cv2.contourArea(contour)
if contourSize > maxContour and np.sum(np.subtract(contour[0:5],maxContourData0[0:5])) != 0:
maxContour = contourSize
maxContourData1 = contour
mask = np.zeros_like(Img)
cv2.fillPoly(mask, [maxContourData0], 1)
if contours.__len__() != 1:
cv2.fillPoly(mask, [maxContourData1], 1)
finalImage = np.zeros_like(Img)
finalImage[:, :] = np.multiply(Img, mask)
cv2.imshow('final', finalImage)
cv2.waitKey(0)
但有时去一个轮廓不是2 ...
强制性地有没有得到两个blob ?
如果斑点的实际尺寸并不重要,但只有相对大小,你可以尝试在提取斑点之前应用腐蚀,它可能有助于消除两个斑点之间的连接。 – Mathias