cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:214

运行以下代码对图像进与运算 

import cv2
import numpy as np
img = cv2.imread('e.jpg')
row,col=img.shape[:2]
c=np.zeros(img.shape[:2],dtype='uint8')
cv2.circle(c,(row//2,col//2),100,255,-1)
result=cv2.bitwise_and(img,c)
cv2.imshow('c',img)
cv2.imshow('circle',c)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destoryAllWindows()

报错

 说进行与运算的两张图片大小不匹配,应该是我另一张图片是用np.zeros创建出来的图片,另一张确实彩色图像,不匹配。将彩色图片以灰色模式读入,再做与运算

img = cv2.imread('e.jpg',cv2.IMREAD_GRAYSCALE)

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:214_第1张图片

 如果我选择两张彩色图片则图片正常显示

import cv2
import numpy as np
img = cv2.imread('e.jpg')
row,col=img.shape[:2]
c=cv2.imread('d.jpg')
cv2.circle(c,(row//2,col//2),100,255,-1)
result=cv2.bitwise_and(img,c)
cv2.imshow('c',img)
cv2.imshow('circle',c)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destoryAllWindows()

你可能感兴趣的:(python图像处理,python)