【问题标题】:Is there any way to fill the interior of an image with white?有没有办法用白色填充图像的内部?
【发布时间】:2019-10-24 00:07:49
【问题描述】:

例如,在 MATLAB 图像处理工具箱中,我们有 imfill 函数。如何在 Python 中做到这一点?

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

我尝试过使用按位运算、形态变换和填充。我是 Floodfill 新手,不知道它是如何工作的。

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)))

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

【问题讨论】:

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


【解决方案1】:

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

cv2.drawContours(thresh, cnts, -1, (255, 255, 255), thickness=cv2.FILLED)

【讨论】:

  • @ShivamSahil 如果答案有效,请通过单击计票旁边的空心对勾(复选标记)将其标记为 “已接受”,以便 a) 回答者收到到期识别,b)其他潜在的回答者知道有一个解决方案,c)其他有相同问题的人可以立即看到哪个答案有效。谢谢。
  • 感谢@MarkSetchell 的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 2018-09-11
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多