opencv 我需要更改颜色空间来查找轮廓,但无法解决问题

col17t5w  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(181)

我知道我得到的错误,我需要改变变量的颜色空间为灰色,那么我就不会得到错误,但我不能解决这个问题,所以我不能进入我的项目的下一个阶段。

error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\imgproc\src\contours.cpp:197: 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'
import cv2
import numpy as np

img = cv2.imread("s.jpg")

while True:
    # ret, frame = cap.read()
    # frame = cv2.flip(frame,1)
    
    # print(frame.shape)    # 480,640
    
    ycrbc = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
    
    minYCrCb = np.array([0,140,90],np.uint8)
    maxYCrCb = np.array([230,170,120],np.uint8)
    imgeYCrCb = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
    skinRegionYCrCb = cv2.inRange(imgeYCrCb,minYCrCb,maxYCrCb)
    skinYCrCb = cv2.bitwise_and(img, img, mask = skinRegionYCrCb)
    median_ycrcb = cv2.medianBlur(skinYCrCb, 3)
    
    _, esik = cv2.threshold(median_ycrcb, 20, 255, cv2.THRESH_BINARY)
    median_binary = cv2.medianBlur(esik, 7)
    
    gray = cv2.cvtColor(median_binary, cv2.COLOR_BGR2GRAY)
    
    contours, hierarchy = cv2.findContours(median_binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    areas = [cv2.contourArea(c) for c in contours]
    max_index = np.argmax(areas)
    x,y,w,h = cv2.boundingRect(contours[max_index])

    print(x, y, x+w, y+h)
    
    cv2.imshow("ycrbc",ycrbc)
    cv2.imshow("skinYCrCb",median_ycrcb)
    cv2.imshow("binary goruntu", esik)
    cv2.imshow("median_binary", median_binary)
    
    if cv2.waitKey(5) & 0xFF == ord("q"):
        break
    
# cap.release()
cv2.destroyAllWindows()

我拍了这张照片:

如果你能通过改变这里的颜色空间来解决我的问题,我将非常感激,我搜索了很多,但我找不到它,或者我错过了一些东西,如果你能帮助我,这将是伟大的。

vlju58qv

vlju58qv1#

使用以下内容:

contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

您必须在findContours函数中使用单通道输入。将您的输入更改为“灰色”,即单通道,应该可以修复此问题。

相关问题