Почему выдаётся ошибка, хотя размеры массивов должны быть идентичными?
Мне необходимо сделать так, чтобы область интереса на изображении стала черно-белой, а все остальные пиксели остались прежними.
У меня получилось написать следующий код:
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('a meme 2nd.png')
mask = np.zeros(img.shape[:2], np.uint8)
mask[326:326+124, 349:349+106] = 255
masked_img = cv2.bitwise_and(img, img, mask=mask)
crop_img = masked_img[326:326+124, 349:349+106]
crop_img_gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
crop_img_gray_bgr = cv2.cvtColor(crop_img_gray, cv2.COLOR_GRAY2BGR)
result = cv2.addWeighted(masked_img, 1, crop_img_gray_bgr, 1, 0)
cv2.imshow('', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Но почему-то при попытке выполнить код выдаёт ошибку:
"cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'".
Она явно означает различие в размерах массивов в .addWeighted(), но понятия не имею, почему такое происходит, потому что в строке
crop_img_gray_bgr = cv2.cvtColor(crop_img_gray, cv2.COLOR_GRAY2BGR)
как раз и происходит "подгонка" размера массива ч/б изображения.
Помогите с этим, пожалуйста. Заранее спасибо.