针对下图的船进行轮廓提取,
img = cv2.imread('./img/yuantu.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
ret, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('lunkuo', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行结果就是,提取了图像中全部的轮廓。
应改用模板匹配算法