-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing_image.py
More file actions
149 lines (106 loc) · 4.54 KB
/
Copy pathpreprocessing_image.py
File metadata and controls
149 lines (106 loc) · 4.54 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
"""
Sorbonne University - Barkicay university
Master of enginerring sciences - Intelligent Systems Engineering (ISI)
Project of Master 1. Preporcessing Rectal Tumor MR images
file: Preprocessing image
"""
# Import Librairies
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
import random
IMG_SIZE = 224
np.random.seed(42)
# Function for putting the data in folder and sperate them between data et label
def load_images_from_folder(folder, target_size=(224, 224)):
images = []
labels = []
label_map = {'benign': 0, 'malignant': 1} # Mapping textual label by 1 or 0 label
for label in os.listdir(folder):
label_path = os.path.join(folder, label)
if os.path.isdir(label_path):
for filename in os.listdir(label_path):
img_path = os.path.join(label_path, filename)
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) # download the image by grayscale
if img is not None:
images.append(img)
labels.append(label_map.get(label, 1)) # label the image
return images, labels
###########################################################################
# Function to preprocess image data by croping and fit for CNN
def crop_and_resize(image_path, target_size=(IMG_SIZE,IMG_SIZE)):
image = cv2.imread(image_path)
height, width = image.shape[:2]
# Calculate the coordinates for the center crop
center_x, center_y = width // 2, height // 2
crop_size = min(height, width)
x1 = center_x - crop_size // 2
y1 = center_y - crop_size // 2
x2 = x1 + crop_size
y2 = y1 + crop_size
# Crop the image
cropped_image = image[y1:y2, x1:x2]
# Resize the image
resized_image = cv2.resize(cropped_image, target_size, interpolation=cv2.INTER_AREA)
cv2.imwrite(image_path, resized_image)
def affiche(history):
# summarize history for accuracy
plt.subplot(211)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.grid()
plt.show()
# summarize history for loss
plt.subplot(212)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.grid()
plt.show()
##############################################################################
#####################################################################################
# Apply CLAHE to a grayscale image
def apply_clahe_grayscale(image_path):
# Read the image in grayscale
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Check if the image has been loaded correctly
if img is None:
print(f"Error loading image: {image_path}")
return
# Apply CLAHE on the grayscale image
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl_img = clahe.apply(img)
# Save the modified image (overwrite the original)
cv2.imwrite(image_path, cl_img)
#####################################################################################
##############################################################################
# Function applying sobel filter
def apply_sobel_filter(image_path):
# Read the image in grayscale
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply sobel filter (kernel 5x5)
sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
# Compute the magnitude
sobel_image = cv2.magnitude(sobelx, sobely)
# Normalize the image to range 0-255 and convert to uint8
sobel_image = cv2.normalize(sobel_image, None, 0, 255, cv2.NORM_MINMAX)
sobel_image = np.uint8(sobel_image)
# Save the modified image (overwrite the original)
cv2.imwrite(image_path, sobel_image)
# Function for Gaussian filter preprocessing
def apply_gaussian_filter(image_path):
# Read the image in grayscale
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply gaussian filter (kernel 5x5)
image_Gaussian = cv2.GaussianBlur(image, (5, 5), 0)
# Save the modified image (overwrite the original)
cv2.imwrite(image_path, image_Gaussian)