Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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_Numpy_Opencv_Image Processing_Computer Vision - Fatal编程技术网

如何在Python中使用opencv绘制轮廓?

如何在Python中使用opencv绘制轮廓?,python,numpy,opencv,image-processing,computer-vision,Python,Numpy,Opencv,Image Processing,Computer Vision,我对以下图像进行了预处理: 现在我想使用findContours方法从图像中查找单元格,然后使用drawcourts从图像中为每个单元格绘制轮廓。我这样做了,但没有显示任何内容 contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 255, 0), 3) 我收到以下错误 等高线不是numpy数组,也不是标量数组

我对以下图像进行了预处理:

现在我想使用
findContours
方法从图像中查找单元格,然后使用
drawcourts
从图像中为每个单元格绘制轮廓。我这样做了,但没有显示任何内容

contours =  cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
我收到以下错误

等高线不是numpy数组,也不是标量数组

另一个答案对我没有帮助。

我还使用以下解决方案

contours =  cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
ctr = numpy.array(contours).reshape((-1,1,2)).astype(numpy.int32)
cv2.drawContours(img, ctr, -1, (0, 255, 0), 3)

但是此解决方案没有在给定的图像上绘制任何内容。

更新,OpenCV 4.0更改了
cv2.findContours
。所以我修改了代码示例。还有一个更一般的方法:

cnts, hiers  = cv2.findContours(...)[:-2]

cv2.findContours
返回一个元组,而不仅仅是轮廓。您可以这样做:

## Find contours
if cv2.__version__.startswith("3."):
    _, contours, _ = cv2.findContours(bimg.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
    contours, _ = cv2.findContours(bimg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

## Draw contours 
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

相关的类似问题:

(一)


(2)

更新,OpenCV 4.0更改
cv2.findContours
。所以我修改了代码示例。还有一个更一般的方法:

cnts, hiers  = cv2.findContours(...)[:-2]

cv2.findContours
返回一个元组,而不仅仅是轮廓。您可以这样做:

## Find contours
if cv2.__version__.startswith("3."):
    _, contours, _ = cv2.findContours(bimg.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
    contours, _ = cv2.findContours(bimg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

## Draw contours 
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

相关的类似问题:

(一)


(2)

cv2返回的元组。findContours的形式为
(im,轮廓,层次)
,其中
im
是轮廓可视化的图像,
contours
是两个numpy数组的元组,其形式为
(x\u值,y\u值)
,并且
继承权
取决于检索模式。由于您使用的是CV_RETR_EXTERNAL,
heirarchy
没有太大意义。

cv2返回的元组。findContours
的形式为
(im,轮廓,层次)
其中
im
是轮廓可视化的图像,
contours
是两个numpy数组的元组,格式为
(x_值,y_值)
继承权
取决于检索模式。由于您使用的是CV_RETR_EXTERNAL,
继承权
没有太大意义。

谢谢!我似乎使用了版本>3。@MihaiAlexandru Ionut注意,OpenCV 4.0将
cv2.findContours
改回,所以我修改了代码示例。谢谢!看起来是这样的我使用了版本>3。@MihaiAlexandru Ionut注意,OpenCV 4.0将
cv2.findConteurs
更改回原来的版本,因此我修改了代码示例。