Skip to content

Instantly share code, notes, and snippets.

@drabaioli
Created December 1, 2020 15:54
Show Gist options
  • Save drabaioli/71bf7b3728452e337f033e3082a4d6a5 to your computer and use it in GitHub Desktop.
Save drabaioli/71bf7b3728452e337f033e3082a4d6a5 to your computer and use it in GitHub Desktop.
Stitch images into a MxN collage
#include <opencv2/opencv.hpp>
cv::Mat imageCollage( std::vector<cv::Mat> & array_of_images, int M, int N )
{
// All images should be the same size
const cv::Size images_size = array_of_images[0].size();
// Create a black canvas
cv::Mat image_collage( images_size.height * N, images_size.width * M, CV_8UC3, cv::Scalar( 0, 0, 0 ) );
for( int i = 0; i < N; ++i )
{
for( int j = 0; j < M; ++j )
{
if( ( ( i * M ) + j ) >= array_of_images.size() )
break;
cv::Rect roi( images_size.width * j, images_size.height * i, images_size.width, images_size.height );
array_of_images[ ( i * M ) + j ].copyTo( image_collage( roi ) );
}
}
return image_collage;
}
int main()
{
std::vector<cv::Mat> array_of_images;
array_of_images.push_back( cv::imread( "1.jpg" ) );
array_of_images.push_back( cv::imread( "2.jpg" ) );
cv::Mat image_collage = imageCollage( array_of_images, 3, 3 );
cv::imshow( "Image Collage", image_collage );
cv::waitKey( 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment