-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLABScratches.m
More file actions
66 lines (60 loc) · 2.4 KB
/
Copy pathgetLABScratches.m
File metadata and controls
66 lines (60 loc) · 2.4 KB
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
function idx = getLABScratches(rgb,lightness_bound,green_to_red_bound,blue_to_yellow_bound,show_pic)
% GETLABSCRATCHES Detect scratches in RGB image (rgb) from specified
% thresholds by converting to LAB color space.
%
% INPUTS
% rgb: RGB image to analyze
% lightness_bound: interval of brightness level of the image to be
% analyzed (values between 0 to 100)
% green_to_red_bound: interval of degree of red-to-green of the image
% to be analyzed (values between -100 to 100)
% blue_to_yellow_bound: interval of blue-to-yellow of the image
% to be analyzed (values between -100 to 100)
% show_pic: boolean representing if to display the (enhanced) images
% and the detected scratches or not
% RETURNS
% idx: binary matrix of the same size as the original image where 1
% represents a scratch and 0 represents no scratch
% Convert RGB image to LAB image
cform = makecform('srgb2lab');
lab = applycform(rgb, cform);
% Enhance contrast of the image using histogram equalization
lab = histeq(lab);
lightness = lab(:,:,1);
green_to_red = lab(:,:,2);
blue_to_yellow = lab(:,:,3);
% Create binary matrices for each layer where the intensity is in
% the corresponding specified interval from input lightness_bound,
% green_to_red_bound and blue_to_yellow_bound. The binary matrices are
% called l, a and b.
l1 = lightness>=lightness_bound(1);
l2 = lab(:,:,1)<=lightness_bound(2);
l = logical(l1.*l2);
a1 = green_to_red>=green_to_red_bound(1);
a2 = green_to_red<=green_to_red_bound(2);
a = logical(a1.*a2);
b1 = blue_to_yellow>=blue_to_yellow_bound(1);
b2 = blue_to_yellow<=blue_to_yellow_bound(2);
b = logical(b1.*b2);
% Create a binary matrix where the intensity is in the threshold
% interval in all three layers
idx = logical(l.*a.*b);
% If input show_pic is true: display each enhanced image lightness,
% green_to_red and blue_to_yellow and corresponding binary matrix (l, a,
% b) where scratches have been detected
if show_pic
figure,clf
subplot(2,3,4)
imshow(lightness)
subplot(2,3,5)
imshow(green_to_red)
subplot(2,3,6)
imshow(blue_to_yellow)
subplot(2,3,1)
imshow(l,[0 1])
subplot(2,3,2)
imshow(a,[0,1])
subplot(2,3,3)
imshow(b,[0,1])
end
end