我正在使用python openCV库来获取简单图像中的轮廓坐标 . 据我所知,轮廓中点的顺序将是findContour()方法返回的顺序 .
但是findContour()返回的点集在其中有重复 . 如果订单被保留,那么怎么会有重复?
我如何阅读输出?
这是代码
import numpy as np
import cv2
from pylab import plot,show
from PIL import Image
def get_contours(im):
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
return im2, contours, hierarchy
def main():
im = cv2.imread('border.jpeg')
contour_image, contours, hierarchy = get_contours(im)
contour_points = []
for pt in contours[1]:
contour_points.append((pt[0][0],pt[0][1]))
plot(pt[0][0],pt[0][1])
if len(contour_points)!= len(set(contour_points)):
print "has_duplicates"
else:
print "no duplicates"
main()
1 回答
您找到轮廓的对象包含约1个像素宽的“瓶颈”,因此相对侧的边共享一个顶点 .
最好用图片来说明这一点 .
想象一下4x5像素图像中的这种情况:
让我们运行一个简短的脚本来找到这个轮廓:
如果我们把它画进来,那就很明显了: