jugggernault commited on
Commit
9285af4
·
1 Parent(s): bbcdd70

feat: add histogram, erosion and dilatation

Browse files
Files changed (2) hide show
  1. app.py +33 -3
  2. improcess.py +75 -6
app.py CHANGED
@@ -30,6 +30,18 @@ def handle_contrast(output_image, contrast: list):
30
  return output_image
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # Fonction pour egaliser l'histogramme
34
  def equalize_hist(image, equalize):
35
  if equalize == True:
@@ -67,12 +79,19 @@ with gr.Blocks(theme="Zarkel/IBM_Carbon_Theme") as demo:
67
  )
68
  with gr.Group():
69
  output_img = gr.Image(
70
- "./profile.jpeg", label="Transformed image", scale=2, interactive=False
 
 
 
 
71
  )
72
  btn = gr.Button(value="Reset") # Bouton de réinitialisation
73
  with gr.Row(variant="panel"):
74
  with gr.Group():
75
- equalize_img = gr.Checkbox(label="Equalize image")
 
 
 
76
  gamma = gr.Slider(
77
  minimum=0, maximum=1, label="Luminosity", interactive=True, value=0
78
  )
@@ -95,6 +114,7 @@ with gr.Blocks(theme="Zarkel/IBM_Carbon_Theme") as demo:
95
  interactive=True,
96
  label="Extract feature",
97
  )
 
98
 
99
  # Mise à jour de l'image lorsqu'un filtre est appliqué
100
  filter.change(fn=choose_filter, inputs=[input_img, filter], outputs=output_img)
@@ -114,12 +134,22 @@ with gr.Blocks(theme="Zarkel/IBM_Carbon_Theme") as demo:
114
  contrast.change(
115
  fn=handle_contrast, inputs=[output_img, contrast], outputs=output_img
116
  )
 
 
 
 
117
  # Egaliser l'histogramme de l'image
118
- equalize_img.change(fn=equalize_hist, inputs=[input_img], outputs=[output_img])
119
  # Effectuer une rotation de l'image
120
  angle.change(fn=improcess.rotate, inputs=[input_img, angle], outputs=[output_img])
121
 
 
122
  features.select(fn=detect_feat, inputs=[input_img, features], outputs=[output_img])
 
 
 
 
 
123
  # Lancer l'application
124
  if __name__ == "__main__":
125
  demo.launch()
 
30
  return output_image
31
 
32
 
33
+ # Fonction pour modifier la morphologie
34
+ def handle_morphology(image, morphology):
35
+ if image is None:
36
+ return None
37
+ for trans in morphology:
38
+ if trans == "Erosion":
39
+ image = improcess.erosion(image)
40
+ elif trans == "Dilatation":
41
+ image = improcess.dilatation(image)
42
+ return image
43
+
44
+
45
  # Fonction pour egaliser l'histogramme
46
  def equalize_hist(image, equalize):
47
  if equalize == True:
 
79
  )
80
  with gr.Group():
81
  output_img = gr.Image(
82
+ "./profile.jpeg",
83
+ label="Transformed image",
84
+ scale=2,
85
+ interactive=False,
86
+ format="png",
87
  )
88
  btn = gr.Button(value="Reset") # Bouton de réinitialisation
89
  with gr.Row(variant="panel"):
90
  with gr.Group():
91
+ morphology = gr.CheckboxGroup(
92
+ choices=["Dilatation", "Erosion"], label="Morphology"
93
+ )
94
+ # equalize_img = gr.Checkbox(label="Equalize image")
95
  gamma = gr.Slider(
96
  minimum=0, maximum=1, label="Luminosity", interactive=True, value=0
97
  )
 
114
  interactive=True,
115
  label="Extract feature",
116
  )
117
+ show_histogram = gr.Button(value="Show Histogram")
118
 
119
  # Mise à jour de l'image lorsqu'un filtre est appliqué
120
  filter.change(fn=choose_filter, inputs=[input_img, filter], outputs=output_img)
 
134
  contrast.change(
135
  fn=handle_contrast, inputs=[output_img, contrast], outputs=output_img
136
  )
137
+ # Mise à jour de l'image avec une transformation morphologique
138
+ morphology.change(
139
+ fn=handle_morphology, inputs=[input_img, morphology], outputs=output_img
140
+ )
141
  # Egaliser l'histogramme de l'image
142
+ # equalize_img.change(fn=equalize_hist, inputs=[input_img], outputs=[output_img])
143
  # Effectuer une rotation de l'image
144
  angle.change(fn=improcess.rotate, inputs=[input_img, angle], outputs=[output_img])
145
 
146
+ # Extraire les caractéristiques
147
  features.select(fn=detect_feat, inputs=[input_img, features], outputs=[output_img])
148
+
149
+ # Afficher l'histogram
150
+ show_histogram.click(
151
+ fn=improcess.return_histogram_path, inputs=[input_img], outputs=[output_img]
152
+ )
153
  # Lancer l'application
154
  if __name__ == "__main__":
