Ayesha352 commited on
Commit
a3781ec
·
verified ·
1 Parent(s): d45e25b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -65,6 +65,24 @@ def fit_to_box(img, target_h=600, target_w=600):
65
  canvas[top:top+new_h, left:left+new_w] = resized
66
  return canvas
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # ---------------- Main Function ----------------
69
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
70
  flat_img = cv2.imread(flat_file)
@@ -87,12 +105,21 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
87
  kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
88
  if kp1 is None or kp2 is None or len(good_matches)<4: continue
89
 
90
- # ---------------- Match Image (aligned sizes)
91
  flat_box = fit_to_box(flat_img, 600, 600)
92
  persp_box = fit_to_box(persp_img, 600, 600)
93
- match_img = cv2.drawMatches(flat_box,kp1,persp_box,kp2,good_matches,None,flags=2)
94
 
95
- # ---------------- Homography ROI
 
 
 
 
 
 
 
 
 
 
96
  src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
97
  dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
98
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
@@ -104,7 +131,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
104
  cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
105
  for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
106
 
107
- # ---------------- XML Ground-Truth overlay
108
  xml_gt_img = persp_img.copy()
109
  ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
110
  xml_polygon = [xml_points[pt] for pt in ordered_pts]
 
65
  canvas[top:top+new_h, left:left+new_w] = resized
66
  return canvas
67
 
68
+ # ---------------- NEW: Remap keypoints to boxed image coords ----------------
69
+ def remap_keypoints_to_box(kps, orig_shape, target_h=600, target_w=600):
70
+ h, w = orig_shape[:2]
71
+ scale = min(target_w / w, target_h / h)
72
+ new_w, new_h = int(w * scale), int(h * scale)
73
+ top = (target_h - new_h) // 2
74
+ left = (target_w - new_w) // 2
75
+
76
+ kps_new = []
77
+ for kp in kps:
78
+ x, y = kp.pt
79
+ x_new = x * scale + left
80
+ y_new = y * scale + top
81
+ # (x, y, size, angle, response, octave, class_id)
82
+ kps_new.append(cv2.KeyPoint(x_new, y_new, max(1.0, kp.size * scale),
83
+ kp.angle, kp.response, kp.octave, kp.class_id))
84
+ return kps_new
85
+
86
  # ---------------- Main Function ----------------
87
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
88
  flat_img = cv2.imread(flat_file)
 
105
  kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
106
  if kp1 is None or kp2 is None or len(good_matches)<4: continue
107
 
108
+ # ---------------- Match Image (aligned sizes) ----------------
109
  flat_box = fit_to_box(flat_img, 600, 600)
110
  persp_box = fit_to_box(persp_img, 600, 600)
 
111
 
112
+ # NEW: remap original keypoints -> boxed coords (order preserved)
113
+ kp1_box = remap_keypoints_to_box(kp1, flat_img.shape, 600, 600)
114
+ kp2_box = remap_keypoints_to_box(kp2, persp_img.shape, 600, 600)
115
+
116
+ match_img = cv2.drawMatches(
117
+ flat_box, kp1_box,
118
+ persp_box, kp2_box,
119
+ good_matches, None, flags=2
120
+ )
121
+
122
+ # ---------------- Homography ROI (still uses original coords) ----------------
123
  src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
124
  dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
125
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
 
131
  cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
132
  for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
133
 
134
+ # ---------------- XML Ground-Truth overlay ----------------
135
  xml_gt_img = persp_img.copy()
136
  ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
137
  xml_polygon = [xml_points[pt] for pt in ordered_pts]