Ask Your Question
2

How to update Mat with multiple channels?

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

napalm gravatar image

updated 2012-09-04 10:12:23 -0500

Andrey Pavlenko gravatar image

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)?

Example:

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.

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

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

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
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 4,273 times

Last updated: Sep 04 '12