Ask Your Question
1

adjacent images in single window

asked 2013-09-10 05:59:47 -0600

yum gravatar image

Hi!I am using C++ and opencv..I need to display 3 images adjacent to each other in a single window. Can someone help please

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-09-10 06:20:00 -0600

Moster gravatar image

Ok, shouldnt be that hard. I assume that all have the same height (otherwise find the biggest height) The 3 input images are img1, img2, img3.

cv::Size s1 = img1.size();
cv::Size s2 = img2.size();
cv::Size s3 = img3.size();

cv::Mat output(s1.height, s1.width + s2.width + s3.width, CV_MAT_TYPE); // put in the type of your mat

cv::Mat help1(output, cv::Rect(0,0, s1.width, s1.height);
cv::Mat help2(output, cv::Rect(s1.width, 0, s2.width, s2.height);
cv::Mat help3(output, cv::Rect(s1.width + s2.width, 0, s3.width, s3.height);

img1.copyTo(help1);
img2.copyTo(help2);
img3.copyTo(help3);

cv::imshow("Output", output);

Here is how this works: You create an output image that has the total size of all 3 images. Then you create 3 Mats that only point to a part of your output image that equals their respective input image. This only works since Mat(output, Rect()) does not copy the output Mat to a new one, it only points to it. So by img.copyTo(help) you copy the input image to the respective part in output.

edit flag offensive delete link more

Comments

thanks :)

will try this and see if it works :)

yum gravatar imageyum ( 2013-09-10 06:41:07 -0600 )edit

omyyygodddd it workssss!!!!!! hehe thanks again :))

yum gravatar imageyum ( 2013-09-10 08:12:58 -0600 )edit

No problem :)

Moster gravatar imageMoster ( 2013-09-10 08:16:57 -0600 )edit

for Mat output and Mat help1,help2,help3..can you tell me exactly what are the parameters exactly because they seem to be different for Mat output and Mat help1/2/3..

yum gravatar imageyum ( 2013-09-10 08:29:00 -0600 )edit

Which parameter do you mean?

Moster gravatar imageMoster ( 2013-09-10 08:37:32 -0600 )edit

cv::Mat output(s1.height, s1.width + s2.width + s3.width, CV_MAT_TYPE) and cv::Mat help2(output, cv::Rect(s1.width, 0, s2.width, s2.height);..what are those parameters in general?what does s1.height,s1.width + s2.width + s3.width, CV_MAT_TYPE mean in general?hope iim being clear

yum gravatar imageyum ( 2013-09-10 08:42:07 -0600 )edit

CV_MAT_TYPE( just a placeholder) is the image type of your input images. They have to be the same. So you can basically just do: cv::Mat output(s1.height, s1.width + s2.width + s3.width, img1.type())

s1, s2, s3 are the sizes of your input images. That are also the first 3 statements in my answer. s1.height is obviously the height of one of your input images (make sure you take the biggest height there). Your output image needs to have this height. s1.width + s2.width + s3.width is the total width of your output image, since you want to put them all next to each other.

With cv::Rect(s1.width, 0, s2.width, s2.height) you determine the size a rectangle and also its position. This is for example the rectangle of your 2nd image (you could call it region of interest).

Moster gravatar imageMoster ( 2013-09-10 08:51:59 -0600 )edit

You then apply this rectangle on the output image to get the region of interest where you copy the corresponding input image to.

Moster gravatar imageMoster ( 2013-09-10 08:52:50 -0600 )edit

sorryy..maybe i wasnt clear enough :/ cv::Mat is a data structure used to hold images and other matrix data. For example cv::Mat ima(240,320,CV_8U,cv::Scalar(100)); "ima" is the name given to the image, CV_8U is the type of the image,that is, whether it is colored or black and white, 240 and 320 i dont know whether they are coordinates etc

Now in general : Mat "imageName"(int rows,int cols,type,etcccc)

I need to know exactly what the data inside the brackets mean in general, like for Rect(int x, int y, int width, int height)

thanks for the additional details

yum gravatar imageyum ( 2013-09-10 09:13:45 -0600 )edit

As the name says, Mat is just a Matrix that has rows and columns. Each entry of the mat has a value and is determined by the row and column. So if you take Mat as an image container, the rows correspond to the image height and columns correspond to the width. Every entry of the Mat is a pixel value. So you have width * height entries (1 channel image) The 240 and 320 in your example just determine the size of your matrix.

Moster gravatar imageMoster ( 2013-09-10 09:33:58 -0600 )edit

Question Tools

Stats

Asked: 2013-09-10 05:59:47 -0600

Seen: 1,679 times

Last updated: Sep 10 '13