155
  demo.launch()
improcess.py CHANGED
@@ -1,7 +1,72 @@
1
  import cv2 as cv
2
  import numpy as np
3
- from skimage import exposure
 
4
  from scipy import signal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
7
  def resize(image: np.ndarray, scale: float = 1.0) -> np.ndarray:
@@ -37,7 +102,9 @@ def negative(image) -> np.ndarray:
37
 
38
 
39
  def gray_scale(image):
40
- return cv.cvtColor(image, cv.COLOR_RGB2GRAY).astype("uint8")
 
 
41
 
42
 
43
  def binarize(image) -> np.ndarray:
@@ -92,8 +159,7 @@ def rotate(img, angle):
92
 
93
 
94
  def find_contours(image):
95
- gray_image = gray_scale(image)
96
- _, thresh = cv.threshold(gray_image, 125, 255, cv.THRESH_BINARY)
97
  contours, _ = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
98
  blank = np.zeros(image.shape, dtype="uint8")
99
  contour_img = cv.drawContours(blank, contours, -1, (255, 255, 255), 2)
@@ -107,7 +173,10 @@ def find_edges(image):
107
 
108
  if __name__ == "__main__":
109
  image = cv.imread("profile.jpeg")
110
-
111
  cv.imshow("normal", image)
112
- cv.imshow("Gamma", find_contours(image))
 
 
 
113
  cv.waitKey(0)
 
1
  import cv2 as cv
2
  import numpy as np
3
+ from skimage import exposure, morphology
4
+ from skimage.filters import threshold_mean, threshold_otsu
5
  from scipy import signal
6
+ import matplotlib.pyplot as plt
7
+
8
+
9
+ def show_histogram(image):
10
+ _, axes = plt.subplots(1, 2)
11
+ if image.ndim == 2:
12
+ hist = exposure.histogram(image)
13
+ axes[0].imshow(image, cmap=plt.get_cmap("gray"))
14
+ axes[0].set_title("Image")
15
+ axes[1].plot(hist[0])
16
+ axes[1].set_title("Histogram")
17
+ else:
18
+ axes[0].imshow(image)
19
+ axes[0].set_title("Image")
20
+ axes[1].set_title("Histogram")
21
+ colors = ["red", "green", "blue"]
22
+ for i, color in enumerate(colors):
23
+ axes[1].plot(exposure.histogram(image[..., i])[0], color=color)
24
+ plt.show()
25
+
26
+
27
+ def return_histogram_path(image):
28
+ if image.ndim == 2:
29
+ hist = exposure.histogram(image)
30
+ plt.plot(hist[0])
31
+ plt.xlabel("Pixel Intensity")
32
+ plt.ylabel("Frequency")
33
+ plt.title("Histogram")
34
+ else:
35
+ plt.title("Histogram")
36
+ colors = ["red", "green", "blue"]
37
+ for i, color in enumerate(colors):
38
+ plt.plot(exposure.histogram(image[..., i])[0], color=color)
39
+ plt.savefig("histogram.png")
40
+ plt.close()
41
+ return "histogram.png"
42
+
43
+
44
+ def mean_treshold(image):
45
+ if image.ndim == 3:
46
+ image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
47
+ thresh = threshold_mean(image)
48
+ binary = (image > thresh) * 225
49
+ return binary.astype("uint8")
50
+
51
+
52
+ def otsu_treshold(image):
53
+ if image.ndim == 3:
54
+ image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
55
+ thresh = threshold_otsu(image)
56
+ binary = (image > thresh) * 255
57
+ return binary.astype("uint8")
58
+
59
+
60
+ def dilatation(image):
61
+ bin_img = binarize(image) / 255
62
+ dilation = (morphology.binary_dilation(image=bin_img)) * 255
63
+ return dilation.astype("uint8")
64
+
65
+
66
+ def erosion(image):
67
+ bin_img = binarize(image) / 255
68
+ erosion = (morphology.erosion(image=bin_img)) * 255
69
+ return erosion.astype("uint8")
70
 
71
 
72
  def resize(image: np.ndarray, scale: float = 1.0) -> np.ndarray:
 
102
 
103
 
104
  def gray_scale(image):
105
+ if image.ndim == 3:
106
+ return cv.cvtColor(image, cv.COLOR_RGB2GRAY).astype("uint8")
107
+ return image
108
 
109
 
110
  def binarize(image) -> np.ndarray:
 
159
 
160
 
161
  def find_contours(image):
162
+ thresh = binarize(image)
 
163
  contours, _ = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
164
  blank = np.zeros(image.shape, dtype="uint8")
165
  contour_img = cv.drawContours(blank, contours, -1, (255, 255, 255), 2)
 
173
 
174
  if __name__ == "__main__":
175
  image = cv.imread("profile.jpeg")
176
+ # show_histogram(image)
177
  cv.imshow("normal", image)
178
+ cv.imshow(
179
+ "Gamma",
180
+ erosion(image),
181
+ )
182
  cv.waitKey(0)