需要帮助使用开放的CV Python从图像中删除轮廓
我试图使用此教程 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?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论