phyloforfun commited on
Commit
a4b44e3
·
1 Parent(s): dc75c91
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -115,6 +115,20 @@ def get_points_from_contours(contours):
115
  # # Display the image with the selected quadrilateral
116
  # display_image_with_quadrilateral(base_image, quad_points)
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  def get_centroid(contour):
119
  # Compute the centroid for the contour
120
  M = cv2.moments(contour)
@@ -164,6 +178,14 @@ def warp_and_display_images(img, centroids, base_name, flag_mask_rgb, plant_mask
164
  return plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp
165
 
166
  def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper, loc):
 
 
 
 
 
 
 
 
167
  with loc:
168
  btn_back, btn_next = st.columns([2,2])
169
 
@@ -222,26 +244,21 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
222
 
223
  # Initial quadrilateral index
224
  st.session_state['selected_quad_index'] = 0
225
- centroids = get_points_from_contours(valid_quadrilaterals[st.session_state['selected_quad_index']])
226
- # Function to update displayed quadrilateral based on selected index
227
- def update_displayed_quadrilateral(index):
228
- # Extract the four points of the current quadrilateral
229
- centroids = get_points_from_contours(valid_quadrilaterals[index])
230
- return centroids
231
 
232
  # Show initial quadrilateral
233
- centroids = update_displayed_quadrilateral(st.session_state['selected_quad_index'])
234
 
235
  with btn_back:
236
  if st.button('Previous'):
237
- st.session_state['selected_quad_index'] = max(st.session_state['selected_quad_index'] - 1, 0)
238
- update_displayed_quadrilateral(st.session_state['selected_quad_index'])
239
  with btn_next:
240
  if st.button('Next'):
241
- st.session_state['selected_quad_index'] = min(st.session_state['selected_quad_index'] + 1, len(valid_quadrilaterals) - 1)
242
- update_displayed_quadrilateral(st.session_state['selected_quad_index'])
243
 
244
- poly_mask = create_polygon_mask(centroids, flag_mask.shape)
245
 
246
  # Mask the plant_mask with poly_mask
247
  mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
@@ -257,7 +274,7 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
257
  # Draw the bounding quadrilateral
258
  plot_rgb = plant_rgb.copy()
259
  for i in range(4):
260
- cv2.line(plot_rgb, centroids[i], centroids[(i+1)%4], (0, 0, 255), 3)
261
 
262
  # Convert the masks to RGB for visualization
263
  flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
@@ -271,8 +288,8 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
271
  mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
272
 
273
  # Warp the images
274
- plant_rgb_warp = warp_image(plant_rgb, centroids)
275
- plant_mask_warp = warp_image(mask_plant_plot_rgb, centroids)
276
 
277
  return flag_mask_rgb, plant_mask_rgb, mask_plant_plot_rgb, plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp, plant_mask, mask_plant_plot, black_pixels_in_quad
278
 
 
115
  # # Display the image with the selected quadrilateral
116
  # display_image_with_quadrilateral(base_image, quad_points)
117
 
118
+ def increment_index():
119
+ st.session_state.selected_quad_index = (st.session_state.selected_quad_index + 1) % len(st.session_state.valid_quadrilaterals)
120
+ st.session_state.centroids = update_displayed_quadrilateral(st.session_state.selected_quad_index)
121
+
122
+ def decrement_index():
123
+ st.session_state.selected_quad_index = (st.session_state.selected_quad_index - 1) % len(st.session_state.valid_quadrilaterals)
124
+ st.session_state.centroids = update_displayed_quadrilateral(st.session_state.selected_quad_index)
125
+
126
+ # Function to update displayed quadrilateral based on selected index
127
+ def update_displayed_quadrilateral(valid_quadrilaterals, index):
128
+ # Extract the four points of the current quadrilateral
129
+ centroids = get_points_from_contours(valid_quadrilaterals[index])
130
+ return centroids
131
+
132
  def get_centroid(contour):
133
  # Compute the centroid for the contour
134
  M = cv2.moments(contour)
 
178
  return plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp
179
 
180
  def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper, loc):
181
+ # Ensure session_state variables are initialized
182
+ if 'valid_quadrilaterals' not in st.session_state:
183
+ st.session_state.valid_quadrilaterals = [perm for perm in itertools.permutations(significant_contours, 4) if is_valid_quadrilateral(get_points_from_contours(perm))]
184
+ if 'selected_quad_index' not in st.session_state:
185
+ st.session_state.selected_quad_index = 0
186
+ if 'centroids' not in st.session_state:
187
+ st.session_state.centroids = get_points_from_contours(st.session_state.valid_quadrilaterals[0])
188
+
189
  with loc:
190
  btn_back, btn_next = st.columns([2,2])
191
 
 
244
 
245
  # Initial quadrilateral index
246
  st.session_state['selected_quad_index'] = 0
247
+ st.session_state.centroids = get_points_from_contours(valid_quadrilaterals[st.session_state['selected_quad_index']])
248
+
 
 
 
 
249
 
250
  # Show initial quadrilateral
251
+ st.session_state.centroids = update_displayed_quadrilateral(st.session_state['selected_quad_index'])
252
 
253
  with btn_back:
254
  if st.button('Previous'):
255
+ decrement_index() # Call function to decrement index
256
+
257
  with btn_next:
258
  if st.button('Next'):
259
+ increment_index()
 
260
 
261
+ poly_mask = create_polygon_mask(st.session_state.centroids, flag_mask.shape)
262
 
263
  # Mask the plant_mask with poly_mask
264
  mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
 
274
  # Draw the bounding quadrilateral
275
  plot_rgb = plant_rgb.copy()
276
  for i in range(4):
277
+ cv2.line(plot_rgb, st.session_state.centroids[i], st.session_state.centroids[(i+1)%4], (0, 0, 255), 3)
278
 
279
  # Convert the masks to RGB for visualization
280
  flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
 
288
  mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
289
 
290
  # Warp the images
291
+ plant_rgb_warp = warp_image(plant_rgb, st.session_state.centroids)
292
+ plant_mask_warp = warp_image(mask_plant_plot_rgb, st.session_state.centroids)
293
 
294
  return flag_mask_rgb, plant_mask_rgb, mask_plant_plot_rgb, plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp, plant_mask, mask_plant_plot, black_pixels_in_quad
295