Placement of two images side by side, opencv 2.3, C ++

I #m read two images and want to get a third, which is just a combination of the two. img_object and img_scene are not the same size.

int main( int argc, char** argv ) Mat combine; Mat img_object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE ); Mat img_scene = imread( scene_filename , CV_LOAD_IMAGE_GRAYSCALE ); if( !img_object.data || !img_scene.data ) { std::cout<< " --(!) Error reading images " << std::endl; return -1; } namedWindow( "Display window oject", 0 );// Create a window for display. namedWindow( "Display window scene ", 0 ); namedWindow( "Display window combine ", 0 ); imshow( "Display window oject", img_object ); imshow( "Display window scene", img_scene ); imshow( "Display window scene", combine ); waitKey(0); return 0; } 
+4
source share
5 answers

There is a very simple way to display two images side by side. You can use the following function, which is provided by opencv.

 Mat image1, image2; hconcat(image1,image2,image1);//Syntax-> hconcat(source1,source2,destination); 

This function can also be used to copy a set of columns from an image to another image.

 Mat image; Mat columns=image.colRange(20,30); hconcat(image,columns,image); 
+5
source
 // -------------------------------------------------------------- // Function to draw several images to one image. // Small images draws into cells of size cellSize. // If image larger than size of cell ot will be trimmed. // If image smaller than cellSize there will be gap between cells. // -------------------------------------------------------------- char showImages(string title, vector<Mat>& imgs, Size cellSize) { char k=0; namedWindow(title); float nImgs=imgs.size(); int imgsInRow=ceil(sqrt(nImgs)); // You can set this explicitly int imgsInCol=ceil(nImgs/imgsInRow); // You can set this explicitly int resultImgW=cellSize.width*imgsInRow; int resultImgH=cellSize.height*imgsInCol; Mat resultImg=Mat::zeros(resultImgH,resultImgW,CV_8UC3); int ind=0; Mat tmp; for(int i=0;i<imgsInCol;i++) { for(int j=0;j<imgsInRow;j++) { if(ind<imgs.size()) { int cell_row=i*cellSize.height; int cell_col=j*cellSize.width; imgs[ind].copyTo(resultImg(Range(cell_row,cell_row+tmp.rows),Range(cell_col,cell_col+tmp.cols))); } ind++; } } imshow(title,resultImg); k=waitKey(10); return k; } 
+3
source

If the images do not have the same size, the combine width will be equal to the sum of the width, but the height should be greater than the height of the two images.

Define a combination image as follows:

 Mat combine(max(img_object.size().height, img_scene.size().height), img_object.size().width + img_scene.size().width, CV_8UC3); 

Note that we simply create a new Mat object with a height equal to the maximum height and a width equal to the combined width of the images (if you need a small distance between the images, you need to consider this here).

Then you can define areas of interest for each side inside combine (using the convenient Mat constructor) and finally copy each image to the corresponding side (here I assume that the object goes to the left and the scene goes to the right):

 Mat left_roi(combine, Rect(0, 0, img_object.size().width, img_object.size().height)); img_object.copyTo(left_roi); Mat right_roi(combine, Rect(img_object.size().width, 0, img_scene.size().width, img_scene.size().height)); img_scene.copyTo(right_roi); 

Edit: Fixed the typo that Timzaman indicated.

+2
source

You can do this with a loop, assuming your images are the same size:

 Mat combine = Mat::zeros(img_object.rows,img_object.cols *2,img_object.type()); for (int i=0;i<combine.cols;i++) { if (i < img_object.cols) { combine.col(i) = img_object.col(i); } else { combine.col(i) = img_scene.col(i-img_object.col); } } 

I have not tested it, but the way you can do it

+1
source

I tried putting several images side by side, just try this.

 Mat combine = Mat::zeros(img_buff[0].rows, img_buff[0].cols * (int)img_index.size(), img_buff[0].type()); int cols = img_buff[0].cols; for (int i=0;i<combine.cols;i++) { int fram_index = i / img_buff[0].cols; cout<<fram_index<<endl; img_buff[fram_index].col(i % cols).copyTo(combine.col(i)); } imshow("matching plot", combine); 

Note that when copying columns from one image to another, do the following:

 A.row(j).copyTo(A.row(i)); 

Do not do this:

 A.row(j) = A.row(i); 
0
source

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


All Articles