-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveWhiteSpace.m
158 lines (135 loc) · 4.24 KB
/
RemoveWhiteSpace.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function u_out = RemoveWhiteSpace(u_in, varargin)
% © February 2nd, 2012, By Reza Farrahi Moghaddam, Synchromedia Lab, ETS, Montreal, Canada
%
% RemoveWhiteSpace function removes white spaces around an image.
%
% Syntax:
% 1. For an image: u_out = RemoveWhiteSpace(u_in)
%
% 2. For an image file, to write the result on the same file: RemoveWhiteSpace([], 'file', input_filename)
%
% 3. For an image file, to make a new output file: RemoveWhiteSpace([], 'file', input_filename, 'output', output_filename)
%
% get the arguments
[it_is_a_file_flag, input_filename, output_filename] = check_the_argin_infile(nargin, varargin{:});
%
if (it_is_a_file_flag)
[u_in, map] = imread(input_filename);
if (numel(map) ~= 0)
u_in = ind2rgb(u_in, map);
end
end
u_in = mat2gray_infile(u_in);
[xm ym zm] = size(u_in);
%
if (zm == 3)
% u_gray = rgb2gray(u_in);
u_gray = mean(u_in, 3);
else
u_gray = mean(u_in, 3);
end
%
u_white_mask = u_gray > 0.99;
u_white_mask_hori = reshape(sum(u_white_mask, 1), [], 1);
u_white_mask_vert = sum(u_white_mask, 2);
%
u_white_mask_hori(u_white_mask_hori < xm / 2) = 0;
u_white_mask_vert(u_white_mask_vert < ym / 2) = 0;
u_white_mask_hori_diff = diff(u_white_mask_hori);
u_white_mask_vert_diff = diff(u_white_mask_vert);
[~, boundingbox_hori] = findpeaks(abs(u_white_mask_hori_diff));
[~, boundingbox_vert] = findpeaks(abs(diff(u_white_mask_vert)));
if (numel(boundingbox_hori) == 0)
boundingbox_hori = [0 ym];
elseif (numel(boundingbox_hori) == 1)
if (boundingbox_hori > ym / 2)
boundingbox_hori = [0, boundingbox_hori];
else
boundingbox_hori = [boundingbox_hori, ym];
end
else
boundingbox_hori = boundingbox_hori([1, end]);
boundingbox_hori = boundingbox_hori .* [- u_white_mask_hori_diff(boundingbox_hori(1)) > 0; u_white_mask_hori_diff(boundingbox_hori(2)) > 0];
end
if (numel(boundingbox_vert) == 0)
boundingbox_vert = [0 xm];
elseif (numel(boundingbox_vert) == 1)
if (boundingbox_vert > xm / 2)
boundingbox_vert = [0, boundingbox_vert];
else
boundingbox_vert = [boundingbox_vert, xm];
end
else
boundingbox_vert = boundingbox_vert([1, end]);
boundingbox_vert = boundingbox_vert .* [- u_white_mask_vert_diff(boundingbox_vert(1)) > 0; u_white_mask_vert_diff(boundingbox_vert(2)) > 0];
end
boundingbox_hori(1) = boundingbox_hori(1) + 1;
boundingbox_vert(1) = boundingbox_vert(1) + 1;
if (boundingbox_hori(2) == 0)
boundingbox_hori(2) = ym;
end
if (boundingbox_vert(2) == 0)
boundingbox_vert(2) = xm;
end
%
u_out = u_in(boundingbox_vert(1) : boundingbox_vert(2), boundingbox_hori(1) : boundingbox_hori(2), :);
%
if (it_is_a_file_flag)
imwrite(u_out, output_filename);
end
end
function [it_is_a_file_flag, input_filename, output_filename] = check_the_argin_infile(nargin, varargin)
% 120202: Reza
% %
it_is_a_file_flag = false;
input_filename = '';
output_filename = '';
%
default_fields = {'file', 'output'};
for temp_label = 1 : 2 : (nargin - 1)
parameter_name = varargin{temp_label};
parameter_val = varargin{temp_label + 1};
matched_field = find(strcmpi(parameter_name, default_fields));
if isempty(matched_field)
error('Error: Unknown argument: %s.\n', parameter_name);
else
switch(matched_field)
case 1 % input_filename
input_filename = parameter_val;
output_filename = parameter_val;
it_is_a_file_flag = true;
case 2 % output_filename
output_filename = parameter_val;
it_is_a_file_flag = true;
end
end
end
%
if (numel(output_filename) > 0)&&(numel(input_filename) == 0)
input_filename = output_filename;
end
end
function u_in = mat2gray_infile(u_in)
% 120212: Reza
%
u_in = double(u_in);
u_in_min = min(u_in(:));
u_in_max = max(u_in(:));
%
if (u_in_max == u_in_min)
u_in(:) = 1;
else
u_in = (u_in - u_in_min) ./ (u_in_max - u_in_min);
end
end
%{
u_in = mat2gray(imread('test.png'));
subfigure(2, 2, [2 2]), imshow(u_in, 'InitialMagnification','fit');
subfigure(2, 2, [1 1]), imshow(RemoveWhiteSpace(u_in), 'InitialMagnification','fit');
%}
%{
RemoveWhiteSpace([], 'file', 'test.png', 'output', 'test_out.png');
%}
%{
RemoveWhiteSpace([], 'file', 'test.png');
%}