python 如何在opencv中绘制图像轮廓?

k10s72fa  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(142)

错误代码:OpenCV(4.6.0)/io/opencv/模块/图像处理/源代码/轮廓。错误:(-210:不支持的格式或格式组合)[开始]FindContours在模式!= CV_RETR_FLOODFILL时仅支持CV_8UC1图像,否则仅在函数“cvStartFindContours_Impl”中支持CV_32SC1图像
这是我密码,怎么了?

import cv2 
import numpy as np 
image = cv2.imread('j.jpg') 
cv2.waitKey(0) 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
edged = cv2.Canny(gray, 30, 200) 
cv2.waitKey(0) 
  
contours, hierarchy = cv2.findContours(edged,  
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
  
cv2.imshow('Canny Edges After Contouring', edged) 
cv2.waitKey(0) 
  
print("Number of Contours found = " + str(len(contours))) 
cv2.drawContours(image, contours, -1, (0, 255, 0), 3) 
  
cv2.imshow('Contours', image) 
cv2.waitKey(0) 
cv2.destroyAllWindows()

我在试着描绘图像上的线条

y4ekin9u

y4ekin9u1#

我刚刚在python3中尝试了你的代码,一切正常,你的代码工作正常。
这是我测试的代码,只是删除了一些等待键。

import cv2 
            import numpy as np 
            image = cv2.imread('1.png') 

            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
            edged = cv2.Canny(gray, 30, 200) 

              
            contours, hierarchy = cv2.findContours(edged,  
                cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
              
            cv2.imshow('Canny Edges After Contouring', edged) 

              
            print("Number of Contours found = " + str(len(contours))) 
            cv2.drawContours(image, contours, -1, (0, 0, 255), 3) 
              
            cv2.imshow('Contours', image) 
            cv2.waitKey(0) 
            cv2.destroyAllWindows()

我也改变了一个jpg图像的图像,这里是结果. enter image description here
如您所见,轮廓是用红色绘制的。
我有opencv 4.2版本,可能你的版本有点老。

相关问题