需要帮助使用开放的CV Python从图像中删除轮廓

发布于 2025-02-12 11:09:16 字数 2288 浏览 2 评论 0原文

我正在尝试删除此矩形轮廓来自此照片

我试图使用此教程 https://pyimagesearch.com/2015/02/09/removing-contours-image-image-using-python-opencv/ ,但这并不产生所需的结果,我对什么并不清楚什么是什么语法确实如此。

thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 61, 10)
cntrs, hiearchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(cntrs, key=cv2.contourArea)
largest_cntr = [largest_areas[-1]] #rectangular contour or largest contour
    
mask = np.ones(thresh.shape[:2], dtype="uint8") * 255    
cv2.drawContours(mask, largest_cntr, -1, 0, -1)
thresh = cv2.bitwise_and(thresh, thresh, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", thresh)

最后,我得到了一个黑色的图像。如何正确从图像中删除轮廓?

完整代码:

import cv2,os, glob
from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np


folder_dir = os.getcwd()
print(folder_dir)
img = cv2.imread(folder_dir + "/imgur.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 10)
cntrs, hiearchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(cntrs, key=cv2.contourArea)
largest_cntr = [largest_areas[-1]] #rectangular contour or largest contour
    
mask = np.ones(thresh.shape[:2], dtype="uint8") * 255    
cv2.drawContours(mask, largest_cntr, -1, 0, -1)
thresh = cv2.bitwise_and(thresh, thresh, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

I am trying to remove this rectangular contour from the binary image of this photo.

I have tried to use this tutorial https://pyimagesearch.com/2015/02/09/removing-contours-image-using-python-opencv/ but it is not yielding the desired results and I am not very clear of what the syntax does.

thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 61, 10)
cntrs, hiearchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(cntrs, key=cv2.contourArea)
largest_cntr = [largest_areas[-1]] #rectangular contour or largest contour
    
mask = np.ones(thresh.shape[:2], dtype="uint8") * 255    
cv2.drawContours(mask, largest_cntr, -1, 0, -1)
thresh = cv2.bitwise_and(thresh, thresh, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", thresh)

In the end I get a black-filled image. How can I remove the contour from the image properly?
enter image description here
enter image description here

Full code:

import cv2,os, glob
from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np


folder_dir = os.getcwd()
print(folder_dir)
img = cv2.imread(folder_dir + "/imgur.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 10)
cntrs, hiearchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largest_areas = sorted(cntrs, key=cv2.contourArea)
largest_cntr = [largest_areas[-1]] #rectangular contour or largest contour
    
mask = np.ones(thresh.shape[:2], dtype="uint8") * 255    
cv2.drawContours(mask, largest_cntr, -1, 0, -1)
thresh = cv2.bitwise_and(thresh, thresh, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文