【发布时间】:2021-05-30 19:29:53
【问题描述】:
考虑这张图片:
我只想提取代表图像中最大轮廓的数字,但 opencv 总是显示原始图像和小于数字的小轮廓。所以当我运行这个函数时
def contouTreat(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="left-to-right")
cv2.drawContours(image, cnts, -1, (0, 255, 0), 3)
#cv2_imshow(image)
ROI_number = 0
arr=[]
v=True
for c in cnts:
area = cv2.contourArea(c)
if area != image.shape[1]*image.shape[0]:
x,y,w,h = cv2.boundingRect(c)
#if minc != x:
x,y,w,h = cv2.boundingRect(c)
#if area < 800 and area > 200:
#if area < 1620 and h>58 and w <50:
#if h>(70*image.shape[1])/100 and w>(60*image.shape[0])/100 :
if v:
ROI = image[y:y+h, x:x+w]
print(h)
print(w)
cv2_imshow(ROI)
return None
image=cv2.imread("/content/téléchargement (2).png")
contouTreat(image)
我得到了这个结果:
【问题讨论】:
标签: python python-3.x opencv computer-vision opencv-contour