图像在水平方向的简单连接

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main (int argc, char **argv)
{
  int img_num = argc > 1 ? argc-1 : 2;
  const char defaultfile[2][32] = {"../image/moon.png", "../image/sunset.png"};
  int total_width=0, max_height=0;  
 
  // (1)load all images specified on the command line
  vector<Mat> src_img;
  for(int i=0; i<img_num; i++) {
    src_img.push_back(imread(argc-1?argv[i+1]:defaultfile[i]));
    Mat last = src_img.back();
    if(!last.data)
      return -1;
    total_width += last.cols;
    max_height = max_height < last.rows ? last.rows : max_height;
  }
 
  // (2)append images laterally one after another
  Mat combined_img(Size(total_width, max_height), CV_8UC3);
  vector<Mat>::iterator it = src_img.begin(), it_end = src_img.end();
  Rect roi_rect;
  for(; it!=it_end; ++it) {
    roi_rect.width = it->cols;
    roi_rect.height = it->rows;
    Mat roi(combined_img, roi_rect);
    it->copyTo(roi);
    roi_rect.x += it->cols;
  }
 
  // (3)show the combined image, and quit when any key pressed
  namedWindow("Combined Image", CV_WINDOW_AUTOSIZE);
  imshow("Combined Image", combined_img);
  waitKey(0);
 
  return 0;
}

 

 

源图像:


 

运行后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值