OpenCV python bitwise_and() 皮肤分割错误

OpenCV python bitwise_and() error skin segmentation

大家好,我正在使用 OpenCv 在 Python 上开发皮肤分割程序,这是代码:


import cv2 as cv
import numpy as np



cap = cv.VideoCapture(0)

while(1):
    _, frame = cap.read()
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    #Establece los intervalos de el color de piel
    low_skin = np.array([0,5,0])
    up_skin = np.array([65,165,165])

    #Guarda esa matriz en mask
    mask = cv.inRange(hsv, low_skin, up_skin)
    #cv.imshow('Segmentacion',mask)

    #Lleva los pixeles de 255 a 1 para multiplicarlo despues
    _,mask = cv.threshold(mask,127,255,cv.THRESH_BINARY)

    #Erosion del frame
    element_E = cv.getStructuringElement(cv.MORPH_ELLIPSE, (2*5 + 1, 2*5+1), (5, 5))
    Ero = cv.erode(mask, element_E)

    #Dilatacion de los elemntos previa erosion
    element_D = cv.getStructuringElement(cv.MORPH_ELLIPSE, (2*11 + 1, 2*11 + 1), (11, 11))
    dil = cv.dilate(Ero, element_D)

    #Multiplica ambas matrices
    res = cv.bitwise_and(frame,frame,mask=mask)

    cv.imshow('Actual',res)
    cv.imshow('Salida',frame)
    print('Mask')
    print( mask.shape )
    print( mask.dtype )
    print('Dil')
    print( dil.shape )
    print( dil.dtype )


    k = cv.waitKey(30)
    if k == ord('q') or k == 27:
        break

cv.destroyAllWindows()
cap.release()


问题是,当我尝试将 bitwise_and 函数与掩码(我从 InRange 中获取)函数一起使用时,它起作用了,但我需要腐蚀和扩张该掩码,所以当一切都完成后,我尝试使用 "dil" 应用 bitwise_and 函数 res = cv.bitwise_and(frame,frame,dil=dil) 并显示下一个错误:

  File "<stdin>", line 32, in <module>
TypeError: 'dil' is an invalid keyword argument for bitwise_and()

dil 和 mask 数组显示我的大小和类型相同。

有什么问题吗?感谢您的回答

我正在 windows

PD:我有原始的Frame和mask

当我用这两个函数创建 bitwise_and 函数时,它给了我第三个函数:

但是我需要用原来的和最后一个来做("dil")....

res = cv.bitwise_and(frame,frame,mask=dil)

不是吗?你能更好地解释一下你想用矩阵做什么吗?

解释。

bitwise_and takes 4 keyword arguments - 源 1、源 2、目标图像和遮罩。您的 TypeError 发生是因为您按名称指定关键字参数 - dil=dil。 OpenCV 无法识别这一点,因此您会得到 TypeError。也许你想要 res = cv.bitwise_and(frame, frame, mask=dil)?