-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniios_cpts_preprocess_v2_pro.m
283 lines (235 loc) · 7.9 KB
/
niios_cpts_preprocess_v2_pro.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
%close all;
clear;
clc;
q = 0; % being cut
k = 0; % less than 256
j = 0; % recheck the actual img_list size
%Table with images analyzability for filtering out the unwanted images
analyzable_data = readtable('');
%Folder Names (change these)
img_folder = '';
lbl_folder = '';
final_img_folder = '';
final_lbl_folder = '';
mkdir(final_img_folder); mkdir(final_lbl_folder);
direc = dir(img_folder);
img_list = extractfield(direc(3:end), 'name')';
Only preprocess the analyzable images
temp = analyzable_data.Filename; %copy the original list
C = strcmp('analyzable', analyzable_data.Analyzability);
x = find(C == 0);
temp(x) = [];
for i= 1:length(img_list)
if ismember(img_list(i),temp) == 1
i = i + 1;
else img_list(i) = [];
end
end
for f = 1:length(img_list)
%Load the image and corresponding label, conver the image from uint8 to
%double type
filename = img_list{f};
filesplit = split(filename, '.');
filetype = filesplit{2};
disp(img_folder);
img = imread([img_folder, filename]);
img = img(:,:,1);
img_double = im2double(img);% added,dont know the diff between img_double and new_img
lbl = imread([lbl_folder, filename]);
if strcmp(filetype, 'jpg')
lbl(lbl < 100) = 0;
lbl(lbl > 100) = 1;
end
new_img = double(img);
% The following crops the dark and bright columns and rows
%line 56 should be here
[crop_img, crop_lbl, crop_array(f, 1:4)] = crop_march(new_img, lbl);%changed the sequence
if isempty(crop_img)
q = q+1;
crop_array(f, 1:4) = [0, 0, 0, 0];
continue
end
[m,n]=size(crop_img);
if m < 256 || n <256
k = k+1 ;
disp(img_list{f});
img_list{f} = '';
else
[readytocentrecrop,lbl] = shenhejiancha(crop_img,crop_lbl);
%The following will crop the center of images to the size [256,
%256],wont use
win = centerCropWindow2d(size(readytocentrecrop),[256,256]);
crop_img = imcrop(readytocentrecrop, win); %%%%
crop_lbl = imcrop(lbl, win);%%% change from lbl to crop_lbl to new lbl
crop_lbl = [ones(size(crop_lbl, 1), 1), crop_lbl(:, 2:end-1), ones(size(crop_lbl, 1), 1)];
crop_lbl = [ones(1, size(crop_lbl, 2)); crop_lbl(2:end-1, :); ones(1, size(crop_lbl, 2))];
%The following is to remove the varying illumination from all images
[adjust_img] = remove_gradient(crop_img);
[adjust_img] = anyingmeihua(adjust_img);
%Saving the Images and Labels
imwrite(adjust_img, [final_img_folder, filename(1:end-4), '.bmp']);
imwrite(255*crop_lbl, [final_lbl_folder, filename(1:end-4), '.bmp']);
j = j + 1;
end
end
disp('number of the image that is cropped of dark and bright columns : ')
disp(1029 - q);
disp('the number of picture which is less than 256 after being cropped:')
disp(k);
disp('number of picture after screening in the img_list')
disp(j);
%This part identifies the bright regions of the image
BW = imregionalmax(img_double, 8);
J = imreconstruct(220*BW, img_double);
difimg = img_double - J;
% figure; imshow(difimg, []);
%The following uses morphological operations to segment the bright
%region and identify which rows make up this bright area
edge_img = edge(difimg, 'Sobel');
dil_img = imdilate(edge_img, strel('disk', 3));
pad_img = padarray(dil_img, [1, 0], 1, 'pre');
fil_img = imfill(pad_img, 'holes');
fil_img = fil_img(2:end, :);
[rows, ~] = find(fil_img == 1);
unique_rows = unique(rows);
%This for loop is for filling the pixels with the average mean of the pixels in the same row
%Probably should consider just cropping this part....
for r = 1:length(unique_rows)
fil_col = find(fil_img(unique_rows(r), :) == 1);
fil_col = [fil_col, max(fil_col) + [1:17]];
if max(fil_col)+length(fil_col) > size(img_double, 2)
new_col = ones(1, length(fil_col))*mean(img_double(unique_rows(r), max(fil_col)+1:size(img_double,2)),'all');
else
new_col = ones(1, length(fil_col))*mean(img_double(unique_rows(r), max(fil_col)+1:max(fil_col)+length(fil_col)),'all');
end
new_img(unique_rows(r), fil_col) = new_col;
end
en
function [crop_img, crop_lbl, crop_array] = crop_march(new_img, lbl)
sum_cols = sum(new_img, 1);
sum_rows = sum(new_img, 2);
thresh_dark = 0.22*mean(sum_cols);
thresh_bright = 110000;
cols_left = 1;
for c = 1:length(sum_cols)
if sum_cols(c) <= thresh_dark || sum_cols(c) >= thresh_bright
cols_left = c;
elseif c - cols_left == 25
break
end
end
cols_right = length(sum_cols);
for c = length(sum_cols):-1:1
if sum_cols(c) <= thresh_dark || sum_cols(c) >= thresh_bright
cols_right = c;
elseif cols_right - c == 25
break
end
end
thresh_dark = 0.22*mean(sum_rows);
thresh_bright = 110000;
rows_top = 1;
for r = 1:length(sum_rows)
if sum_rows(r) <= thresh_dark || sum_rows(r) >= thresh_bright
rows_top = r;
elseif r - rows_top == 25
break
end
end
rows_bot = length(sum_rows);
for r = length(sum_rows):-1:1
if sum_rows(r) <= thresh_dark || sum_rows(r) >= thresh_bright
rows_bot = r;
elseif rows_bot - r == 25
break
end
end
crop_img = new_img(rows_top + 5:rows_bot - 5, cols_left + 5:cols_right - 5);
crop_lbl = lbl(rows_top + 5:rows_bot - 5, cols_left + 5:cols_right - 5);
crop_lbl = [ones(size(crop_lbl, 1), 1), crop_lbl(:, 2:end-1), ones(size(crop_lbl, 1), 1)];
crop_lbl = [ones(1, size(crop_lbl, 2)); crop_lbl(2:end-1, :); ones(1, size(crop_lbl, 2))];
crop_array = [rows_top + 5, rows_bot - 5, cols_left + 5, cols_right - 5];
end
function [adjust_img] = remove_gradient(crop_img)
% New Method
imgGS1 = imgaussfilt(crop_img, 21, 'FilterSize', [65, 65]);
imgsub = im2double(crop_img)-imgGS1;
imgsub2 = imgsub - min(imgsub(:));
subnorm8 = im2uint8(255*imgsub2./max(255*imgsub2(:)));
adjust_img = imadjust(subnorm8);
% Gimp method
% % img_blur = imgaussfilt(crop_img, 85/3, 'FilterSize', [85, 85]);
% % img_inv = imcomplement(img_blur);
% % C = imfuse(img_inv, crop_img, 'blend');
% % C_stretch = imadjust(C, stretchlim(C));
% % adjust_img = imadjust(C_stretch, [0.117, 1]);
end
function [adjust_img] = anyingmeihua(file)
global omit;
div = 0;%deviate
m = 0;
oo=file;%copy a copy
I = imadjust(file,[0 1],[0.3 0.7]);
[h,w] = size(I);
I2 = uint8(I);
for i=1:h
for j=1:w
m = m + double(I(i,j));
end
end
m = (m)/(h*w);
disp('m:');
disp(m);
for i=1:h
for j=1:w
%disp((double(I(i,j))));
div= div + abs(double(I(i,j))-m)*abs(double(I(i,j))-m);
%disp(m);
end
end
div = div^0.5;
disp('diviate');
disp(div);
for i=1:h
for j=1:w
if I(i,j)> m
I2(i,j)=255;
end
if I(i,j) < m+2
I2(i,j)=30;
end
end
end
adjust_img = imadd(I2,I);
adjust_img = imadd(adjust_img*0.25,oo);
end
function [readytocentrecrop,lbl] = shenhejiancha(file,label)
m = 0;
div = 0;
copy = file;
temp = centerCropWindow2d(size(copy),[256,256]);
crop_temp = imcrop(copy, temp);
[crop_temp] = remove_gradient(crop_temp);
I = imadjust(crop_temp,[0 1],[0.3 0.7]);
[h,w] = size(I);
for i=1:h
for j=1:w
m = m + double(I(i,j));
end
end
m = (m)/(h*w);
for i=1:h
for j=1:w
div= div + abs(double(I(i,j))-m)*abs(double(I(i,j))-m);
end
end
div = div^0.5;
if div > 4.1e+03
readytocentrecrop = file;
lbl = label;
else
[m,n] = size(file);
readytocentrecrop = imcrop(file,[20,0,n,m]);
lbl = imcrop(label,[20,0,n,m]);
end
end