cv2 画多边形不填充_如何在OpenCV中绘制一组闭合的多边形曲线,将每个线段表示为不同的颜色(即在彩虹颜色空间中)?...

在学习OpenCV时,作者尝试使用cv2.approxPolyDP函数分解轮廓并以彩虹颜色展示,但遇到了绘制结果为点而非连续曲线的问题。文章探讨了轮廓提取、位运算、RDP算法以及cv2.drawContours函数的行为,并寻求解决方案。
摘要由CSDN通过智能技术生成

我正在学习使用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函数在这里做什么没有完全的理解。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值