matlab遍历文件夹中的图片并保存在指定的文件夹中

本文详细介绍了一种基于Reinhard的色彩转移算法实现过程,通过对比源图片和目标图片的色彩特性,利用MATLAB代码实现色彩风格迁移。文章深入解析了cf_reinhard函数的原理,包括色彩空间转换、统计对齐等关键步骤。

以color transfer为例
源图片:
在这里插入图片描述

参考图片:
在这里插入图片描述

结果图片:
在这里插入图片描述

循环遍历文件夹的代码(参考):

image_dir  = 'C:\Users\Desktop\reinhard_color_transfer\m\building\';%原始图片路径
image_result_dir = 'C:\Users\Desktop\reinhard_color_transfer\m\building_blue\';  %结果图存储路径
I1 = im2double(imread('2.jpg'));  %参考图片
fpath = fullfile(image_dir, '*.jpg');
im_dir  = dir(fpath);
im_num = length(im_dir);
NIQE = [];
j=0;
k=0;
for i = 1:im_num
     IMname = regexp(im_dir(i).name, '\.', 'split');
     IMname = IMname{1};
     im=imread(fullfile(image_dir, im_dir(i).name));
     image_result = cf_reinhard(im,I1);
     [msize,nsize]=size(im);       
         k= k+1;
         image_name3=strcat(image_result_dir,num2str(k),'.jpg');
            imwrite(image_result,image_name3,'jpg');      %写图片      
end

为了方便更好地理解,现补充cf_reinhard代码:

function est_im = cf_reinhard(source,target)
%CF_REINHARD computes Reinhard's image colour transfer
%
%   CF_REINHARD(SOURCE,TARGET) returns the colour transfered source
%   image SOURCE according to the target image TARGET.
%

%   Copyright 2015 Han Gong <gong@fedoraproject.org>, University of East
%   Anglia.

%   References:
% Erik Reinhard, Michael Ashikhmin, Bruce Gooch and Peter Shirley, 
% 'Color Transfer between Images', IEEE CG&A special issue on Appliedi
% Perception, Vol 21, No 5, pp 34-41, September - October 2001

[x,y,z] = size(source);
img_s = reshape(im2double(source),[],3);
img_t = reshape(im2double(target),[],3);

a = [0.3811 0.5783 0.0402;0.1967 0.7244 0.0782;0.0241 0.1288 0.8444];
b = [1/sqrt(3) 0 0;0 1/sqrt(6) 0;0 0 1/sqrt(2)];
c = [1 1 1;1 1 -2;1 -1 0];
b2 = [sqrt(3)/3 0 0;0 sqrt(6)/6 0;0 0 sqrt(2)/2];
c2 = [1 1 1;1 1 -1;1 -2 0];

img_s = max(img_s,1/255);
img_t = max(img_t,1/255);

% convert to LMS space
LMS_s = a*img_s';
LMS_t = a*img_t';

% take the log of LMS
LMS_s = log10(LMS_s);
LMS_t = log10(LMS_t);

% convert to lab space
lab_s = b*c*LMS_s;
lab_t = b*c*LMS_t;

% compute mean and std
mean_s = mean(lab_s,2);
std_s = std(lab_s,0,2);
mean_t = mean(lab_t,2);
std_t = std(lab_t,0,2);

res_lab = zeros(3,x*y);

sf = std_t./std_s;

for ch = 1:3 % for each channel, apply the statistical alignment
    res_lab(ch,:) = (lab_s(ch,:) - mean_s(ch))*sf(ch) + mean_t(ch);
end

% convert back to LMS
LMS_res=c2*b2*res_lab;
for ch = 1:3
    LMS_res(ch,:) = 10.^LMS_res(ch,:);
end

% convert back to RGB
est_im = ([4.4679 -3.5873 0.1193;-1.2186 2.3809 -0.1624;0.0497 -0.2439 1.2045]*LMS_res)';
est_im = reshape(est_im,size(source)); % reshape the image

Matlab中,可借助`dir`函数实现遍历文件夹中文件的操作。该函数会返回一个结构体,其中存放着所有目录下文件的信息,通过遍历这个结构体就能访问所有文件。以下是几种不同场景下的实现方法: ### 遍历指定路径下的所有文件 ```matlab path = 'E:/matlabTest'; files = ls(path); ``` 此代码遍历`E:/matlabTest`路径下的所有文件,将文件信息存储在`files`变量里[^1]。 ### 遍历指定文件夹下的所有图片 #### 有文件夹图片指定文件夹下 ```matlab clc imgPath = 'E:\my_img\'; % 图像库路径 imgDir = dir([imgPath '*.png']); for m = 1:length(imgDir) b = imread([imgPath imgDir(m).name]); end ``` 这段代码遍历`E:\my_img`文件夹下的所有PNG图片读取每一张图片[^2]。 #### 无文件夹图片在当前路径的情况 ```matlab for m = 1:2 t = [num2str(m), '.jpg']; b = imread(t, 'jpg'); end ``` 该代码会读取当前路径下的`1.jpg`和`2.jpg`两张图片[^2]。 ### 遍历文件夹及其子目录下所有特定的CSV文件 ```matlab maindir = 'E:\database'; % 假设E中的database为总文件夹 subdir = dir(maindir); % 得到的为一个结构体文件,长度为文件夹项目长度加2(一个点和两个点也在其中) for i = 1:length(subdir) if (isequal(subdir(i).name, '.') || isequal(subdir(i).name, '..') || ~subdir(i).isdir()) continue; % 表示如果subdir表示的是当前目录,上层目录或者不是文件夹则跳过 end sub2dirpath = fullfile(maindir, subdir(i).name); sub2dir = dir(sub2dirpath); for j = 1:length(sub2dir) if (isequal(sub2dir(j).name, '.') || isequal(sub2dir(j).name, '..') || ~sub2dir(j).isdir()) continue; % 表示如果subdir表示的是当前目录,上层目录或者不是文件夹则跳过 end sub3dirpath = fullfile(maindir, subdir(i).name, sub2dir(j).name, '*.csv'); % 找出所有的csv文件 % 此处可以根据csv文件名将其中数据提取出来或做其他处理,或将csv文件路径分别保存起来均可 end end ``` 此代码使用层次遍历访问CSV文件,适用于小于5层子目录的情况。若子目录多于5层,需使用深度遍历[^4]。 ### 递归遍历文件夹下的所有文件 ```matlab function [ files ] = scanDir( root_dir ) files = {}; if root_dir(end) ~= '/' root_dir = [root_dir, '/']; end fileList = dir(root_dir); n = length(fileList); cntpic = 0; for i = 1:n if strcmp(fileList(i).name, '.') == 1 || strcmp(fileList(i).name, '..') == 1 continue; else if ~fileList(i).isdir full_name = [root_dir, fileList(i).name]; cntpic = cntpic + 1; files(cntpic) = {full_name}; else files = [files, scanDir([root_dir, fileList(i).name])]; end end end end ``` 这个函数可以递归地遍历指定文件夹及其子文件夹下的所有文件,将文件的完整路径存储在`files`单元格数组中[^5]。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值