我正在尝试使用OpenCV的位非运算符在图像上应用掩码。如果我将原始图像和掩码图像都以灰度模式读取,则可以实现此结果,但在3通道图像上无法实现。
我已阅读此线程OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op,但我的问题不是数组形状或掩码不是uint8格式。
我希望输出的结果是原始图像与标签掩码合并,但我遇到了错误。
我已阅读此线程OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op,但我的问题不是数组形状或掩码不是uint8格式。
import cv2
import numpy as np
img = cv2.imread("Original.png") # original image, shape 544,480,3, dtype uint8
label = cv2.imread("Mask.png") # black and white mask,shape 544,480,3, dtype uint 8
shape = img.shape # 544,480,3
black_background = np.zeros(shape=shape, dtype=np.uint8)
result = cv2.bitwise_not(img,black_background,mask=label) # this is where error occurs
cv2.imwrite("masked.png",result)
我希望输出的结果是原始图像与标签掩码合并,但我遇到了错误。
OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:245: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
uint8 (544, 480, 3) uint8 (544, 480, 3) uint8 (544, 480, 3)
- Andrei Tsitsvidze