Ask Your Question

bitwise_and giving Sizes of input arguments do not match

asked 2016-01-07 14:28:21 -0500

codersquirrel gravatar image

I'm trying to use bitwise in order to exclude all the rest of an image based on a threshold but when i try it gives

OpenCV Error: 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 binary_op, file /home/schirrel/Github/opencv/opencv-2.4.10/modules/core/src/arithm.cpp, line 1021 terminate called after throwing an instance of 'cv::Exception' what(): /home/schirrel/Github/opencv/opencv-2.4.10/modules/core/src/arithm.cpp:1021: error: (-209) 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 binary_op

My code is

Mat src, src_gray, dst;
int main() {
src = imread("robosoccer.jpg", 1);
cvtColor(src, src_gray, CV_BGR2GRAY);
threshold(src_gray, dst, 150, 255, 1);
Mat res;
bitwise_and(src, dst, res);
imshow("AND", res);
("hold", res);
return (0);


Can u guys help me?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-01-07 15:11:24 -0500

try by changing bitwise_and( src, src, res, dst );

edit flag offensive delete link more


This worked but instead of only shown the points of the mask at the image it exclude it, how to invert?

codersquirrel gravatar imagecodersquirrel ( 2016-01-08 11:32:32 -0500 )edit

could you explain by a picture? i did not understand what you mean by points.

sturkmen gravatar imagesturkmen ( 2016-01-08 11:38:15 -0500 )edit

Hey man, sorry,my threshold was inverted thats why it was not working properly, now its perfect thanks

codersquirrel gravatar imagecodersquirrel ( 2016-01-08 11:44:39 -0500 )edit

answered 2016-01-07 21:19:37 -0500

Tetragramm gravatar image

The bitwise_and function is just what it says, a bitwise function. So since your src image is 3 channels, and the dst image is one channel, they don't match.

For this simple case, use src.copyTo(res, dst);

This uses the dst as a mask on the copy. Just make sure dst is all zeros first or there maybe junk data where the mask is empty.

edit flag offensive delete link more



@StevenPuttemans I have to kinda disagree with you this time. @Tetragramm is right about one thing: src and dst are different type matrices, as dst is the thresholded version of src_gray (1 channel). I also think the OP is trying to do a copyTo with mask (even if he does not know how to). Regarding @sturkmen version, though valid code, I find it pretty nonsense for this reason: if you bitwise_and one matrix with itself, you get a full white matrix (all 255); if you bitwise_and it with using an extra mask, ok, then you have 255 elements only where the mask has 255 elements. So, why doing the bitwise_and at all? You already have your desired result in the used mask.

LorenaGdL gravatar imageLorenaGdL ( 2016-01-08 04:25:21 -0500 )edit

I need to refrain from my remark, I was indeed wrong ... apologies for that :D

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-08 06:14:14 -0500 )edit

Question Tools

1 follower


Asked: 2016-01-07 14:28:21 -0500

Seen: 14,280 times

Last updated: Jan 07 '16