phyloforfun commited on
Commit
9060842
·
1 Parent(s): 931d86a
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -115,6 +115,29 @@ def update_displayed_quadrilateral(index, point_combinations, base_image_path):
115
  # Display the image with the selected quadrilateral
116
  display_image_with_quadrilateral(base_image, quad_points)
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  def is_valid_quadrilateral(centroids):
119
  if len(centroids) != 4:
120
  return False
@@ -185,7 +208,9 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
185
  permutations_of_four = list(itertools.permutations(significant_contours, 4))
186
 
187
  # Filter out invalid quadrilaterals
188
- valid_permutations = [perm for perm in permutations_of_four if is_valid_quadrilateral(get_points_from_contours(perm))]
 
 
189
 
190
  if not valid_permutations:
191
  st.error("No valid quadrilaterals found.")
@@ -508,6 +533,7 @@ def main():
508
  st.image(plant_rgb_warp, caption="Plant Mask Inside Plot Warped to Square", use_column_width=True)
509
  # st.image(plot_rgb_warp, caption="Flag Mask", use_column_width=True)
510
  with R_save:
 
511
  if st.button('Save'):
512
  # Save the masks to their respective folders
513
  save_img(directory_manager.mask_flag, base_name, flag_mask)
 
115
  # Display the image with the selected quadrilateral
116
  display_image_with_quadrilateral(base_image, quad_points)
117
 
118
+ def quadrilateral_area(centroids):
119
+ # Assuming centroids are in correct order (A, B, C, D) to form a quadrilateral
120
+ def distance(p1, p2):
121
+ return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
122
+
123
+ A, B, C, D = centroids
124
+ # Using Bretschneider's formula to calculate area of a quadrilateral
125
+ a = distance(A, B)
126
+ b = distance(B, C)
127
+ c = distance(C, D)
128
+ d = distance(D, A)
129
+ p = (a + b + c + d) / 2 # semi-perimeter
130
+ return math.sqrt((p - a) * (p - b) * (p - c) * (p - d))
131
+
132
+ def sort_permutations_by_area(valid_permutations):
133
+ # Calculate area for each permutation and return sorted list
134
+ perm_areas = [(perm, quadrilateral_area(get_points_from_contours(perm))) for perm in valid_permutations]
135
+ # Sort by area in descending order (largest first)
136
+ perm_areas.sort(key=lambda x: x[1], reverse=True)
137
+ # Return only the sorted permutations, not the areas
138
+ sorted_permutations = [perm for perm, area in perm_areas]
139
+ return sorted_permutations
140
+
141
  def is_valid_quadrilateral(centroids):
142
  if len(centroids) != 4:
143
  return False
 
208
  permutations_of_four = list(itertools.permutations(significant_contours, 4))
209
 
210
  # Filter out invalid quadrilaterals
211
+ valid_permutations0 = [perm for perm in permutations_of_four if is_valid_quadrilateral(get_points_from_contours(perm))]
212
+
213
+ valid_permutations = sort_permutations_by_area(valid_permutations0)
214
 
215
  if not valid_permutations:
216
  st.error("No valid quadrilaterals found.")
 
533
  st.image(plant_rgb_warp, caption="Plant Mask Inside Plot Warped to Square", use_column_width=True)
534
  # st.image(plot_rgb_warp, caption="Flag Mask", use_column_width=True)
535
  with R_save:
536
+ st.write(f"Showing plot outline #{st.session_state.quad_index}")
537
  if st.button('Save'):
538
  # Save the masks to their respective folders
539
  save_img(directory_manager.mask_flag, base_name, flag_mask)