[C++] 如何使用opencv对多个图像进行横向或者纵向拼接

27 篇文章 1 订阅

什么是图像拼接?

图像拼接是指将多张图片按照一定的规则和算法进行组合,形成一张大图的过程。在实际应用中,常常需要将多张拍摄的图片拼接成一幅完整的场景或物体照片。

图像拼接是一个图像处理过程中常见的应用场景。我们可以通过opencv库非常容易实现多个图像的横向或者纵向的拼接。

使用的opencv函数或者类

Mat类

[C++] opencv - Mat类的介绍和使用场景-CSDN博客

copyTo函数

[C++] opencv - copyTo函数介绍和使用案例-CSDN博客

convertTo函数

[C++] opencv - convertTo函数介绍和使用场景-CSDN博客

imwrite函数

[C++] opencv - imwrite函数介绍和使用场景_c++ opencv imwrite-CSDN博客

imread函数

[C++] opencv - imwrite函数介绍和使用场景_c++ opencv imwrite-CSDN博客

源代码

/**
 * 处理结果,可用于表示某个方法的处理状态。
*/
struct ProcResult
{
    int StatusCode; // 状态码
    std::string StatusDetail; // 状态详细信息,可以用来提供与对应状态的详细信息
};


struct ImageCombinerParam
{
    std::vector<std::string> subImagePaths; // 要进行拼接的图像路径的列表
    std::vector<cv::Mat> subImageMats;  // 要进行拼接的图像的列表
    bool byPath = true;  // 按照路径subImagePaths来进行拼接,如果为false,则自己来设置subImageMats
    std::string saveImagePath;  // 拼接后图像保存的路径
    bool enableImgBoarder = false;  // 是否给要拼接的图像,进行边框绘制,方便观察原始图像在拼接完之后图像中的位置
    int direction = 1; // 1 - 横向, 2- 纵向
};

std::vector<std::string> Utils::findFilesByExt(const std::string dir, const std::string ext){
    std::vector<std::string> foundFiles;
    for (const auto& entry : fs::directory_iterator(dir)) {
        if (entry.is_regular_file() && entry.path().extension() == ext) {
            foundFiles.push_back(entry.path().string());
        }
    }
    return foundFiles;
};


cv::Vec3b CVHelper::getRandColor(){
    // 获取当前时间点
    auto now = std::chrono::system_clock::now();
    
    // 将时间点转换为time_t类型
    std::time_t currentTime = std::chrono::system_clock::to_time_t(now);

    // 初始化随机数种子
    // srand(static_cast<unsigned int>(currentTime)); 
    // srand(static_cast<unsigned int>(time(0)));

    // 生成随机颜色
    cv::Vec3b randomColor(rand() % 256, rand() % 256, rand() % 256);
    // cv::Scalar randomColor(rand() % 256, rand() % 256, rand() % 256);

    return randomColor;
};

