[英]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 模式!= CV_RETR_FLOODFILL 否則僅在 function '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()
我正在嘗試計算圖像中的線條
我剛剛在 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 圖像,這是結果。 在此處輸入圖像描述
如您所見,這些國家是用紅色繪制的。
我有 opencv 4.2 版本,也許你的版本有點舊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.