1010import numpy as np
1111import rasterio as rio
1212from numpy .ctypeslib import ndpointer
13+ import cv2
1314import ransac
1415
1516from s2p import rpc_utils
1617from s2p import estimation
1718
19+ try :
20+ from matching .matcher import get_keypoints_loftr
21+ except ImportError as e :
22+ print ("LoFTR matching library could not be imported." )
23+ print (e )
24+
25+ try :
26+ from matching .matcher import get_keypoints_superglue
27+ except ImportError as e :
28+ print ("SuperGlue matching library could not be imported." )
29+ print (e )
30+
31+
1832# Locate sift4ctypes library and raise an ImportError if it can not be
1933# found This call will raise an exception if library can not be found,
2034# at import time
@@ -240,13 +254,12 @@ def keypoints_match_from_nparray(k1, k2, method, sift_threshold,
240254
241255
242256def matches_on_rpc_roi (im1 , im2 , rpc1 , rpc2 , x , y , w , h ,
243- method , sift_thresh , epipolar_threshold ):
257+ method , sift_thresh , epipolar_threshold , matching_method = "sift" ,
258+ min_value = 200 , max_value = 3000 , confidence_threshold = 0.5 ):
244259 """
245260 Compute a list of SIFT matches between two images on a given roi.
246-
247261 The corresponding roi in the second image is determined using the rpc
248262 functions.
249-
250263 Args:
251264 im1, im2: paths to two large tif images
252265 rpc1, rpc2: two instances of the rpcm.RPCModel class
@@ -255,7 +268,6 @@ def matches_on_rpc_roi(im1, im2, rpc1, rpc2, x, y, w, h,
255268 of the rectangle.
256269 method, sift_thresh, epipolar_threshold: see docstring of
257270 s2p.sift.keypoints_match()
258-
259271 Returns:
260272 matches: 2D numpy array containing a list of matches. Each line
261273 contains one pair of points, ordered as x1 y1 x2 y2.
@@ -267,18 +279,100 @@ def matches_on_rpc_roi(im1, im2, rpc1, rpc2, x, y, w, h,
267279 rpc_matches = rpc_utils .matches_from_rpc (rpc1 , rpc2 , x , y , w , h , 5 )
268280 F = estimation .affine_fundamental_matrix (rpc_matches )
269281
270- # if less than 10 matches, lower thresh_dog. An alternative would be ASIFT
271- thresh_dog = 0.0133
272- for _ in range (2 ):
273- p1 = image_keypoints (im1 , x , y , w , h , thresh_dog = thresh_dog )
274- p2 = image_keypoints (im2 , x2 , y2 , w2 , h2 , thresh_dog = thresh_dog )
275- matches = keypoints_match (p1 , p2 , method , sift_thresh , F ,
276- epipolar_threshold = epipolar_threshold ,
277- model = 'fundamental' )
278- if matches is not None and matches .ndim == 2 and matches .shape [0 ] > 10 :
279- break
280- thresh_dog /= 2.0
281- else :
282- print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
283- return None
282+ if matching_method == "loftr" :
283+ p1 , p2 = get_keypoints_loftr (im1 , im2 , min_value , max_value , confidence_threshold ,
284+ x , x2 , y , y2 , w , w2 , h , h2 , rpc_match = True )
285+ if len (p1 ) < 10 or len (p2 ) < 10 :
286+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
287+ return None
288+ p1 [:, 0 ] += x
289+ p1 [:, 1 ] += y
290+ p2 [:, 0 ] += x2
291+ p2 [:, 1 ] += y2
292+ matches = np .hstack ((p1 , p2 ))
293+
294+ if len (matches ) == 0 or matches .ndim != 2 :
295+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
296+ return None
297+ elif len (matches ) > 10 :
298+ inliers = ransac .find_fundamental_matrix (matches , ntrials = 1000 ,
299+ max_err = 0.3 )[0 ]
300+ matches = matches [inliers ]
301+ else :
302+ print ("WARNING: sift.matches_on_rpc_roi: not enough matches." )
303+ return None
304+
305+
306+ elif matching_method == "superglue" :
307+ p1 , p2 = get_keypoints_superglue (im1 , im2 , min_value , max_value , x , x2 , y , y2 , w , w2 , h , h2 , rpc_match = True )
308+ if len (p1 ) < 10 or len (p2 ) < 10 :
309+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
310+ return None
311+
312+ p1 [:, 0 ] += x
313+ p1 [:, 1 ] += y
314+ p2 [:, 0 ] += x2
315+ p2 [:, 1 ] += y2
316+ matches = np .hstack ((p1 , p2 ))
317+
318+ if matches .ndim != 2 :
319+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
320+ return None
321+
322+ elif matching_method == "sift" :
323+ # if less than 10 matches, lower thresh_dog. An alternative would be ASIFT
324+ thresh_dog = 0.0133
325+ for _ in range (2 ):
326+ p1 = image_keypoints (im1 , x , y , w , h , thresh_dog = thresh_dog )
327+ p2 = image_keypoints (im2 , x2 , y2 , w2 , h2 , thresh_dog = thresh_dog )
328+ matches = keypoints_match (p1 , p2 , method , sift_thresh , F ,
329+ epipolar_threshold = epipolar_threshold ,
330+ model = 'fundamental' )
331+ if matches is not None and matches .ndim == 2 and matches .shape [0 ] > 10 :
332+ break
333+ thresh_dog /= 2.0
334+ else :
335+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
336+ return None
337+
338+
339+ elif matching_method == "all" :
340+ p1 , p2 = get_keypoints_loftr (im1 , im2 , min_value , max_value , confidence_threshold ,
341+ x , x2 , y , y2 , w , w2 , h , h2 , rpc_match = True )
342+ p1 [:, 0 ] += x
343+ p1 [:, 1 ] += y
344+ p2 [:, 0 ] += x2
345+ p2 [:, 1 ] += y2
346+ matches = np .hstack ((p1 , p2 ))
347+ if len (matches ) == 0 or matches .ndim != 2 :
348+ print ("WARNING: sift.matches_on_rpc_roi: found no matches." )
349+ matches = None
350+ elif len (matches ) > 10 :
351+ inliers = ransac .find_fundamental_matrix (matches , ntrials = 1000 ,
352+ max_err = 0.3 )[0 ]
353+ matches = matches [inliers ]
354+
355+ p1_sg , p2_sg = get_keypoints_superglue (im1 , im2 , min_value , max_value , x , x2 , y , y2 , w , w2 , h , h2 , rpc_match = True )
356+ p1_sg [:, 0 ] += x
357+ p1_sg [:, 1 ] += y
358+ p1_sg [:, 0 ] += x2
359+ p1_sg [:, 1 ] += y2
360+ matches_sg = np .hstack ((p1_sg , p2_sg ))
361+
362+ thresh_dog = 0.0133
363+ for _ in range (2 ):
364+ p1_sift = image_keypoints (im1 , x , y , w , h , thresh_dog = thresh_dog )
365+ p2_sift = image_keypoints (im2 , x2 , y2 , w2 , h2 , thresh_dog = thresh_dog )
366+ matches_sift = keypoints_match (p1_sift , p2_sift , method , sift_thresh , F ,
367+ epipolar_threshold = epipolar_threshold ,
368+ model = 'fundamental' )
369+ if matches_sift is not None and matches_sift .ndim == 2 and matches_sift .shape [0 ] > 10 :
370+ break
371+ thresh_dog /= 2.0
372+
373+ matches = np .vstack ((matches , matches_sg , matches_sift ))
374+
375+ if len (matches ) < 10 :
376+ return None
377+
284378 return matches
0 commit comments