opencv c++ 实现图像拼接(vconcat函数)


(25条消息) opencv c++ 图片拼接,读取地址处理_Sir.雨田的博客-CSDN博客

前几天手写了一个拼接垂直图片的小控制台程序,今天翻了一下文件夹,发现一个opencv自带的拼接函数vconcat(用于垂直拼接)和hconcat(用于水平拼接),很方便。

主函数,展示concat用法:前两个参数是用于拼接的源图像,最后一个是拼接完的图像

int main()
{
    Mat img_above,img_below,img_result;
    string path_above,path_below;
    cout<<"Type the path of the image spliced ABOVE:"<<"\n";
    cin>>path_above;
    path_above=path_process(path_above);
    cout<<"Type the path of the image spliced BELOW"<<"\n";
    cin>>path_below;
    path_below=path_process(path_below);
    img_above=imread(path_above);
    img_below=imread(path_below);
    vconcat(img_above,img_below,img_result);
    imwrite("img_result.jpg",img_result);
    return 0;
}
这个是上一次写的函数,用于处理直接拷贝的路径
详见:
https://blog.csdn.net/lbzyyds/article/details/130475637?spm=1001.2014.3001.5501

string path_process(string str){
    for(int i=0;i<str.size();i++){
        char text_tmp=str[i];
        if(text_tmp=='\\'){ //"\\"代表一个"\"字符
            str[i]='/';
        }
        if(text_tmp=='"'){
            str[i]=' ';
        }
    }
    str.erase(0, str.find_first_not_of(" "));   
    //删除0到第一个不是空格的字符
    str.erase(str.find_last_not_of(" ")+1, str.length());
    //删除最后一个不是空格的字符到字符串结尾
    return str;
}
合在一起就是

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
string path_process(string str){
    for(int i=0;i<str.size();i++){
        char text_tmp=str[i];
        if(text_tmp=='\\'){ //"\\"代表一个"\"字符
            str[i]='/';
        }
        if(text_tmp=='"'){
            str[i]=' ';
        }
    }
    str.erase(0, str.find_first_not_of(" "));
    //删除0到第一个不是空格的字符
    str.erase(str.find_last_not_of(" ")+1, str.length());
    //删除最后一个不是空格的字符到字符串结尾
    return str;
}
int main()
{
    Mat img_above,img_below,img_result;
    string path_above,path_below;
    cout<<"Type the path of the image spliced ABOVE:"<<"\n";
    cin>>path_above;
    path_above=path_process(path_above);
    cout<<"Type the path of the image spliced BELOW"<<"\n";
    cin>>path_below;
    path_below=path_process(path_below);
    img_above=imread(path_above);
    img_below=imread(path_below);
    vconcat(img_above,img_below,img_result);
    imwrite("img_result.jpg",img_result);
    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值