-
Notifications
You must be signed in to change notification settings - Fork 1
/
CropImage.m
28 lines (24 loc) · 990 Bytes
/
CropImage.m
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
function CropImage(imageName, imageName2)
% Load a saved image, crops the whitespace and saves it again. Hardcore Unix users could alternatively use the crop command :)
if nargin < 2 || ~ischar(imageName2)
[filePart1, filePart2, filePart3] = fileparts(imageName);
imageName2 = [fullfile(filePart1, filePart2), '_Cropped', filePart3];
end
image = imread(imageName);
imageMod = image;
if max(imageMod(:)) <= 1
imageMod = 255*imageMod;
end
if ismatrix(imageMod)
imageMod = repmat(imageMod, 1, 1, 3);
end
imageSum = sum(imageMod, 3);
nonzero1 = find(sum(imageSum, 2) ~= size(imageSum,2)*255*3);
nonzero2 = find(sum(imageSum, 1) ~= size(imageSum,1)*255*3);
if ismatrix(image)
image = image(nonzero1(1):nonzero1(end), nonzero2(1):nonzero2(end));
else
image = image(nonzero1(1):nonzero1(end), nonzero2(1):nonzero2(end), :);
end
imwrite(image, imageName2);
end