C++ 批量图片拼接

本文介绍了如何使用C++编程语言结合OpenCV库,进行批量图片拼接的操作。通过示例展示了对merge1.jpg、merge2.jpg和merge3.jpg等图片进行拼接的具体步骤和代码实现。
摘要由CSDN通过智能技术生成

C++ 批量图片拼接

/**函数功能:不同图片拼接
 *     参数:
 *          vector<string> pic_list      : 图片名称列表
 *          int pic_cols_rows           : horizontal==true,pic_cols_rows为生成图片的行数
                                           horizontal==false,pic_cols_rows为生成图片的列数
 *          bool horizontal             : true-先横向后纵向合成图片  false-先纵向后横向合成图片
 *          bool draw_rect              :  true-在图片边缘画矩形框 false-不在图片边缘画矩形框
 *
 */
void mergeDiffPic(vector<string> pic_list, int pic_cols_rows, string output_file, bool horizontal=true, bool draw_rect=false)
{
   
    int pic_cols = 0;
    int pic_rows = 0;
    int max_cols=0;
    int max_rows=0;
    int size_cols=0;
    int size_rows=0;
    vector<int> tmp_cols;
    vector<int> tmp_rows;
    //获取图片数量
    int pic_num = pic_list.size();
    vector<Mat>input(pic_num);
    Mat merge;
    for(int i=0; i<pic_num; i++){
   
        input[i] = imread(pic_list[i]);
        //draw_rect为true,画矩形
        if(draw_rect){
   
            Rect rect = Rect(0,0,input[i].cols,input[i].rows);
            rectangle(input[i],rect,Scalar(0, 0, 255));
        }
    }
    //按水平方向合成
    if (horizontal){
   
        pic_cols = pic_cols_rows;
        pic_rows = pic_num/pic_cols;
        //生成的图片行数
        if (pic_num%pic_cols != 0) pic_rows +
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是使用C语言批量复制tif图片并重命名保存到新的文件夹下的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char source_folder[100] = "./source/"; // 源文件夹路径,需要复制的tif图片所在文件夹 char target_folder[100] = "./target/"; // 目标文件夹路径,复制后的tif图片将保存在该文件夹下 char filename[100]; // 文件名 char new_filename[100]; // 新的文件名 int count = 1; // 文件计数器 // 打开源文件夹 DIR *dir; struct dirent *ent; if ((dir = opendir(source_folder)) != NULL) { // 循环遍历源文件夹中的所有文件 while ((ent = readdir(dir)) != NULL) { // 判断文件是否为tif图片 if (strstr(ent->d_name, ".tif") != NULL) { // 拼接文件路径 strcpy(filename, source_folder); strcat(filename, ent->d_name); // 打开源文件 FILE *source_file = fopen(filename, "rb"); if (source_file == NULL) { printf("Failed to open source file: %s\n", filename); continue; } // 构造新的文件名 sprintf(new_filename, "%s%d.tif", target_folder, count++); // 打开目标文件 FILE *target_file = fopen(new_filename, "wb"); if (target_file == NULL) { printf("Failed to create target file: %s\n", new_filename); fclose(source_file); continue; } // 复制文件内容 int c; while ((c = fgetc(source_file)) != EOF) { fputc(c, target_file); } // 关闭文件 fclose(source_file); fclose(target_file); } } closedir(dir); } else { printf("Failed to open source folder: %s\n", source_folder); return 1; } printf("Finished copying %d files.\n", count - 1); return 0; } ``` 在代码中,我们首先定义了源文件夹路径和目标文件夹路径,然后使用`opendir()`函数打开源文件夹,并循环遍历文件夹中的所有文件。对于每个文件,我们判断其是否为tif图片,然后打开源文件和目标文件,复制文件内容并保存到目标文件中,最后关闭文件。在复制文件时,我们使用了`fgetc()`和`fputc()`函数分别读取和写入文件内容。 需要注意的是,我们在复制文件时使用了计数器来生成新的文件名,如果源文件夹中存在同名的tif图片,则后面的文件会覆盖前面的文件。如果需要避免文件名冲突,可以使用其他的命名方式,例如在文件名中加入当前的时间戳等。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值