[Python] Need help in cv2 error: (-209:Sizes of input arguments do not match)

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo


    error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function '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))


    This code made the error and I don't understand why:

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

    cv.waitKey(0)

    Continue reading...

Compartilhe esta Página