phyloforfun commited on
Commit
5a0ce16
·
1 Parent(s): 8345aa0
Files changed (1) hide show
  1. app.py +84 -40
app.py CHANGED
@@ -188,6 +188,50 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
188
  if st.button('Next'):
189
  selected_quad_index = min(selected_quad_index + 1, len(point_combinations) - 1)
190
  centroids = update_displayed_quadrilateral(selected_quad_index)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  # If there are exactly 4 largest contours, proceed with existing logic
193
  elif len(significant_contours) == 4:
@@ -204,50 +248,50 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
204
  else:
205
  cx, cy = 0, 0
206
  centroids.append((cx, cy))
207
-
208
- # Compute the centroid of the centroids
209
- centroid_x = sum(x for x, y in centroids) / 4
210
- centroid_y = sum(y for x, y in centroids) / 4
211
 
212
- # Sort the centroids
213
- centroids.sort(key=lambda point: (-math.atan2(point[1] - centroid_y, point[0] - centroid_x)) % (2 * np.pi))
214
 
215
- # Create a polygon mask using the sorted centroids
216
- poly_mask = np.zeros_like(flag_mask)
217
- cv2.fillPoly(poly_mask, [np.array(centroids)], 255)
218
-
219
- # Mask the plant_mask with poly_mask
220
- mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
221
 
222
- # Count the number of black pixels inside the quadrilateral
223
- total_pixels_in_quad = np.prod(poly_mask.shape)
224
- white_pixels_in_quad = np.sum(poly_mask == 255)
225
- black_pixels_in_quad = total_pixels_in_quad - white_pixels_in_quad
226
-
227
- # Extract the RGB pixels from the original image using the mask_plant_plot
228
- plant_rgb = cv2.bitwise_and(img, img, mask=mask_plant_plot)
229
-
230
- # Draw the bounding quadrilateral
231
- plot_rgb = plant_rgb.copy()
232
- for i in range(4):
233
- cv2.line(plot_rgb, centroids[i], centroids[(i+1)%4], (0, 0, 255), 3)
234
-
235
- # Convert the masks to RGB for visualization
236
- flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
237
- orange_color = [255, 165, 0] # RGB value for orange
238
- flag_mask_rgb[np.any(flag_mask_rgb != [0, 0, 0], axis=-1)] = orange_color
239
-
240
- plant_mask_rgb = cv2.cvtColor(plant_mask, cv2.COLOR_GRAY2RGB)
241
- mask_plant_plot_rgb = cv2.cvtColor(mask_plant_plot, cv2.COLOR_GRAY2RGB)
242
- bright_green_color = [0, 255, 0]
243
- plant_mask_rgb[np.any(plant_mask_rgb != [0, 0, 0], axis=-1)] = bright_green_color
244
- mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
245
-
246
- # Warp the images
247
- plant_rgb_warp = warp_image(plant_rgb, centroids)
248
- plant_mask_warp = warp_image(mask_plant_plot_rgb, centroids)
249
 
250
- 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
251
 
252
  def calculate_coverage(mask_plant_plot, plant_mask_warp, black_pixels_in_quad):
253
  # Calculate the percentage of white pixels for mask_plant_plot
 
188
  if st.button('Next'):
189
  selected_quad_index = min(selected_quad_index + 1, len(point_combinations) - 1)
190
  centroids = update_displayed_quadrilateral(selected_quad_index)