ProcResult ImageCombiner::combineSubImages(ImageCombinerParam param){
    ProcResult result;
    if((param.byPath && param.subImagePaths.size() <= 0) || (!param.byPath && param.subImageMats.size() <=0)){
        std::cerr << "there is no images." << std::endl;
        result.StatusCode = 1;
        result.StatusDetail = "there is no images.";
        return result;
    }
    std::vector<cv::Mat> imageMats;
    if(param.byPath){
        for(size_t i = 0; i < param.subImagePaths.size(); i++){
            cv::Mat img = cv::imread(param.subImagePaths[i]);
            if(!img.empty()){
                if(imageMats.size() > 0 && img.type()!= imageMats[0].type()){
                    img.convertTo(img, imageMats[0].type());
                }
                imageMats.push_back(img);
            }else{
                std::string errMsg = "fail to read image. img_path:" + param.subImagePaths[i];
                SPDLOG_ERROR(errMsg);
            }
        }
    }else{
        imageMats = param.subImageMats;
    }

    if(param.byPath && imageMats.size() != param.subImagePaths.size()){
        result.StatusCode = 2;
        std::string errorMsg = "fail to read all images.";
        result.StatusDetail = errorMsg;
        return result;
    }else{
        if(fs::exists(param.saveImagePath)){
            fs::remove(param.saveImagePath);
        }
        fs::path savePath(param.saveImagePath);
        if(!fs::exists(savePath.parent_path())){
            fs::create_directories(savePath.parent_path());
        }
        if (param.direction == 1){ // 横向合并
            int combinedWidth = 0;
            int combinedHeight = 0;
            for(size_t i = 0; i < imageMats.size(); i++){
                cv::Mat img = imageMats[i];
                combinedWidth += img.cols;
                if(img.rows > combinedHeight){
                    combinedHeight = img.rows;
                }
                if(param.enableImgBoarder){
                    cv::Vec3b randomColor = CVHelper::getRandColor();
                    cv::rectangle(img, cv::Point(0, 0), cv::Point(img.cols, img.rows),  randomColor, 5, cv::LINE_8);
                }
            }
    
            cv::Mat combinedImg = cv::Mat(combinedHeight, combinedWidth , imageMats[0].type(), cv::Scalar(0, 0, 0));
            int startX = 0;
            int startY = 0;
            for(size_t i = 0; i < imageMats.size(); i++){
                cv::Mat img = imageMats[i];
                imageMats[i].copyTo(combinedImg(cv::Rect(startX, startY, img.cols, img.rows)));
                startX += img.cols;
            }
            cv::imwrite(param.saveImagePath, combinedImg);
            result.StatusCode = common::STATUS_CODE_OK;
            result.StatusDetail = "combine success by cols";
            return result;
        }else{ // 纵向合并
            int combinedWidth = 0;
            int combinedHeight = 0;
            for(size_t i = 0; i < imageMats.size(); i++){
                cv::Mat img = imageMats[i];
                combinedHeight += img.rows;
                if(img.cols > combinedWidth){
                    combinedWidth = img.cols;
                }
                
                if(param.enableImgBoarder){
                    cv::Vec3b randomColor = CVHelper::getRandColor();
                    cv::rectangle(img, cv::Point(0, 0), cv::Point(img.cols, img.rows),  randomColor, 5, cv::LINE_8);
                }
            }
    
            cv::Mat combinedImg = cv::Mat(combinedHeight, combinedWidth , imageMats[0].type(), cv::Scalar(0, 0, 0));
            int startX = 0;
            int startY = 0;
            for(size_t i = 0; i < imageMats.size(); i++){
                cv::Mat img = imageMats[i];
                imageMats[i].copyTo(combinedImg(cv::Rect(startX, startY, img.cols, img.rows)));
                startY += img.rows;
            }
            
            cv::imwrite(param.saveImagePath, combinedImg);
            result.StatusCode = common::STATUS_CODE_OK;
            result.StatusDetail = "combine success by rows";
            return result;
        }
    }
};

测试代码

测试代码中使用了命令行参数,如何配置cxxopts,可以阅读 [C++] 第三方库命令行解析库argparse和cxxopts介绍和使用-CSDN博客

#include <filesystem>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <algorithm.hpp>
#include <cxxopts.hpp>

using namespace cv;
using namespace std;
namespace fs = std::filesystem;
namespace fdect = flaw_detect;

