【发布时间】:2020-11-23 16:26:49
【问题描述】:
所以我有一个 Mat 对象数组(jpeg 图像),我想将其转换为 MxN 数组,因此最终输出将是由数组中的所有输入图像组成的图像,从左到右放入矩阵对,然后从上到下。假设所有输入图像的大小相同,我如何在 C++ 中使用 Opencv 做到这一点?
非常感谢
【问题讨论】:
所以我有一个 Mat 对象数组(jpeg 图像),我想将其转换为 MxN 数组,因此最终输出将是由数组中的所有输入图像组成的图像,从左到右放入矩阵对,然后从上到下。假设所有输入图像的大小相同,我如何在 C++ 中使用 Opencv 做到这一点?
非常感谢
【问题讨论】:
应该这样做:
#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 );
}
【讨论】: