Ask Your Question

How to update Mat with multiple channels?

asked 2012-09-04 09:30:13 -0600

napalm gravatar image

updated 2020-10-20 12:12:51 -0600

I have three CV_8U Mat objects(Mat r,g,b) representing each one channels of an image. How to update an CV_8UC3 Mat with that separated channels(r, g, b)?


Mat rgb = new Mat(height, width,  CvType.CV_8UC3); 

Mat r = new Mat(height, width, CvType.CV_8U);
Mat g = new Mat(height, width, CvType.CV_8U);
Mat b = new Mat(height, width, CvType.CV_8U);

...Do some process in r, g and b objects...

Now how I can update a red channel of 'rgb' with 'r' object, a green channel of 'rgb' with 'g' and a blue channel with 'b'?

Obs: I'm using OpenCV for Android.


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-09-04 10:19:25 -0600

Andrey Pavlenko gravatar image

If you need to create a single Mat from several ones (of the same sizes and types) representing its channels, the best choice is the cv::merge() function.

In Java it looks following:

public void testMerge() {
    Mat src1 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(1));
    Mat src2 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(2));
    Mat src3 = new Mat(2, 2, CvType.CV_32FC1, new Scalar(3));
    Mat dst  = new Mat();
    List<Mat> listMat = Arrays.asList(src1, src2, src3);

    Core.merge(listMat, dst);

    truth = new Mat(2, 2, CvType.CV_32FC3, new Scalar(1, 2, 3));
    assertMatEqual(truth, dst, EPS);
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2012-09-04 09:30:13 -0600

Seen: 6,393 times

Last updated: Sep 04 '12