int main(int argc,char **argv){
    try{
        cxxopts::Options options("combine_image", "Cut multiple images to a big by a image list");
        options.add_options()
            ("imgdir", "要合并的图像所在目录", cxxopts::value<std::string>()->default_value("D:/LocalTest/ResizeImages/img_for_combine"))
            ("img_bd", "是否给要合并的图像添加边框", cxxopts::value<bool>()->default_value("true"))
            ("reverse", "按图像名称进行倒序", cxxopts::value<bool>()->default_value("true"))
            ("savepath", "合并之后的图像保存的路径", cxxopts::value<std::string>()->default_value("D:/LocalTest/ResizeImages/resize_combined.bmp"))
            ("help", "使用帮助");
        
        auto cliArgs = options.parse(argc, argv);
        if (cliArgs.count("help"))
        {
            std::cout << options.help() << std::endl;
            exit(0);
        }
        std::string imgDir = cliArgs["imgdir"].as<std::string>();
        bool enableImgBorder =  cliArgs["img_bd"].as<bool>();
        bool reverse =  cliArgs["reverse"].as<bool>();
        std::string savePath = cliArgs["savepath"].as<std::string>();
        
        std::cout << "imgDir:" << imgDir << ", enableImgBorder:" << enableImgBorder << ", savePath:" << savePath 
        << std::endl;

        std::vector<std::string> imageFiles = fdect::common::Utils::findFilesByExt(imgDir, ".bmp");
        if (reverse){
            sort(imageFiles.rbegin(), imageFiles.rend());
        }else{
            sort(imageFiles.begin(), imageFiles.end());
        }
        fdect::algorithm::ImageCombinerParam param;
        param.subImagePaths = imageFiles;
        param.enableImgBoarder = enableImgBorder;
        param.saveImagePath = savePath;
        param.direction = 1;
        fdect::algorithm::ImageCombiner combiner;
        fdect::common::ProcResult result = combiner.combineSubImages(param);
        std::cout << "处理结果: (" << result.StatusCode << ", " << result.StatusDetail << ")" << std::endl;

        return 0;
    }catch( Exception e){
        std::cerr << "发生异常. 异常信息:" << e.what() << std::endl;
        return -1;
    }
}

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 好的,我来给您解释一下如何使用 C 语言完成 BMP 格式的两幅图像拼接。 首先,需要了解 BMP 格式的基本结构。BMP 文件由文件头和图像数据组成。文件头包含了图像的基本信息,如图像的宽度、高度、颜色深度等。图像数据则包含了每个像素的颜色信息。 下面是一个基本的步骤,帮助您完成 BMP 格式的两幅图像拼接: 1. 读取两幅图像的文件头和图像数据。 2. 创建一个新的 BMP 文件,并写入新的文件头。新文件的宽度应该是两幅图像的宽度之和,高度应该是两幅图像中较高的一幅。 3. 将第一幅图像图像数据写入新文件中。 4. 将第二幅图像图像数据写入新文件中,注意在写入时需要指定偏移量,使得图像数据写入到正确的位置。 5. 关闭文件并保存。 请注意,这只是一个基本的步骤,您可能还需要考虑一些细节,如文件读写错误处理、图像数据压缩等。 希望这些信息能帮助您。 ### 回答2: 要用C语言完成BMP格式两幅图像拼接,需要以下步骤: 1. 首先,我们需要了解BMP文件的结构。BMP文件由文件头、信息头和像素数据组成。文件头包含文件类型、文件大小等信息,信息头包含图像宽度、高度等信息,而像素数据则包含图像的实际像素信息。 2. 首先,我们需要使用C语言的文件操作函数来读取两幅BMP图像的文件头和信息头,并验证它们的格式是否符合BMP标准。 3. 接下来,我们需要根据图像的宽度和高度来计算每幅图像的像素数据大小,并通过动态内存分配函数来分配足够的内存空间。 4. 然后,我们需要使用文件操作函数将两幅图像的像素数据读取到内存中的相应位置。 5. 接着,我们需要将两幅图像的像素数据拼接到一起。这可以通过使用循环语句来遍历每个像素点,并将其复制到拼接后的像素数据中。 6. 最后,我们需要使用文件操作函数来创建一个新的BMP文件,并将拼接后的图像的文件头、信息头以及像素数据写入到该文件中。 需要注意的是,在拼接像素数据时,可能会遇到图像宽度对齐的问题。BMP图像的每行像素数据长度必须是4的倍数,因此在拼接时,需要根据宽度进行调整。 综上所述,使用C语言完成BMP格式两幅图像拼接需要理解BMP文件的结构,利用文件操作函数读取和写入图像数据,使用动态内存分配函数分配内存空间,并通过循环遍历进行像素数据的拼接。 ### 回答3: 在C语言中完成BMP格式两幅图像拼接可以通过以下步骤实现: 1. 导入相关的头文件和库文件,如stdio.h、stdlib.h、string.h等,以及用于操作BMP文件的自定义函数所在的头文件。 2. 读取两幅BMP图像文件的数据。使用自定义的函数读取BMP文件头信息,获取图像的宽度、高度等参数,并分配内存空间用于存储图像的像素数据。 3. 读取每个图像的像素数据。使用自定义的函数读取BMP文件的像素数据,并将其存储到相应的内存空间中。 4. 创建一个新的BMP图像文件的数据结构。生成一个新的BMP文件的文件头,并设置相应的参数,如宽度、高度等。 5. 合并两幅图像的像素数据。可以使用循环结构将两幅图像的像素数据进行合并,根据需求可以选择横向纵向拼接,将两幅图像的像素逐个复制到新的BMP图像中。 6. 将合并后的图像数据写入BMP文件。使用自定义的函数将新的BMP图像的文件头和像素数据写入到一个新的BMP文件中。 7. 释放之前分配的内存空间。使用自定义的函数释放之前分配的存储图像像素数据的内存空间。 8. 完成图像拼接。新的BMP图像已经生成,可以进行后续的操作,如保存、显示或进一步处理。 需要注意的是,在具体实现中,需要根据BMP文件的格式和特点进行相应的处理,如按行或按列读取像素数据、考虑对齐字节等。同时,要保证两幅图像的像素数据能够完全拼接,需要确保两幅图像的宽度或高度相等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老狼IT工作室

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值