-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoisey-Images-Segmentation-Classification-Framework.py
More file actions
412 lines (343 loc) · 15.9 KB
/
Noisey-Images-Segmentation-Classification-Framework.py
File metadata and controls
412 lines (343 loc) · 15.9 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import numpy as np
import pandas as pd
import glob
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models
from sklearn.utils import compute_class_weight
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
import os
from tensorflow.keras.preprocessing.image import load_img, img_to_array, save_img
import matplotlib.pyplot as plt
from PIL import Image
from termcolor import colored
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.image import load_img, img_to_array, save_img
########################### Adding Noise to The Original Images in the validation set [Gaussian and Salt-and-Pepper Noise] ########################
###########################^^############################^^^^################################################^^^^^^^^^
######### Function to add Gaussian noise to an image#########
# CHnage sigma value as needed (5, 15, 25, 35)
def add_gaussian_noise(image, mean=0, sigma=25):
"""Add Gaussian noise to an image"""
gaussian_noise = np.random.normal(mean, sigma, image.shape)
noisy_image = image + gaussian_noise
noisy_image = np.clip(noisy_image, 0, 255) # Ensure pixel values are valid
return noisy_image
########## Function to add salt-and-pepper noise to an image #########
def add_salt_and_pepper_noise(image, salt_prob=0.05, pepper_prob=0.05):
"""Add salt and pepper noise to an image"""
noisy_image = np.copy(image)
total_pixels = image.size
# Add salt noise
num_salt = np.ceil(salt_prob * total_pixels)
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
noisy_image[coords] = 1
# Add pepper noise
num_pepper = np.ceil(pepper_prob * total_pixels)
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
noisy_image[coords] = 0
return noisy_image
########## Function to load an image#########
def load_image(image_path, target_size=(512, 512)):
"""Load a grayscale image"""
img = load_img(image_path, target_size=target_size, color_mode="grayscale")
img_array = img_to_array(img)
return img_array
########## Function to save an image#########
def save_image(image, save_path):
"""Save a grayscale image to the specified path in JPEG format"""
save_img(save_path, image, data_format='channels_last', file_format='jpeg')
# Path to validation set folder
#gaussian_output_path is used for saving images with Gaussian noise.
#salt_and_pepper_output_path is used for saving images with salt-and-pepper noise.
#The images with Gaussian noise will be saved in the val-gaussian-noise-added directory, and the images with salt-and-pepper noise will be saved in the val-salt-and-pepper-noise-added directory.
validation_set_path = '/home/idu/Desktop/COV19D/val'
gaussian_output_path = '/home/idu/Desktop/COV19D/val-gaussian-noise-added'
salt_and_pepper_output_path = '/home/idu/Desktop/COV19D/val-salt-and-pepper-noise-added'
# Function to add noise to images and save them
def add_noise_and_save_images(image_paths, output_path, validation_set_path, noise_type='gaussian', **kwargs):
for image_path in image_paths:
try:
# Determine output path for noisy image
relative_path = os.path.relpath(image_path, start=validation_set_path)
noisy_image_save_path = os.path.join(output_path, relative_path)
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(noisy_image_save_path), exist_ok=True)
# Load image
image = load_image(image_path)
# Add noise
if noise_type == 'gaussian':
noisy_image = add_gaussian_noise(image, **kwargs)
elif noise_type == 'salt_and_pepper':
noisy_image = add_salt_and_pepper_noise(image, **kwargs)
else:
raise ValueError(f"Unsupported noise type: {noise_type}")
# Save noisy image
save_image(noisy_image, noisy_image_save_path)
print(f"Saved noisy image: {noisy_image_save_path}")
except Exception as e:
print(f"Error processing {image_path}: {str(e)}")
# Get all image paths recursively from covid and non-covid directories
covid_image_paths = []
non_covid_image_paths = []
for root, dirs, files in os.walk(os.path.join(validation_set_path, 'covid')):
for file in files:
if file.lower().endswith(('.jpg', '.jpeg')):
covid_image_paths.append(os.path.join(root, file))
for root, dirs, files in os.walk(os.path.join(validation_set_path, 'non-covid')):
for file in files:
if file.lower().endswith(('.jpg', '.jpeg')):
non_covid_image_paths.append(os.path.join(root, file))
# Process and save noisy covid images with Gaussian noise
print("Processing and saving COVID images with Gaussian noise:")
add_noise_and_save_images(covid_image_paths, gaussian_output_path, validation_set_path, noise_type='gaussian', sigma=25)
# Process and save noisy non-covid images with Gaussian noise
print("\nProcessing and saving Non-COVID images with Gaussian noise:")
add_noise_and_save_images(non_covid_image_paths, gaussian_output_path, validation_set_path, noise_type='gaussian', sigma=25)
# Process and save noisy covid images with salt-and-pepper noise
print("\nProcessing and saving COVID images with salt-and-pepper noise:")
add_noise_and_save_images(covid_image_paths, salt_and_pepper_output_path, validation_set_path, noise_type='salt_and_pepper', salt_prob=0.05, pepper_prob=0.05)
# Process and save noisy non-covid images with salt-and-pepper noise
print("\nProcessing and saving Non-COVID images with salt-and-pepper noise:")
add_noise_and_save_images(non_covid_image_paths, salt_and_pepper_output_path, validation_set_path, noise_type='salt_and_pepper', salt_prob=0.05, pepper_prob=0.05)
#######################################################################################
######################## Processing Newly Created Noisey IMages ############################################
#######################################################################################
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^modified UNet based segmentation and lung extraction
# Load our UNet model, which was trained on the original clear images
UNet_model = load_model("/home/idu/Desktop/COV19D/saved-models/Full FrameWork/Segmentation/UNet_model_depth3_128batch.h5")
# Define paths
base_path = '/home/idu/Desktop/COV19D/val' # Chnge the path as necessary
covid_path = os.path.join(base_path, 'covid')
non_covid_path = os.path.join(base_path, 'non-covid')
covid_seg_path = os.path.join(base_path, 'covid-seg')
non_covid_seg_path = os.path.join(base_path, 'non-covid-seg')
# Create directories for segmented images if they don't exist
os.makedirs(covid_seg_path, exist_ok=True)
os.makedirs(non_covid_seg_path, exist_ok=True)
dim = (224, 224) # Target dimensions for resizing
# Function to process and segment images using the our Unet model
def process_and_segment_images(input_path, output_path):
for fldr in os.listdir(input_path):
sub_folder_path = os.path.join(input_path, fldr)
if not os.path.isdir(sub_folder_path):
continue
output_sub_folder_path = os.path.join(output_path, fldr)
os.makedirs(output_sub_folder_path, exist_ok=True)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
output_file_path = os.path.join(output_sub_folder_path, filee)
try:
# Load and preprocess image
n = cv2.imread(file_path, 0)
if n is None:
print(f"Skipped {file_path}: image not loaded properly")
continue
image = cv2.imread(file_path, 0)
# Resize images
n = cv2.resize(n, dim)
image = cv2.resize(image, dim)
# Normalize image
image = image * 100.0 / 255.0
image = image / 255.0
image = image[None, :, :, None]
# Predict segmentation mask
pred_mask = UNet_model.predict(image) > 0.5
pred_mask = np.squeeze(pred_mask).astype(np.uint8) * 255
# Clear image border
cleared = clear_border(pred_mask)
# Label image
label_image = label(cleared)
# Keep the labels with the two largest areas
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
# Erosion
selem = disk(2)
binary = binary_erosion(binary, selem)
# Closure operation
selem = disk(10)
binary = binary_closing(binary, selem)
# Fill in small holes
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
binary = binary.astype(np.uint8)
# Superimpose the binary image on the original image
final = cv2.bitwise_and(n, n, mask=binary)
# Save the segmented image
cv2.imwrite(output_file_path, final)
print(f"Segmented image saved: {output_file_path}")
except Exception as e:
print(f"Error processing image {file_path}: {e}")
continue
# Process and segment images in "covid" and "non-covid" directories
process_and_segment_images(covid_path, covid_seg_path)
process_and_segment_images(non_covid_path, non_covid_seg_path)
print("Segmentation completed.")
####################################################################################
######################## Light Weight CNN Model for classification of Noisey and segmented Images
####################################################################################
# Loaing our saved model
model = keras.models.load_model("/home/idu/Desktop/COV19D/saved-models/CNN model/imageprocess-sliceremove-cnn.h5")
#Making predictions on the validation set of noisey images COV19-CT-DB
## Choosing the directory where the test/validation data is at
from termcolor import colored # Importing colored for colored console output
import os
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
folder_path = '/home/idu/Desktop/COV19D/val-noise-added/non-covid' # Change as needed
extensions0 = []
extensions1 = []
extensions2 = []
extensions3 = []
extensions4 = []
extensions5 = []
extensions6 = []
extensions7 = []
extensions8 = []
extensions9 = []
extensions10 = []
extensions11 = []
extensions12 = []
extensions13 = []
covidd = []
noncovidd = []
coviddd = []
noncoviddd = []
covidddd = []
noncovidddd = []
coviddddd = []
noncoviddddd = []
covidd6 = []
noncovidd6 = []
covidd7 = []
noncovidd7 = []
covidd8 = []
noncovidd8 = []
results = 1
for fldr in os.listdir(folder_path):
sub_folder_path = os.path.join(folder_path, fldr)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
try:
c = load_img(file_path, color_mode='grayscale', target_size=(227, 300)) ## The image input size expected
c = img_to_array(c)
c = np.expand_dims(c, axis=0)
c /= 255.0
result = model.predict(c) # Probability of 1 (non-covid)
if result > 0.97: # Class probability threshold is 0.97
extensions1.append(results)
else:
extensions0.append(results)
if result > 0.90: # Class probability threshold is 0.90
extensions3.append(results)
else:
extensions2.append(results)
if result > 0.70: # Class probability threshold is 0.70
extensions5.append(results)
else:
extensions4.append(results)
if result > 0.40: # Class probability threshold is 0.40
extensions7.append(results)
else:
extensions6.append(results)
if result > 0.50: # Class probability threshold is 0.50
extensions9.append(results)
else:
extensions8.append(results)
if result > 0.15: # Class probability threshold is 0.15
extensions11.append(results)
else:
extensions10.append(results)
if result > 0.05: # Class probability threshold is 0.05
extensions13.append(results)
else:
extensions12.append(results)
except Exception as e:
print(f"Error processing image {file_path}: {e}")
continue
# The majority voting at Patient's level
if len(extensions1) > len(extensions0):
print(fldr, colored("NON-COVID", 'red'), len(extensions1), "to", len(extensions0))
noncovidd.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions0), "to", len(extensions1))
covidd.append(fldr)
if len(extensions3) > len(extensions2):
print(fldr, colored("NON-COVID", 'red'), len(extensions3), "to", len(extensions2))
noncoviddd.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions2), "to", len(extensions3))
coviddd.append(fldr)
if len(extensions5) > len(extensions4):
print(fldr, colored("NON-COVID", 'red'), len(extensions5), "to", len(extensions4))
noncovidddd.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions5), "to", len(extensions4))
covidddd.append(fldr)
if len(extensions7) > len(extensions6):
print(fldr, colored("NON-COVID", 'red'), len(extensions7), "to", len(extensions6))
noncoviddddd.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions6), "to", len(extensions7))
coviddddd.append(fldr)
if len(extensions9) > len(extensions8):
print(fldr, colored("NON-COVID", 'red'), len(extensions9), "to", len(extensions8))
noncovidd6.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions8), "to", len(extensions9))
covidd6.append(fldr)
if len(extensions11) > len(extensions10):
print(fldr, colored("NON-COVID", 'red'), len(extensions11), "to", len(extensions10))
noncovidd7.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions10), "to", len(extensions11))
covidd7.append(fldr)
if len(extensions13) > len(extensions12):
print(fldr, colored("NON-COVID", 'red'), len(extensions13), "to", len(extensions12))
noncovidd8.append(fldr)
else:
print(fldr, colored("COVID", 'blue'), len(extensions12), "to", len(extensions13))
covidd8.append(fldr)
extensions0 = []
extensions1 = []
extensions2 = []
extensions3 = []
extensions4 = []
extensions5 = []
extensions6 = []
extensions7 = []
extensions8 = []
extensions9 = []
extensions10 = []
extensions11 = []
extensions12 = []
extensions13 = []
# Checking the results
# print(len(covidd))
# print(len(coviddd))
print(len(covidddd))
print(len(coviddddd))
print(len(covidd6))
print(len(covidd7))
# print(len(covidd8))
# print(len(noncovidd))
# print(len(noncoviddd))
print(len(noncovidddd))
print(len(noncoviddddd))
print(len(noncovidd6))
print(len(noncovidd7))
# print(len(noncovidd8))
# print(len(covidd+noncovidd))
# print(len(coviddd+noncoviddd))
print(len(covidddd+noncovidddd))
print(len(coviddddd+noncoviddddd))
print(len(covidd6+noncovidd6))
print(len(covidd7+noncovidd7))
# print(len(covidd8+noncovidd8))