多边形不填充"/>
cv2 画多边形不填充
我正在学习使用cv2.approxPolyDP函数将OpenCV轮廓分割成更简单、更相关的曲线。我想给自己说明一下,以便更好地了解发生了什么。我越来越接近了,cv2.approxPolyDP函数(实现RDP算法)似乎正在工作,但是当绘制为轮廓时,输出的似乎是一系列的点,而不是我期望的曲线。在
随时提供任何有帮助的东西。
这是测试-图案.png我正在使用的文件:
import numpy as np
import cv2, cv
#read the test image - this one happens to be binary
img = cv2.imread("test-pattern.png",0)
#invert the image
img2 = cv2.bitwise_not(img)
cv2.imshow("after bitwise not",img2)
#find the contours of the image
contour,hier = cv2.findContours(img,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
#make a color image of the same size for displaying contours later on .... img3 and img4
height, width = img.shape # binary image is height, width; otherwise would be height, width, depth
img3 = np.zeros((height,width,3), np.uint8)
img4 = np.zeros((height,width,3), np.uint8)
#draw contours on the color image
cv2.drawContours(img3,contour[0],-1,(0,0,200),1)
cv2.imshow("contours",img3)
#print a bunch of stuff to the command line for diagnostic purposes
print "contour vector length: ", len(contour), "\n hier length: ", len(hier)
print "contour area: ", cv2.contourArea(contour[0])
print "canvas area: ", height*width
print "bounding rect: ", cv2.boundingRect(contour[0])
#get the RDP curve vector, and try to display it as a contour on img4
#I chose an epsilon value of 1.1 ... it can be tweaked later
#It will be best to draw these curves in a rainbow colorspace so I can see them
print "RDP curve:", cv2.approxPolyDP(contour[0], 1.1, 1)
print "RDP curve length: ", len(cv2.approxPolyDP(contour[0], 1.1, 1))
cv2.drawContours(img4,cv2.approxPolyDP(contour[0], 1.1, 1),-1,(0,90,200),1)
cv2.imshow("contours",img4)
cv2.waitKey(0)
这是最后一段代码生成的输出片段。我把它剪了,这样你就可以看到圆点图案了。我不习惯通过OpenCV处理曲线,但是为了理解,我想在某种彩虹色空间中显示由RDP算法(cv2.approxPolyDP)生成的曲线。这样我就可以看到所有的曲线被连接起来形成轮廓。稍后我将需要对这些曲线执行一些操作,因此可视化它们可能会非常有用。在
另一个有趣的事情,我最初在绘制轮廓时注意到,如果我使用cv2.drawContours传递所有的轮廓cv2.drawContours(img3,contour,-1,(0,0,200),1)我得到了形状的实体跟踪,以及框架的外部边界:
另一方面,如果我选择表示形状本身的轮廓(而不是框架的外部边界)cv2.drawContours(img3,contour[0],-1,(0,0,200),1),我得到更多的点跟踪作为输出:
我想我对drawContours函数在这里做什么没有完全的理解。在
更多推荐
cv2 画多边形不填充
发布评论