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

以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

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值