Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python opencv:在点之间绘制线并查找完整轮廓_Python_Python 3.x_Numpy_Opencv - Fatal编程技术网

Python opencv:在点之间绘制线并查找完整轮廓

Python opencv:在点之间绘制线并查找完整轮廓,python,python-3.x,numpy,opencv,Python,Python 3.x,Numpy,Opencv,我有一组三角形的三个点。例如:[[39037],[371179],[555179]] 画完线, 如何在我刚才画的线条之间找到完整的轮廓 我一直收到以下错误: img = np.zeros([1000, 1000, 3], np.uint8) cv2.drawContours(img, triangle_vertices, 0, (0, 0, 0), -1) contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTER

我有一组三角形的三个点。例如:[[39037],[371179],[555179]]

画完线,

如何在我刚才画的线条之间找到完整的轮廓

我一直收到以下错误:

    img = np.zeros([1000, 1000, 3], np.uint8)
    cv2.drawContours(img, triangle_vertices, 0, (0, 0, 0), -1)
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
错误:(-210:不支持的格式或格式组合)[Start]FindContours在模式下仅支持CV_8UC1图像!=CV_RETR_FLOODFILL仅在函数“cvStartFindContours_Impl”中支持CV_32SC1图像


我相信你的主要问题是你只能在二值图像上找到轮廓(不是颜色)。您的点还需要位于Numpy数组中(如x,y对——请注意逗号)。下面是我如何在Python/OpenCV中实现的

import cv2
import numpy as np

# create black background image
img = np.zeros((500, 1000), dtype=np.uint8)

# define triangle points
points = np.array( [[390,37], [371,179], [555,179]], dtype=np.int32 )

# draw triangle polygon on copy of input
triangle = img.copy()
cv2.polylines(triangle, [points], True, 255, 1)

# find the external contours
cntrs = cv2.findContours(triangle, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# get the single contour of the triangle
cntr = cntrs[0]

# draw filled contour on copy of input
triangle_filled = img.copy()
cv2.drawContours(triangle_filled, [cntr], 0, 255, -1)

# save results
cv2.imwrite('triangle.jpg', triangle)
cv2.imwrite('triangle_filled.jpg', triangle_filled)

cv2.imshow('triangle', triangle)
cv2.imshow('triangle_filled', triangle_filled)
cv2.waitKey()

三角形多边形图像:

三角形填充轮廓图像:


我认为您的主要问题是,您只能在二值图像上找到轮廓(而不是颜色)。您的点还需要位于Numpy数组中(如x,y对——请注意逗号)。下面是我如何在Python/OpenCV中实现的

import cv2
import numpy as np

# create black background image
img = np.zeros((500, 1000), dtype=np.uint8)

# define triangle points
points = np.array( [[390,37], [371,179], [555,179]], dtype=np.int32 )

# draw triangle polygon on copy of input
triangle = img.copy()
cv2.polylines(triangle, [points], True, 255, 1)

# find the external contours
cntrs = cv2.findContours(triangle, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# get the single contour of the triangle
cntr = cntrs[0]

# draw filled contour on copy of input
triangle_filled = img.copy()
cv2.drawContours(triangle_filled, [cntr], 0, 255, -1)

# save results
cv2.imwrite('triangle.jpg', triangle)
cv2.imwrite('triangle_filled.jpg', triangle_filled)

cv2.imshow('triangle', triangle)
cv2.imshow('triangle_filled', triangle_filled)
cv2.waitKey()

三角形多边形图像:

三角形填充轮廓图像:


谢谢,顺便说一句,这条线在干什么?cntrs=cntrs[0]如果len(cntrs)==2或者cntrs[1],为什么会有第二个轮廓,如果它总是[0]?这条线是用来确定从findContours返回多少东西,并且只得到轮廓,而不是层次结构。返回的findContours的不同版本不是2项(层次结构和轮廓)。此处的[0]指的是哪个退货项目,而不是第一个轮廓。CNTR指所有退货项目,包括层次结构和轮廓。3.4.2版本返回3项:图像、层次和轮廓,而不是像大多数版本那样仅返回层次和轮廓。请参见好的,最后一个问题,绘制等高线和多段线之间的区别是什么?他们似乎在做同样的事情,但在视觉上并没有多少作用。但根据直线度和轮廓的绘制方式,轮廓可以有许多顶点(线中的像素数)。等高线也有一个与之关联的层次,而多边形线则没有。此外,如果有图像,则可以查找轮廓,但无法直接查找多段线的顶点。但是你可以从轮廓中获取顶点,并将它们减少到一些简单形状的顶点。谢谢,顺便问一下,这条线在做什么?cntrs=cntrs[0]如果len(cntrs)==2或者cntrs[1],为什么会有第二个轮廓,如果它总是[0]?这条线是用来确定从findContours返回多少东西,并且只得到轮廓,而不是层次结构。返回的findContours的不同版本不是2项(层次结构和轮廓)。此处的[0]指的是哪个退货项目,而不是第一个轮廓。CNTR指所有退货项目,包括层次结构和轮廓。3.4.2版本返回3项:图像、层次和轮廓,而不是像大多数版本那样仅返回层次和轮廓。请参见好的,最后一个问题,绘制等高线和多段线之间的区别是什么?他们似乎在做同样的事情,但在视觉上并没有多少作用。但根据直线度和轮廓的绘制方式,轮廓可以有许多顶点(线中的像素数)。等高线也有一个与之关联的层次,而多边形线则没有。此外,如果有图像,则可以查找轮廓,但无法直接查找多段线的顶点。但是,您可以从轮廓中获取顶点,并经常将它们减少为几个简单形状的顶点。