从不同名称(所有bmp)的相同文件夹上载不同的图片并进行处理

时间:2014-09-20 15:36:50

标签: matlab

我想上传1个文件夹中的所有图片,这些图片名称不同,但都是bmp。我想在之后处理它们。如何上传它们并用1个命令处理它们?

文件夹目录为C:\Users\Gramoz\Desktop\Gramoz\140917,下面显示了我必须逐个处理它们的代码(对于具有图像名称的特定文件夹)。

我只需要1个文件夹的mutiupload和multiprocess命令,因为代码可能不清楚。

fname = '103_Y(50)_Z(150)_EXP(8.159).bmp';
im = imread(fname);
figure(13)
imagesc(im)

C=max(max(im));
s=max(max(C));
[ROW COLUMN]=find(im==C);

row1 = ROW-17;
row2 = ROW+17;
col1 = COLUMN-17;
col2 = COLUMN+17;

Etot_min_BG = ( sum(sum(im))-(sum(sum(im(1:300,1:300)))/90000)*1024*1280);
Efoc = sum(sum(im(row1:row2,col1:col2)));
Efoc_min_BG = sum(sum(im(row1:row2,col1:col2))) - sum(sum(im(1:300,1:300)))/90000 * (row2-row1+1)*(col2-col1+1);


% =========================================================================
% =========================================================================


Nx = 1024; %Replace with real image dimensions 
Ny = 1280; %Replace with real image dimensions 
x0 = 451; %Replace with coordinate of MMF center
y0 = 644; %Replace with coordinate of MMF center
x = (1:Nx) - x0;
y = (1:Ny) - y0;
[X,Y] = meshgrid(x,y);
X = X';
Y = Y';

% im = (X.^2+Y.^2).*exp(-(X.^2+Y.^2)/50); %Comment out this line, 
temp = imread(fname); %Uncomment this line, write real file name
h = 1/81*ones(9);
im = filter2(h,temp);
% im = zeros(64,80);
% for k = 1:128
%     for m = 1:160
%         im(k,m) = sum(sum(temp((1+(k-1)*8):(k*8) , (1+(m-1)*8):(m*8))));
%     end
% end


[temp1 temp2] = cart2pol(X, Y);

r = reshape(temp2, Nx*Ny,1);

theta = reshape(temp1, Nx*Ny,1);

impol = reshape(im, Nx*Ny,1);
% =========================================================================
% =========================================================================

% figure;
% imagesc(im);colorbar

% figure; 
% plot1=plot(r, impol, 'k.');
% xlabel('r [pix]');
% ylabel('Image value');
% set(gca, 'XLim', [0 350]);


% =========================================================================
% =========================================================================

% figure; 
% plot(theta, impol, 'k.');
% xlabel('theta [rad]');
% ylabel('Image value');

% =========================================================================
% =========================================================================


%=====bin data, r
numbins = 128;
% 
rbins = linspace(min(r), max(r), numbins);
[n rbin] = histc(r, rbins);
mu_r = full(mean(sparse(1:length(r),rbin,double(impol))));
figure;
plot(rbins, mu_r./rbins); %We divide by rbins to get the density in polar coordinates
xlabel('r_{bin} [pix]');
ylabel('Density');

% =========================================================================
% =========================================================================


%=====bin data, theta
thetabins = linspace(min(theta), max(theta), numbins);
[n thetabin] = histc(theta, thetabins);
mu_theta = full(mean(sparse(1:length(theta),thetabin,double(impol))));
% figure;
% plot(thetabins, mu_theta);
% xlabel('\theta_{bin} [rad]');
% ylabel('Density');


%  [Peakvalue Peakposition]=max(impol)]
%  r(Peakposition)
%  theta(Peakposition)
%  

% =========================================================================
% =========================================================================


{'  File name ','Peak value','Row of peak','Column of Peak','Etot_min_BG','Efoc','foc_min_BG';
  20 ,s,ROW,COLUMN,  Etot_min_BG  ,  Efoc  ,  Efoc_min_BG  }

% to find  r(max)
% plot(r,impol) ;
[M1 M2]=max(impol);
r=r(M2)

% to find theta(max)
% plot(theta,impol) ;
[M1 M2]=max(impol);
theta=theta(M2)

[M3 M4]=max(mu_r./rbins);
%  to fidn rdensity
% plot(rbins,mu_r./rbins) ; 
[M3 M4]=max(mu_r./rbins);
rdens=rbins(M4)

[M5 M6]=max(mu_theta);

% plot(thetabins, mu_theta);
[M5 M6]=max(mu_theta);
thetadens=thetabins(M6)

F=[ Etot_min_BG  Efoc  Efoc_min_BG r theta rdens thetadens ]

1 个答案:

答案 0 :(得分:1)

我只是根据 rayryeng 评论总结答案。 因为代码的唯一区别应该是图像,就像上面说的那样,你只需要通过dir函数获取文件的名称,然后遍历不同的文件。

因此你应该有这样的东西:

images_names=dir(fullfile(images_path,'*.bmp'))
for i=1:length(images_names)
im = imread(fullfile(images_path,images_names(i).name));

%%% do your stuff here %%%%%%%%%%%%

%% if you need to save all in the same variable, and dont care how
save_F{i}=F; %%% like your F variable

%%%%%%%%%%%%%%%
end