有什么方法可以用白色填充图像的内部吗?

时间:2019-06-09 04:40:09

标签: python opencv image-processing image-segmentation image-thresholding

例如,在MATLAB Image Processing Toolbox中,我们具有imfill函数。如何在Python中执行此操作?

因此,我试图将各种形状分割开并去除内部黑色。我知道如果得到边界,分割仍然很容易,但是我也想完美地检测形状。简而言之,我想删除图像中黑色覆盖的区域,如图所示。

我尝试使用按位运算,形态转换和泛洪。我是泛洪的新手,并不完全了解它的工作原理。

image=cv2.imread('rec2.jpg')
kernel = np.ones((5,5),np.uint8)
w=int(image.shape[1]*0.3)
h=int(image.shape[0]*0.5)
resized = cv2.resize(image,(w, h), interpolation = cv2.INTER_CUBIC)
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY_INV)[1]
thresh2 = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(len(cnts[1])) #To verify number of counts
cv2.imshow("Result",np.hstack((thresh,thresh2)))

enter image description here

这是到目前为止的分割图像。但是我也希望内部黑色区域也用白色填充,以便可以将其识别为实心形状。

1 个答案:

答案 0 :(得分:0)

您可以通过定义thickness=cv2.FILLED来绘制填充轮廓:

cv2.drawContours(thresh, cnts, -1, (255, 255, 255), thickness=cv2.FILLED)
相关问题