def __init__(self): self.ratio = 0.85 self.min_match = 10 # self.sift=cv2.xfeatures2d.SIFT_create() # self.sift = cv2.ORB_create(nfeatures=1000) self.sift = cv2.SIFT_create(nfeatures=1000) self.smoothing_window_size = 100 self.img1 = None self.img2 = None self.shareview = None self.data1 = cudasift.PySiftData() self.data2 = cudasift.PySiftData() self.last_Hmatrix = np.ndarray(shape=(3, 3), dtype=float, order='F')
def test_01_02_speak_again(self): # # Check the four corners of a square # data = cudasift.PySiftData(100) img = np.zeros((100, 100), np.uint8) img[10:-9, 10] = 128 img[10, 10:-9] = 128 img[10:-9, -10] = 128 img[-10, 10:-9] = 128 cudasift.ExtractKeypoints(img, data) self.assertEqual(len(data), 4) df, keypoints = data.to_data_frame() idx = np.lexsort((df.xpos, df.ypos)) # # Check that the four corners are just inside the square # for i in (0, 1): self.assertTrue(df.ypos[idx[i]] > 10 and df.ypos[idx[i]] < 15) for i in (2, 3): self.assertTrue(df.ypos[idx[i]] > 85 and df.ypos[idx[i]] < 90) for i in (0, 2): self.assertTrue(df.xpos[idx[i]] > 10 and df.xpos[idx[i]] < 15) for i in (1, 3): self.assertTrue(df.xpos[idx[i]] > 85 and df.xpos[idx[i]] < 90)
def test_01_04_match(self): # # A 3/4/5 right triangle # img = np.zeros((100, 100), np.uint8) img[10:90, 10] = 255 img[10, 10:70] = 255 img[np.arange(90, 9, -1), 70 - (np.arange(80, -1, -1) * 3 / 4 + .5).astype(int)] = 255 data1 = cudasift.PySiftData(100) cudasift.ExtractKeypoints(img, data1) data2 = cudasift.PySiftData(100) cudasift.ExtractKeypoints(img.transpose(), data2) cudasift.PyMatchSiftData(data1, data2) df1, keypoints1 = data1.to_data_frame() df2, keypoints2 = data2.to_data_frame() pass
def registration_gpu_old(self, img1, img2): t0 = time.time() img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) data1 = cudasift.PySiftData() cudasift.ExtractKeypoints(img1, data1) data2 = cudasift.PySiftData() cudasift.ExtractKeypoints(img2, data2) printd("length:", len(data1), len(data2)) cudasift.PyMatchSiftData(data1, data2) df1, keypoints1 = data1.to_data_frame() image1_kp = [] image2_kp = [] for index, k in df1.iterrows(): image1_kp.append((k['xpos'], k['ypos'])) image2_kp.append((k['match_xpos'], k['match_ypos'])) image1_kp = np.float32(image1_kp) image2_kp = np.float32(image2_kp) # ------------compare the goodpoints position to decide the iamge sequence------- image1_kp_meanx = np.mean([i[0] for i in image1_kp]) image2_kp_meanx = np.mean([i[0] for i in image2_kp]) printd("Image1_kp meanX", image1_kp_meanx) printd("Image2_kp meanX", image2_kp_meanx) if image1_kp_meanx > image2_kp_meanx: seqFlag = 0 # img1|img2 H, status = cv2.findHomography(image2_kp, image1_kp, cv2.RANSAC, 5.0) else: seqFlag = 1 # img2|img1 H, status = cv2.findHomography(image1_kp, image2_kp, cv2.RANSAC, 5.0) # printd("Finish Regis:", H, seqFlag) printd("TIME: ", time.time() - t0) return H, seqFlag
def test_01_01_nothing(self): # "How? Nothing will come of nothing.", Lear 1:1 # data = cudasift.PySiftData(100) img = np.zeros((100, 100), np.uint8) cudasift.ExtractKeypoints(img, data) self.assertEqual(len(data), 0) df, keypoints = data.to_data_frame() self.assertEqual(len(df), 0) self.assertSequenceEqual(keypoints.shape, (0, 128)) for column in "xpos", "ypos", "scale", "sharpness", "edgeness",\ "orientation", "score", "ambiguity", "match", "match_xpos", \ "match_ypos", "match_error", "subsampling": assert column in df.columns
def test_01_03_from_data_frame(self): data = cudasift.PySiftData(100) img = np.zeros((100, 100), np.uint8) img[10:-9, 10] = 128 img[10, 10:-9] = 128 img[10:-9, -10] = 128 img[-10, 10:-9] = 128 cudasift.ExtractKeypoints(img, data) self.assertEqual(len(data), 4) df, keypoints = data.to_data_frame() data2 = cudasift.PySiftData.from_data_frame(df, keypoints) df2, keypoints2 = data2.to_data_frame() np.testing.assert_array_equal(keypoints, keypoints2) np.testing.assert_array_equal(df2.xpos, df.xpos) np.testing.assert_array_equal(df.ypos, df2.ypos) np.testing.assert_array_equal(df.scale, df2.scale) np.testing.assert_array_equal(df.sharpness, df2.sharpness) np.testing.assert_array_equal(df.edgeness, df2.edgeness) np.testing.assert_array_equal(df.score, df2.score) np.testing.assert_array_equal(df.ambiguity, df2.ambiguity)
def extract_features(array, nfeatures=None, **kwargs): """ A custom docstring. """ if not nfeatures: nfeatures = int(max(array.shape) / 1.25) else: warnings.warn( 'NFeatures specified with the CudaSift implementation. Please ensure the distribution of keypoints is what you expect.' ) siftdata = cs.PySiftData(nfeatures) cs.ExtractKeypoints(array, siftdata, **kwargs) keypoints, descriptors = siftdata.to_data_frame() keypoints = keypoints[[ 'x', 'y', 'scale', 'sharpness', 'edgeness', 'orientation', 'score', 'ambiguity' ]] # Set the columns that have unfilled values to zero to avoid confusion keypoints['score'] = 0.0 keypoints['ambiguity'] = 0.0 return keypoints, descriptors
cv_kps = [] keypoints_1 = keypoints_1[['xpos', 'ypos', 'scale', 'sharpness', 'edgeness', 'orientation', 'score', 'ambiguity']] for i in range(len(keypoints_1)): x = keypoints_1['xpos'][i] y = keypoints_1['ypos'][i] size = keypoints_1['scale'][i] kp = cv2.KeyPoint(x,y,size) cv_kps.append(kp) return cv_kps if 1: array = img1.copy() gray1 = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY) nfeatures = int(max(gray1.shape) / 1.25) siftdata1 = cs.PySiftData(nfeatures) ret = cs.ExtractKeypoints(gray1, siftdata1, thresh=2,upScale=True) # cs.PyPrintSiftData(siftdata1)# 打印信息 keypoints_1, descriptors_1 = siftdata1.to_data_frame() descriptors_1 = (descriptors_1 * 255).astype("float32") keypoints_1 = to_cvKeyPoints(keypoints_1) array = img2.copy() # print("array:",array.shape) gray2 = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY) nfeatures = int(max(gray2.shape) / 1.25) siftdata2 = cs.PySiftData(nfeatures) ret = cs.ExtractKeypoints(gray1, siftdata2, thresh=2,upScale=True) keypoints_2, descriptors_2 = siftdata2.to_data_frame() descriptors_2 = (descriptors_2 * 255).astype("float32") keypoints_2 = to_cvKeyPoints(keypoints_2)