191
+ #############
192
+ # Compute the centroid of the centroids
193
+ centroid_x = sum(x for x, y in centroids) / 4
194
+ centroid_y = sum(y for x, y in centroids) / 4
195
+
196
+ # Sort the centroids
197
+ centroids.sort(key=lambda point: (-math.atan2(point[1] - centroid_y, point[0] - centroid_x)) % (2 * np.pi))
198
+
199
+ # Create a polygon mask using the sorted centroids
200
+ poly_mask = np.zeros_like(flag_mask)
201
+ cv2.fillPoly(poly_mask, [np.array(centroids)], 255)
202
+
203
+ # Mask the plant_mask with poly_mask
204
+ mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
205
+
206
+ # Count the number of black pixels inside the quadrilateral
207
+ total_pixels_in_quad = np.prod(poly_mask.shape)
208
+ white_pixels_in_quad = np.sum(poly_mask == 255)
209
+ black_pixels_in_quad = total_pixels_in_quad - white_pixels_in_quad
210
+
211
+ # Extract the RGB pixels from the original image using the mask_plant_plot
212
+ plant_rgb = cv2.bitwise_and(img, img, mask=mask_plant_plot)
213
+
214
+ # Draw the bounding quadrilateral
215
+ plot_rgb = plant_rgb.copy()
216
+ for i in range(4):
217
+ cv2.line(plot_rgb, centroids[i], centroids[(i+1)%4], (0, 0, 255), 3)
218
+
219
+ # Convert the masks to RGB for visualization
220
+ flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
221
+ orange_color = [255, 165, 0] # RGB value for orange
222
+ flag_mask_rgb[np.any(flag_mask_rgb != [0, 0, 0], axis=-1)] = orange_color
223
+
224
+ plant_mask_rgb = cv2.cvtColor(plant_mask, cv2.COLOR_GRAY2RGB)
225
+ mask_plant_plot_rgb = cv2.cvtColor(mask_plant_plot, cv2.COLOR_GRAY2RGB)
226
+ bright_green_color = [0, 255, 0]
227
+ plant_mask_rgb[np.any(plant_mask_rgb != [0, 0, 0], axis=-1)] = bright_green_color
228
+ mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
229
+
230
+ # Warp the images
231
+ plant_rgb_warp = warp_image(plant_rgb, centroids)
232
+ plant_mask_warp = warp_image(mask_plant_plot_rgb, centroids)
233
+
234
+ return flag_mask_rgb, pla
235
 
236
  # If there are exactly 4 largest contours, proceed with existing logic
237
  elif len(significant_contours) == 4:
 
248
  else:
249
  cx, cy = 0, 0
250
  centroids.append((cx, cy))
251
+ ########################
252
+ # Compute the centroid of the centroids
253
+ centroid_x = sum(x for x, y in centroids) / 4
254
+ centroid_y = sum(y for x, y in centroids) / 4
255
 
256
+ # Sort the centroids
257
+ centroids.sort(key=lambda point: (-math.atan2(point[1] - centroid_y, point[0] - centroid_x)) % (2 * np.pi))
258
 
259
+ # Create a polygon mask using the sorted centroids
260
+ poly_mask = np.zeros_like(flag_mask)
261
+ cv2.fillPoly(poly_mask, [np.array(centroids)], 255)
262
+
263
+ # Mask the plant_mask with poly_mask
264
+ mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
265
 
266
+ # Count the number of black pixels inside the quadrilateral
267
+ total_pixels_in_quad = np.prod(poly_mask.shape)
268
+ white_pixels_in_quad = np.sum(poly_mask == 255)
269
+ black_pixels_in_quad = total_pixels_in_quad - white_pixels_in_quad
270
+
271
+ # Extract the RGB pixels from the original image using the mask_plant_plot
272
+ plant_rgb = cv2.bitwise_and(img, img, mask=mask_plant_plot)
273
+
274
+ # Draw the bounding quadrilateral
275
+ plot_rgb = plant_rgb.copy()
276
+ for i in range(4):
277
+ cv2.line(plot_rgb, centroids[i], 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)
281
+ orange_color = [255, 165, 0] # RGB value for orange
282
+ flag_mask_rgb[np.any(flag_mask_rgb != [0, 0, 0], axis=-1)] = orange_color
283
+
284
+ plant_mask_rgb = cv2.cvtColor(plant_mask, cv2.COLOR_GRAY2RGB)
285
+ mask_plant_plot_rgb = cv2.cvtColor(mask_plant_plot, cv2.COLOR_GRAY2RGB)
286
+ bright_green_color = [0, 255, 0]
287
+ plant_mask_rgb[np.any(plant_mask_rgb != [0, 0, 0], axis=-1)] = bright_green_color
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, centroids)
292
+ plant_mask_warp = warp_image(mask_plant_plot_rgb, 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
 
296
  def calculate_coverage(mask_plant_plot, plant_mask_warp, black_pixels_in_quad):
297
  # Calculate the percentage of white pixels for mask_plant_plot