forked from fpeter71/OverExposureLSCICompensation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OverExposureCorrection.m
112 lines (94 loc) · 3.38 KB
/
OverExposureCorrection.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
function [K_raw, K_corrected, R_saturationratio] = OverExposureCorrection(filenames, N, I_saturation)
%OverExposureCorrection Correction of laser speckle contrast calculation of
%oversatured image sequences
% [K_raw, K_corrected, R_saturationratio] = OverExposureCorrection(filenames, N, I_saturation) sweeps all
% images found in the file list, calculates their speckle contrast of NxN
% neighborhood and average for the sequence. K_raw is the standard
% calulation, K_corrected is corrected for overexposure.
% R_saturationratio [0..1] is the ratio of saturated pixels in the sliding
% NxN window averaged for the sequence.
% The correction factors of the linear extrapolation are set for low
% contrast range.
%
% The image files should be of full path, single channel of type double,
% uint8 or uint16. Saturation is calculated by the given I_saturation (e.g.
% 8-bit images 255, double normalized 1.0, etc.)
%
% Example
% --------
% cls
% clear
% % collection of files with certain extension from a folder
% path = 'path to image sequence\';
% d = dir([path '*.tiff']); % change extension as needed
% for i = 1:length(d)
% name = d(i).name;
% filenames{i} = [path name];
% end
%
% % call the correction
% N = 11;
% I_saturation = 65500; % 255 for uint8, 2^16-1 for uint16, 1.0 for double. Adjust to actual camera maximum.
% [K_raw, K_corrected, R_saturationratio] = OverExposureCorrection(filenames, N, I_saturation);
%
% % show results
% figure
% subplot 131
% imagesc(1./K_raw.^2,[1 15]);
% title('Raw 1/contrast^2 map');
% colorbar
%
% subplot 132
% imagesc(100 * R_saturationratio);
% title('Saturation ratio [%]');
% colorbar
%
% subplot 133
% imagesc(1./K_corrected.^2,[1 15]);
% title('Corrected 1/contrast^2 map');
% colorbar
% colormap(parula)
% Copyright 2022 SZTAKI, INSTITUTE FOR COMPUTER SCIENCE AND CONTROL
% Peter Foldesy, Mate Siket, Adam Nagy, Imre Janoki, [email protected]
disp('Correction of overexposure in laser speckle contrast imaging');
% threshold values
I_saturation_0 = 1.0 * I_saturation;
% contrast maps
K_0 = 0;
% saturation ratios
R_0 = 0;
% calculating contrasts
disp('Calculating contrast maps');
ImageNumber = length(filenames);
for i = 1:ImageNumber
% fist interation
imagein = imread(filenames{round(i)});
imagein = double(imagein);
% artificial saturation and saturation ratio
R_0 = R_0 + conv2(imagein >= I_saturation_0, ones(N)/N/N,'same');
% contrast calculation with the given thresholds
imagein( imagein >= I_saturation_0 ) = I_saturation_0;
m = conv2(imagein, ones(N)/N/N,'same');
st = stdfilt(imagein,ones(N));
K_0 = K_0 + st./m;
end
% normalization for the sequence
R_0 = R_0 / ImageNumber;
K_0 = K_0 / ImageNumber;
disp('Correction iterations');
K_C = K_0 ./ (1-R_0 + eps);
c1 = -0.8;
q1 = -0.85;
q2 = 0.25;
K_C = K_C .* (1+c1*R_0)./(1+q1*R_0+q2*R_0.^2);
% merging with the orginal contrast map
K_C(isnan(K_C)) = K_0(isnan(K_C)); % elimianting possible inf, and nan errors
K_C(isinf(K_C)) = K_0(isnan(K_C));
% optional step to inpainting diverging values
K_C = regionfill(K_C, K_C < 0.01);
tobereplaced = double(R_0 > 0);
% return to kappa, contrast instead of kappa^2
K_corrected = tobereplaced.*K_C + (1-tobereplaced).*K_0;
K_raw = K_0;
R_saturationratio = R_0;
disp('Ready');