forked from abzargar/COVID-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocess_images.py
36 lines (34 loc) · 1.87 KB
/
preprocess_images.py
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
# =============================================================================
# resizing, normalization and adaptive histogram equalization to images
# =============================================================================
import skimage.io as io
from skimage.transform import rescale,resize
from skimage.util import img_as_uint,img_as_ubyte
from skimage.color import rgb2gray
from skimage import exposure
import os
import numpy as np
# =============================================================================
# source and destination dirs
# =============================================================================
class_name='covid'#'covid' or 'normal' or 'pneumonia'
source_dir='dataset/original_images/'+class_name
destination_dir='dataset/original_images_preprocessed/'+class_name
# =============================================================================
# list images list from source dir
# =============================================================================
image_list=os.listdir(source_dir)# list of images
# =============================================================================
# Normalization and adaptive histogram equalization to each single image
# =============================================================================
for img_name in image_list:
img=io.imread(os.path.join(source_dir,img_name))
if img.ndim != 2:
img_gray = rgb2gray(img[..., :3])
else:
img_gray = img #image is already greyscale
img_resized = resize(img_gray, (512, 512))#convert image size to 512*512
img_rescaled=(img_resized-np.min(img_resized))/(np.max(img_resized)-np.min(img_resized))#min-max normalization
img_enhanced=exposure.equalize_adapthist(img_rescaled)#adapt hist
img_resized_8bit=img_as_ubyte(img_enhanced)
io.imsave(os.path.join(destination_dir,img_name),img_resized_8bit)#save enhanced image to destination dir