完整示例:
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
int main(int argc, char **argv)
{
if (argc != 2)
{
return 1;
}
cv::Mat image = cv::imread(argv[1]);
if (image.empty())
{
std::cout << "无法读取图片" << std::endl;
return 1;
}
std::cout << "图片高度: " << image.rows << "宽度: " << image.cols << std::endl;
cv::Mat cat_img = cv::imread("./media/cat.jpg");
cv::Mat cat_resize;
cv::resize(cat_img, cat_resize, cv::Size(320, 240));
cv::Mat dog_resize;
cv::resize(image, dog_resize, cv::Size(320, 240));
cv::Mat cat_resize_copy;
cat_resize.copyTo(cat_resize_copy);
cv::Mat dog_resize_copy;
dog_resize.copyTo(dog_resize_copy);
cv::Mat img1;
cv::hconcat(cat_resize_copy, dog_resize_copy, dog_resize_copy);
cv::Mat cat_resize_copy1;
cat_resize.copyTo(cat_resize_copy1);
cv::Mat dog_resize_copy1;
dog_resize.copyTo(dog_resize_copy1);
cv::vconcat(cat_resize_copy1, dog_resize_copy1, dog_resize_copy1);
std::vector<cv::Mat> imgs{cat_resize, dog_resize, cat_resize, dog_resize};
cv::Mat hconcat_img2;
cv::hconcat(imgs, hconcat_img2);
cv::imshow("opencv 水平拼接", dog_resize_copy);
cv::imshow("opencv 垂直拼接", dog_resize_copy1);
cv::imshow("opencv vector方式水平拼接", hconcat_img2);
cv::waitKey(0);
return 0;
}