opencv 与matlab 批量读取并保存图片

本文介绍了如何在opencv中批量读取和处理图片,解决头文件问题,并提供了matlab实现批量处理图片的简单方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

opencv

  • 首先添加必要的头文件(用于批量处理):
#include <dirent.h>

这里可能会报错,找不到头文件,可以去网上下载,或者点击下面的链接
下载
下载后找到dirent.h文件,复制到…(对应你的VS目录)…\VC\include\

  • 接下来,批量读取并处理图片:
        DIR *dp;
        struct dirent *dirp;
        vector <std::string> filename;
        if ((dp = (opendir(filein.c_str()))) == NULL) {
            perror("open dir error");
            return -1;
        }

        while ((dirp = readdir(dp)) != NULL) {
            filename.push_back(filein+dirp->d_name);
        }

        for (int i = 0; i<filename.size(); i++) {
            cout << filename[i] << endl;
            Mat img = imread(filename[i], CV_LOAD_IMAGE_COLOR);   // Read the file
            //图像处理
            string temp = filename[i];
            size_t pos = temp.find("images");//我这里是把文件放在images里边,然后希望把处理后文件存入与images同目录下的results里边
            fileout = temp.replace(pos, 6, "results");
            imwrite(fileout, img);

        }
        closedir(dp);

matlab

matlab比较简单,不用添加特定头文件直接上代码就好

Input_file_path =  '.\images\';% input image file path
Output_file_path='.\result\';

img_path_list = dir(strcat(Input_file_path,'*.jpg'));
img_num = length(img_path_list);

if img_num > 0 
        for j = 1:img_num 
            image_name = img_path_list(j).name;
            image_name = strcat(Input_file_path,image_name);  
             %图像处理过程
            imwrite(image,strcat(Output_file_path,image_name));
        end  
end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值