Python 删除此图像中的白色部分

Python 删除此图像中的白色部分,python,opencv,image-processing,image-morphology,Python,Opencv,Image Processing,Image Morphology,我有这个图像,是两个图像相减的结果。 我只想得到图像中标记的白色块。 在我以前的工作中,我尝试了一些形态学操作 不幸的是,我的代码并没有给我想要的 #new_diff result of subtraction new_diff= np.array(new_diff, dtype= np.uint8) kernel = np.ones((7,7), np.uint8) erosion = cv2.erode(new_diff.copy(),kernel,iterations= 1) media

我有这个图像,是两个图像相减的结果。

我只想得到图像中标记的白色块。 在我以前的工作中,我尝试了一些形态学操作

不幸的是,我的代码并没有给我想要的

#new_diff result of subtraction
new_diff= np.array(new_diff, dtype= np.uint8)
kernel = np.ones((7,7), np.uint8)
erosion = cv2.erode(new_diff.copy(),kernel,iterations= 1)
median = cv2.medianBlur(erosion,3)
closing = cv2.morphologyEx(median, cv2.MORPH_CLOSE, np.ones((9,9),np.uint8),iterations=1)

cv2.imshow('closing', closing)

cnts =cv2.findContours(closing.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[0]

print 'no of cont',len(cnts)
areaArray = []
for i, c in enumerate(cnts):
    area = cv2.contourArea(c)
    areaArray.append(area)
#first sort the array by area
sorteddata = sorted(zip(areaArray, cnts), key=lambda x: x[0], reverse=True)
#find the nth largest contour [n-1][1]
c = sorteddata[0][1]

#print 'max contour=', c
cv2.drawContours(closing,[c],0,(255,255,255),3)
cv2.imshow('contours', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()
关闭后的结果如下所示。

但当我画出最大的轮廓时,我最终得到了这个。


在一些图像中,我想要的部分与图像中标记的部分相似,但可能只有一半。你知道如何确保这个块是最大的轮廓,这样我就可以把它剪下来吗?提前谢谢

使用中值或形态学运算来去除噪声。距离变换也是一种选择。您应该添加一个阈值操作,以消除对象周围的低强度。在差分计算之前进行一些预处理也可能有所帮助。拉屎入=拉屎出。@AlexanderReynolds我正在使用openCV 2.4,所以它是第一个返回值。@Pieget我会尝试一下,看看我得到了什么!非常感谢。