Programming Error/Python

[OpenCV 오류] error: OpenCV -209:Sizes of input arguments do not match

Barca 2021. 7. 11. 14:18

error: OpenCV(4.4.0) pip-req-build-wef8acrf\opencv\modules\core\src\arithm.cpp:669: 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'.

 

Python을 사용하던 도중 cv2.multiply 함수를 사용할 일이 있었는데 3채널과 1채널짜리의 동일한 H,W 크기의 이미지를 서로 곱해주려고 했는데, 위와 같은 에러가 발생했습니다.

 

파이썬에서 3채널과 1채널짜리 동일한 크기 이미지를 곱하면 파이썬의 브로드캐스팅으로 인해 자동적으로 Element-wise하게 곱해지는데, OpenCV 함수를 사용할 때는 채널까지 정확하게 3채널로 동일하게 맞추어줘야 작동하더라구요. cv2.multiply 외에도 cv2.add, cv2.subtract, cv2.divide를 사용할때도 위 이슈를 생각하면서 연산하면 잘 작동할 것 같습니다.