Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在OpenCV Python中出现按位和发送错误?_Python_Opencv_Cv2_Bitwise And_Opencv4 - Fatal编程技术网

为什么在OpenCV Python中出现按位和发送错误?

为什么在OpenCV Python中出现按位和发送错误?,python,opencv,cv2,bitwise-and,opencv4,Python,Opencv,Cv2,Bitwise And,Opencv4,我试图在第一张图像上通过第二张图像的遮罩 我得到以下错误: cv2.error:OpenCV(4.2.0)/io/OpenCV/modules/core/src/arithm.cpp:250:error:(-215:Assertion failed)(mtype==CV|8U||mtype==CV|8S)和&&u mask.sameSize(*psrc1)在函数“binary|op”中 我遵循了这个教程:并且得到了另一个错误 代码: listFileEtiFinal = getImagesRGB

我试图在第一张图像上通过第二张图像的遮罩

我得到以下错误:

cv2.error:OpenCV(4.2.0)/io/OpenCV/modules/core/src/arithm.cpp:250:error:(-215:Assertion failed)(mtype==CV|8U||mtype==CV|8S)和&&u mask.sameSize(*psrc1)在函数“binary|op”中

我遵循了这个教程:并且得到了另一个错误

代码:

listFileEtiFinal = getImagesRGB("ImgResult","ETIFinal.png") + getImagesRGB("ImgEtiSelect","ETIFinal.png")
listFileEti = getImagesRGB("ImgResult","ETI.png") + getImagesRGB("ImgEtiSelect","ETI.png")

for fileImgF in listFileEtiFinal:
    for fileImgG in listFileEti:
            dos = fileImgF[0] + "/"
            fileEti = fileImgF[1]
            dosTwo = fileImgG[0] + "/"
            fileEtiG = fileImgG[1]
            nb = int(re.findall('\d+', fileEti)[0])
            img = cv2.imread(dos + fileEti)
            img2 = cv2.imread(dos + fileEtiG)
            img_gauss = cv2.GaussianBlur(img2, ksize=(11,11),sigmaX=5)
            _, img_gauss_th = cv2.threshold(img_gauss, thresh=243, maxval=255, type=cv2.THRESH_BINARY)
            img2gray = cv2.cvtColor(img_gauss_th,cv2.COLOR_BGR2GRAY)
            _, mask = cv2.threshold(img2, 200, 255, cv2.THRESH_BINARY_INV)
            mask_inv = cv2.bitwise_not(mask)
            rows,cols,_ = img2.shape
            roi = img[0:rows, 0:cols ]
            img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
            img2_fg = cv2.bitwise_and(roi,roi,mask = mask)  
            out_img = cv2.add(img1_bg,img2_fg)
            img[0:rows, 0:cols ] = out_img
            cv2.imwrite(dos + img , out_img)
根据,
掩码
必须是单通道阵列

掩码–可选操作掩码,8位单通道阵列,用于指定要更改的输出阵列元素

您发布的代码使用带有3个频道的掩码。
您可以检查
mask\u inv.shape
mask.shape

您只能使用掩模的一个通道(仅用于测试):

或者更好,将图像转换为阈值之前的灰度:

_, mask = cv2.threshold(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY_INV)

您是否验证是否满足断言条件?看起来遮罩类型应该是
CV_8U
CV_8S
,并且遮罩的大小应该与源图像相同。
_, mask = cv2.threshold(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY_INV)