NassimeBejaia commited on
Commit
5e5a95f
1 Parent(s): 7206cfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -1
app.py CHANGED
@@ -123,6 +123,21 @@ def display_image(img):
123
 
124
 
125
  # --------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  def process_image(selected_image):
127
  # Convert PIL image to OpenCV format
128
  opencv_image = np.array(selected_image)
@@ -143,7 +158,9 @@ def process_image(selected_image):
143
  # Apply OTSU's thresholding
144
  _, thresh = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
145
 
146
- kernel = np.ones((5, 1), np.uint8)
 
 
147
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
148
 
149
  # # Apply median blur
 
123
 
124
 
125
  # --------------------------------------------
126
+
127
+ def get_dynamic_kernel(img_height):
128
+ # Set the kernel size to be 1% of the image height
129
+ kernel_height = int(0.01 * img_height)
130
+
131
+ # Ensure the kernel height is odd
132
+ if kernel_height % 2 == 0:
133
+ kernel_height += 1
134
+
135
+ # Set minimum and maximum limits
136
+ kernel_height = max(3, kernel_height) # Minimum limit
137
+ kernel_height = min(11, kernel_height) # Maximum limit
138
+
139
+ return np.ones((kernel_height, 1), np.uint8)
140
+
141
  def process_image(selected_image):
142
  # Convert PIL image to OpenCV format
143
  opencv_image = np.array(selected_image)
 
158
  # Apply OTSU's thresholding
159
  _, thresh = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
160
 
161
+
162
+ kernel = get_dynamic_kernel(thresh.shape[0])
163
+ # kernel = np.ones((5, 1), np.uint8)
164
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
165
 
166
  # # Apply median blur