我试图在源图像中绘制十个最大轮廓,但它们不会出现。
import cv2
image_orig=cv2.imread('C:\Users\pc\Desktop\middleeast.jpg')
image_gray=cv2.cvtColor(image_orig,cv2.COLOR_BGR2GRAY)
image_contours=image_orig.copy()
_, image_threshold=cv2.threshold(image_gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_,contours,_=cv2.findContours(image_threshold,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
largest_contours = sorted(contours, key=cv2.contourArea)[-10:]
print len(largest_contours)
for contour in largest_contours:
print cv2.contourArea(contour)
cv2.drawContours(image_contours, largest_contours, -1, (255,255,0), 3)
cv2.imshow('contours',image_contours)
cv2.waitKey(0)
代码工作得很好。因为它的形象很大,你无法看到。在imshow
之前添加此行,您将看到轮廓。
image_contours = cv2.resize(image_contours, None, fx=0.25,fy=0.25 , interpolation = cv2.INTER_CUBIC)