需要 cv2 错误帮助:(-209:输入参数的大小不匹配)

问题描述 投票:0回答:3

错误:(-209:输入参数的大小不匹配)该操作既不是“数组操作数组”(其中数组具有相同的大小和类型),也不是“数组操作标量”,也不是函数中的“标量操作数组” 'cv::binary_op'

import cv2 as cv
import numpy as np

# Opens the image
image = cv.imread('numbers.jpg')
width = image.shape[1]
height = image.shape[0]

resized = cv.resize(image, (int(image.shape[1] * 0.5), int(image.shape[0] * 0.5)), interpolation=cv.INTER_LINEAR)
cropped_image = resized[int(width/9.6):int(width/4.8), int(height/4.32):int(height/(54/35))]
print(width, height)
cv.imshow('resized',cropped_image)

# Image processing
blank = np.zeros(image.shape, dtype='uint8')
gray = cv.cvtColor(cropped_image, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 120, 255,cv.THRESH_BINARY)
inverted_tresh = cv.bitwise_not(thresh)
cv.imshow('thresh', inverted_tresh)

# count white pixels
print(cv.countNonZero(inverted_tresh))

这段代码出错了,我不明白为什么:

# Shape masking
masked_image = cv.bitwise_and(image, inverted_tresh)
cv.imshow('masked', masked_image)

cv.waitKey(0)
python opencv
3个回答
0
投票

我认为@edornd 在这里是正确的。 当您将 (image, inverted_tresh) 传递给 cv.bitwise_and() 或任何按位布尔运算符函数时,请确保两个参数的大小相同。

h1, w1 = image.shape
h2, w2 = inverted_tresh.shape
if h1 * w1 > h2 * w2:
    inverted_tresh = cv2.resize(inverted_tresh, (w1, h1))
else:
    image = cv2.resize(image, (w2, h2))

-1
投票

从你的代码来看,你正在尝试使用源自裁剪版本的inverted_tresh来掩盖

完整
图像(而不是裁剪后的图像)。您的错误实际上表明您的掩码函数中的数组很可能没有相同的大小和类型


-1
投票

这是屏蔽两个对象的写入方式。

# Shape masking
masked_image = cv.bitwise_and(cropped_image, cropped_image, mask=thresh)
cv.imshow('masked', masked_image)

cv.waitKey(0)

© www.soinside.com 2019 - 2024. All rights reserved.