简体   繁体   English

如何在 opencv 中绘制轮廓?

[英]How can I contour an image in opencv?

cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/contours.cpp:195: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl' cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/contours.cpp:195: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when 模式!= CV_RETR_FLOODFILL 否则仅在 function 'cvStartFindContours_Impl' 中支持 CV_32SC1 图像

here's my code.这是我的代码。 whats wrong?怎么了?

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() 

i'm trying to countour the lines at the image我正在尝试计算图像中的线条

I just tried your code in python3 and everything works normal, your code works.我刚刚在 python3 中尝试了您的代码,一切正常,您的代码有效。

here is the coded I tested, just remove some waitkeys.这是我测试的代码,只需删除一些等待键。

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() 

ALso I changed the image for a jpg image, here are the results.另外我将图像更改为 jpg 图像,这是结果。 enter image description here在此处输入图像描述

As you can see the countours are drawed in red color.如您所见,这些国家是用红色绘制的。

I have opencv 4.2 version, maybe yours are a bit older.我有 opencv 4.2 版本,也许你的版本有点旧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM