Monday, November 16, 2015

Matlab: Cropping Images

开贴介绍Matlab祛除图片空白边缘的代码。
原图片见Fig. 1,祛除空白边缘后的效果见Fig. 2。
Fig. 1
Fig. 2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
% Created by LI Xu
% 7 September, 2014
% Version 1.0

% Crop the Images

clear;
clc;


Images=dir('*.png');
n=size(Images, 1);

% Upleft point
upoint=[342, 60];
% Rect
rect=[2900, 2362];

for ii=1:n
    imname=Images(ii).name;
    RGB=imread(imname);
    res=imcrop(RGB, [upoint, rect]);
    nname=strsplit(imname, '.');
    nname=strcat(nname{1}, '_new.png');
    imwrite(res, nname)
end


disp('**************************');
第二版.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
% Created by LI Xu
% 7 September, 2014
% Version 1.0

% Modified by LI Xu
% April 12, 2016
% Version 1.5

% Crop the Images

clear;
clc;

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';
% All Input Files
files=dir(fullfile(SouDir, '*.png'));

% Upleft point
upoint=[342, 60];
% Rect
rect=[2900, 2362];

for ii=1:length(files)
    filename=files(ii).name;
    filepath=fullfile(SouDir, filename);
    RGB=imread(filepath);
    res=imcrop(RGB, [upoint, rect]);
    
    % Output 
    otpath=fullfile(DesDir, filename);
    imwrite(res, otpath)
    disp([num2str(ii), ': ', filename]);
end


disp('**************************');

No comments:

Post a Comment