我得到了这个错误:
cv2.error: OpenCV(4.1.2) 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'
我已经阅读了this post,它描述了我的问题(其他问题没有),但我无法从那里找到我的答案。
下面是我的代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("manzara.jpg")
imgOther = cv2.imread("fuzuli.jpg") # shape of this is 559, 419, 3
width, height, channel = img.shape # 768, 1024, 3
roi = img[0:width, 0:height]
imgOtherGray = cv2.cvtColor(imgOther, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(imgOtherGray, 220, 255, cv2.THRESH_BINARY_INV)
antiMask = cv2.bitwise_not(mask)
img_background = cv2.bitwise_and(roi, roi, mask=antiMask) # where error occurs
imgOther_fg = cv2.bitwise_and(roi, roi, mask=mask)
dst = cv2.add(img_background, imgOther_fg)
img[0:width, 0:height] = dst
cv2.imshow("image", img)
发布于 2020-01-19 03:50:46
我已经知道我正在尝试处理错误的图像"img",img的形状比imgOther的形状大,所以这会导致问题。我更改了
width, height, channel = img.shape # 768, 1024, 3
roi = img[0:width, 0:height]
使用以下代码
width, height, channel = imgOther.shape # 768, 1024, 3
roi = img[0:width, 0:height]
我的问题就解决了
发布于 2020-01-28 08:10:35
我遇到了类似的问题,我相信您的问题源于您正在使用的感兴趣区域(roi)。简而言之:您尝试选择一个感兴趣的区域,该区域包含的坐标不在原始图像中。
在OpenCV中,您可以如下定义一个感兴趣区域(假设您已经打开了一个名为“image”的图像)
roi = image[y1:y2,x1:x2]
在这里,坐标(x1,y1)将与图像的左上角相关,坐标(x2,y2)将是图像的右下角。
在您的代码中,通过首先列出x(宽度),您混合了宽度(x)和高度(y)。OpenCV以相反的方式处理它们,首先使用y变量(高度)。您应该将您的roi变量更改为以下值:
roi = img[0:height,0:width]
这将创建一个感兴趣的区域,即整个图像的大小。
https://stackoverflow.com/questions/59799964
复制相似问题