Ask Your Question

Combining multiple cv::Mat images into single cv::Mat

asked 2013-03-29 04:24:33 -0600

I am working with the new C++ - style interface. I thought I could use the following trick to create an image based on 3 existing images, but the window stays clear of information. Anyone has an idea what I am doing wrong?

blue_channel = color_channels[0];
green_channel = color_channels[1];
red_channel = color_channels[2];

Above code show splitting into color channels which I want to represent next to eachother.

int rows = input_color.rows;
int cols = input_color.cols;

Mat result_channel(rows, cols * 3, CV_8UC1);
result_channel( Rect(0, 0, cols, rows) ) = blue_channel;
result_channel( Rect(cols, 0, cols, rows) ) = green_channel;
result_channel( Rect(cols*2, 0, cols, rows) ) = red_channel;

imshow("color channels", result_channel); cvWaitKey(10);

By using the roi parameter I wanted to assign the color channels to a region in the larger image. Result however is a blank window. Anyone has an idea what is happening?

edit retag flag offensive close merge delete


Why it show CV_8UC1 cannot be resolved to a variable when I write imageroiB.create(block,block,CV_8UC1);

maizi gravatar imagemaizi ( 2014-05-19 03:16:02 -0600 )edit

I do not understand your problem... This has quite nothing to do with this question. I think you simply need

  Mat imageroiB(block, block, CV_8UC1);
StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 04:50:03 -0600 )edit

Can I have your e-mail.I send the screenshots to you.

maizi gravatar imagemaizi ( 2014-05-19 06:59:21 -0600 )edit

No, just open your own question and add them there with some explanation ...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 07:01:01 -0600 )edit

OK ,thank you all the same! I write like this :Mat imageroiB(block,block,CV_8UC1) .it will appear a red break line under the word "Mat".And it can't run. I could not find the "CV_8UC1" in the options when I use the "Alt+/".

maizi gravatar imagemaizi ( 2014-05-19 07:11:41 -0600 )edit

Like I said, own question, add your code and then I might be able to tell the problem!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-19 07:51:19 -0600 )edit

Sorry for my poor English expression abilities. This is my question:

maizi gravatar imagemaizi ( 2014-05-19 09:11:53 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2013-03-29 04:45:41 -0600

AlexanderShishkov gravatar image

You also can use mixChannels() function:

Mat result_channel(rows, cols, CV_8UC3);
Mat in[] = { blue_channel, green_channel, red_channel };
int from_to[] = { 0,0, 1,1, 2,2 };
mixChannels( in, 3, &result_channel, 1, from_to, 3 );
edit flag offensive delete link more

answered 2013-03-29 04:31:47 -0600

Vladislav Vinogradov gravatar image

The Mat assignment operator doesn't copy data, it just copy pointers. To copy data use copyTo method:

blue_channel.copyTo( result_channel( Rect(0, 0, cols, rows) ) );
green_channel.copyTo( result_channel( Rect(cols, 0, cols, rows) ) );
red_channel.copyTo( result_channel( Rect(cols*2, 0, cols, rows) ) );
edit flag offensive delete link more


Thx for the answer :) I was already experimenting with the copyTo operator, but switched the order of what to copy to what exactly. The above did the trick!

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-29 04:37:31 -0600 )edit

Question Tools

1 follower


Asked: 2013-03-29 04:24:33 -0600

Seen: 17,293 times

Last updated: Mar 29 '13