forked from bladonjay/Jadhav-Lab-Codes-JHB
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoveShadows.m
145 lines (105 loc) · 3.14 KB
/
RemoveShadows.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
%% scripting
[vidfile,viddir]=uigetfile();
mp4file=fullfile(viddir,vidfile);
vid=VideoReader(mp4file);
randofind=randi([100 vid.NumFrames-100]);
testframe=read(vid,randofind,'native');
AInv = imcomplement(im2double(testframe));
BInv = imreducehaze(AInv,.8); %, 'ContrastEnhancement','none');
B = imcomplement(BInv);
figure; montage({testframe,B});
figure;
B = imlocalbrighten(im2double(testframe),'AlphaBlend',true);
montage({testframe,B})
figure;
subplot(2,2,1);
image(B);
B2=B;
B2(:,:,1)=imadjust(B2(:,:,1));
subplot(2,2,2); image(B2);
B2(:,:,2)=imadjust(B2(:,:,2));
subplot(2,2,3); image(B2);
B2(:,:,3)=imadjust(B2(:,:,3));
subplot(2,2,4); image(B2);
% smooth this out a bit?
B2=imgaussfilt(B,2);
figure; montage({B,B2});
PSF = fspecial('motion',2,2);
Idouble = im2double(B);
imshow(Idouble)
title('Blurred Image')
wnr1 = deconvwnr(Idouble,PSF,.1);
imshow(wnr1)
title('Restored Blurred Image')
%{
% this doesnt really work, its fine for greyscale though
shadow_lab = rgb2lab(testframe);
max_luminosity = 100;
L = shadow_lab(:,:,1)/max_luminosity;
shadow_imadjust = shadow_lab;
shadow_imadjust(:,:,1) = imadjust(L)*max_luminosity;
shadow_imadjust = lab2rgb(shadow_imadjust);
shadow_histeq = shadow_lab;
shadow_histeq(:,:,1) = histeq(L)*max_luminosity;
shadow_histeq = lab2rgb(shadow_histeq);
shadow_adapthisteq = shadow_lab;
shadow_adapthisteq(:,:,1) = adapthisteq(L)*max_luminosity;
shadow_adapthisteq = lab2rgb(shadow_adapthisteq);
%}
figure
montage({testframe,shadow_imadjust,shadow_histeq,shadow_adapthisteq, B},'Size',[1 5])
title("Original Image and Enhanced Images using imadjust, histeq, adapthisteq, and reducehaze")
%%
% TAKES FOREVER, NOT USEFUL
%[vidfile,viddir]=uigetfile();
ratdir=uigetdir();
vidfiles=getAllFiles(ratdir,'.mp4');
for i=1:length(vidfiles)
%mp4file=fullfile(viddir,vidfile);
mp4file=vidfiles{i};
vid=VideoReader(mp4file);
[a,b,c]=fileparts(fullfile(vid.Path,vid.Name));
outavifile=fullfile(a,[b '-bright']);
ovid = VideoWriter(outavifile);
%ovid = VideoWriter(outavifile,'MPEG-4');
open(ovid)
%c1 = onCleanup(@() close(ovid));
% build waitbar
h = waitbar(0,sprintf('Starting %s Please wait... (%02.0f%%)',b,0));
%c2 = onCleanup(@(x,y) close(h));
updateintv = max(floor(vid.NumFrames/100),1);
%%
% go frame by frame
for ii = 1:vid.NumFrames
% Read the frame
frame = gpuArray(read(vid,ii));
% Apply image filter
newim=nan(size(frame));
for k=1:3
linim=double(linearize(frame(:,:,k)));
immean=mean(linim); imdev=std(linim);
imcutoff=immean+3*imdev;
newim(:,:,k)=imadjust(frame(:,:,k),[0 imcutoff/256]);
end
writeVideo(ovid,uint8(gather(newim)));
if(mod(ii,updateintv)==0)
if(ishandle(h))
waitbar(ii/vid.NumFrames,h,sprintf('Processing %s Please wait... (%02.0f%%)',b,ii*100/vid.NumFrames));
end
end
end
close(ovid);
close(h);
end
%%
myim=gpuArray(frame);
figure;
subplot(1,2,1); image(frame);
newim=nan(size(frame));
for k=1:3
linim=double(linearize(myim(:,:,k)));
immean=mean(linim); imdev=std(linim);
imcutoff=immean+3*imdev;
newim(:,:,k)=imadjust(myim(:,:,k),[0 imcutoff/256]);
end
subplot(1,2,2); image(uint8(newim));