-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn_lighting_compensation.m
61 lines (52 loc) · 2.15 KB
/
fn_lighting_compensation.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
function bw_img = fn_lighting_compensation(img)
%FN_LIGHTING_COMPENSATION Takes in an rgb image of an equation and
% returns a binarized version of the image for which uneven lighting has
% been compensated. Input image is assumed to be black text on white
% background. Output will not be inverted.
% Check if image needs lighting compensation by checking to see what
% proportion of pixels are in mid grayscale values
gray_img=rgb2gray(img);
[height, width] = size(img(:,:,1));
num_mid_gray = sum(sum(gray_img<240 & gray_img >15));
if num_mid_gray < 0.1*height*width % Should just be a scanned image
%% Perform Otsu's Method
thresh = graythresh(gray_img);
bw_img = im2bw(gray_img,thresh);
else
%% Perform lighting compensation
%% Blur if the image is large.
if height*width < 2000*1000
blur_flag = false;
else
blur_flag = true;
end
if blur_flag
fg = fspecial('gaussian', 10, 3);
gray_img = imfilter(gray_img, fg, 'conv','replicate');
end
%% Perform adaptive thresholding through a mean filter heuristic
% Set the window size of the filter based on image dimensions
win_size = round(min(height/60,width/60));
% Get the window mean of each pixel by filtering using an averaging filter
window_means=imfilter(gray_img,fspecial('average',win_size),'replicate');
% Remove the mean and threshold. Also inverts the image.
demeaned=window_means-gray_img-10;
bw_img=im2bw(demeaned,0); % positive values were above the threshold,
% negative values were below.
%% Remove small noise pixels.
noise_size = round(0.0001*height*width);
bw_img = bwareaopen(bw_img, noise_size);
%% Close gaps in edges
se = strel('square',4);
bw_img = imclose(bw_img,se);
%% Fill small holes (less than 5% of area of image)
small_hole_thresh = round(0.0001*height*width);
filled = imfill(bw_img,'holes');
holes = filled & ~bw_img;
lg_holes = bwareaopen(holes,small_hole_thresh);
sm_holes = holes &~lg_holes;
bw_img = bw_img | sm_holes;
%% Return image to original polarity.
bw_img = ~bw_img;
end
end