Matrix logical operation with different number of channels in OpenCV

I have a problem with a matrix logical operation. I want to use bitwise_andwith image A and image B to get the result of image C.

The image data type is everything Mat, image A was processed after some functions, and it was a binary image with three channels. Image B was also a binary image after some processing, but with only 1 channel.

Since the channel numbers are different, I get an error when bitwise_and.

How can I do to combine a channel or any other methods to solve this problem?

Here is a picture of A, B, C:

image A + image B = image C

+4
3

A B , . cvtColor 3 1 . , C:

Mat A; // CV_8UC3
Mat B; // CV_8UC1

, C 3 :

cvtColor(B, B, COLOR_GRAY2BGR);
Mat C;
bitwise_and(A,B,C); // C will be 3 channel, CV_8UC3

else, , C 1 :

cvtColor(A, A, COLOR_BGR2GRAY);
Mat C;
bitwise_and(A,B,C); // C will be 1 channel, CV_8UC1    

, , :

Mat C = A & B;
+3

Java.

1. webcam_image 3.

OPENCV 3.0

// convert 3  channel
Imgproc.cvtColor(thresholded, thresholded, Imgproc.COLOR_GRAY2BGR);
Core.bitwise_and(thresholded, webcam_image, webcam_image);

, .

+1

" " :

vector<Mat> channels(3);
// split img:
split(imageA, channels);

bitwise_and(channels[0], imageB, channels[0]);
bitwise_and(channels[1], imageB, channels[1]);
bitwise_and(channels[2], imageB, channels[2]);

, 3- Mat:

 merge(channels, imageC);
+1

Source: https://habr.com/ru/post/1613020/


All Articles