def detect(detector, fn, splt_num=64, simu_type="default"): full_fn = myfsys.get_template_file_full_path_(fn) img = read_image(full_fn) cv2.imshow('hoge', img) with Timer('Detection with [ ' + simu_type + ' ]'): splt_kp, splt_desc = spltA.affine_detect_into_mesh(detector, splt_num, img, simu_param=simu_type) return img, splt_kp, splt_desc
def split_asift_detect(detector, fn, split_num): img = emod.read_image(fn) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) with Timer('Detection with [ ASIFT ]'): splt_kp, splt_desc = saf.affine_detect_into_mesh(detector, split_num, img, simu_param='asift') return img, splt_kp, splt_desc
def detect_and_match(detector, matcher, set_fn, splt_num=64, simu_type="default"): """ SplitA実験 set_fn: """ fnQ, testcase, fnT = set_fn def get_expt_names(): tmpf, tmpext = os.path.splitext(fnT) return (os.path.basename(__file__), testcase, tmpf) expt_names = get_expt_names() logger = setup(expt_names) logger.info(__doc__) full_fnQ = myfsys.getf_template((fnQ,)) full_fnT = myfsys.getf_input(testcase, fnT) imgQ, imgT = read_images(full_fnQ, full_fnT, logger) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) with Timer('Detection with SPLIT-ASIFT', logger): splt_kpQ, splt_descQ = spltA.affine_detect_into_mesh(detector, splt_num, imgQ, simu_param=simu_type) with Timer('Detection with SFIT', logger): kpT, descT = affine_detect(detector, imgT, pool=pool, simu_param='test') logger.info('imgQ - {0} features, imgT - {1} features'.format(spltA.count_keypoints(splt_kpQ), len(kpT))) with Timer('matching', logger): mesh_pQ, mesh_pT, mesh_pairs = spltA.match_with_cross(matcher, splt_descQ, splt_kpQ, descT, kpT) Hs = [] statuses = [] kp_pairs_long = [] Hs_stable = [] kp_pairs_long_stable = [] for pQ, pT, pairs in zip(mesh_pQ, mesh_pT, mesh_pairs): pairs, H, status = calclate_Homography(pQ, pT, pairs) Hs.append(H) statuses.append(status) if status is not None and not len(status) == 0 and np.sum(status)/len(status) >= 0.4: Hs_stable.append(H) else: Hs_stable.append(None) for p in pairs: kp_pairs_long.append(p) if status is not None and not len(status) == 0 and np.sum(status)/len(status) >= 0.4: kp_pairs_long_stable.append(p) vis = draw_matches_for_meshes(imgQ, imgT, Hs=Hs) cv2.imwrite(myfsys.getf_output(expt_names, 'meshes.png'), vis) visS = draw_matches_for_meshes(imgQ, imgT, Hs=Hs_stable) cv2.imwrite(myfsys.getf_output(expt_names, 'meshes_stable.png'), visS) viw = explore_match_for_meshes('affine find_obj', imgQ, imgT, kp_pairs_long_stable, Hs=Hs_stable) cv2.imwrite(myfsys.getf_output(expt_names, 'meshes_and_keypoints_stable.png'), viw) return vis, visS, viw
def get_split_keypoint_detector(_template_fn, _temp_inf, _detector, _imgQ): try: with splta.Timer('Lording pickle'): splt_kpQ, splt_descQ = splta.affine_load_into_mesh( _template_fn, _temp_inf.get_splitnum()) except ValueError as e: print(e.args) print('If you need to save {} to file as datavase. ¥n' + ' Execute makedb/make_split_combine_featureDB_from_templates.py') with splta.Timer('Detection and dividing'): splt_kpQ, splt_descQ = splta.affine_detect_into_mesh( _detector, _temp_inf.get_splitnum(), _imgQ, simu_param='asift') return splt_kpQ, splt_descQ
def test_affine_detect_into_mesh(self): with Timer('Detection with split into mesh'): splits_kp, splits_desc = splta2.affine_detect_into_mesh( self.detector, self.splt_num, self.img1, simu_param='test2') self.assertIsNotNone(splits_kp) self.assertIsNotNone(splits_desc) self.assertEqual(len(splits_kp), self.splt_num, "It is not same") self.assertEqual(len(splits_desc), self.splt_num, "It is not same") lenskp = 0 lensdescr = 0 for skp, sdesc in zip(splits_kp, splits_desc): if not skp: self.assertTrue(False, "Keypoints of mesh is Empty") else: self.assertEqual(len(skp), 3, "It is not same") for mesh_kp in skp: lenskp += len(mesh_kp) if not sdesc: self.assertTrue(False, "Descriptors of mesh is Empty") self.assertNotEqual(sdesc[0].size, 0, "Descriptor is Empty") self.assertEqual(sdesc[0].shape[1], 128, "SIFT features") self.assertEqual(len(sdesc), 3, "It is not same") for mesh_desc in sdesc: lensdescr += mesh_desc.shape[0] with Timer('Detection'): kp, desc = affine_detect(self.detector, self.img1, simu_param='test2') kps, descs = splta.affine_detect_into_mesh(self.detector, self.splt_num, self.img1, simu_param='test2') a = splta.count_keypoints(kps) print("{0} == {1}, {2}, {3}".format(lenskp, a, len(kp), lensdescr)) self.assertEqual(lenskp, len(kp), "Some keypoints were droped out.") self.assertEqual(lensdescr, len(desc), "Some descriptors were droped out.") self.assertEqual(lenskp, a, "Some keypoints were droped out.") self.assertEqual(lensdescr, a, "Some descriptors were droped out.")
"_scols": scols, "_srows": srows, "_nneighbor": 4 } temp_inf = splta.TmpInf(**template_information) try: with splta.Timer('Lording pickle'): splt_kpQ, splt_descQ = splta.affine_load_into_mesh( template_fn, temp_inf.get_splitnum()) except ValueError as e: print(e.args) print('If you need to save {} to file as datavase. ¥n' + ' Execute makedb/make_split_combine_featureDB_from_templates.py') with splta.Timer('Detection and dividing'): splt_kpQ, splt_descQ = splta.affine_detect_into_mesh( detector, temp_inf.get_splitnum(), imgQ, simu_param='asift') mesh_k_num = splta.np.array([len(keypoints) for keypoints in splt_kpQ ]).reshape(temp_inf.get_mesh_shape()) median = splta.np.nanmedian(mesh_k_num) fn, ext = os.path.splitext(os.path.basename(fn2_full)) testset_name = os.path.basename(os.path.dirname(fn2_full)) imgT = splta.cv2.imread(fn2_full, 0) if imgT is None: print('Failed to load fn2:', fn2_full) sys.exit(1) print("INPUT: {}".format(fn2_full)) pool = splta.ThreadPool(processes=splta.cv2.getNumberOfCPUs()) with splta.Timer('Detection'):
sys.exit(1) print('Using :', feature_name) template_fn, ext = os.path.splitext(os.path.basename(fn1)) template_information = {"_fn": "tmp.png", "template_img": template_fn, "_cols": 800, "_rows": 600, "_scols": 8, "_srows": 8, "_nneighbor": 4} temp_inf = TmpInf(**template_information) try: with Timer('Lording pickle'): splt_kpQ, splt_descQ = affine_load_into_mesh(template_fn, temp_inf.get_splitnum()) except ValueError as e: print(e) print('If you need to save {} to file as datavase. ¥n' + ' Execute /Users/tiwasaki/PycharmProjects/makedb/make_split_combine_featureDB_from_templates.py') with Timer('Detection and dividing'): splt_kpQ, splt_descQ = affine_detect_into_mesh(detector, temp_inf.get_splitnum(), imgQ, simu_param='default') sk_num = count_keypoints(splt_kpQ) m_skQ, m_sdQ, m_k_num, merged_map = combine_mesh_compact(splt_kpQ, splt_descQ, temp_inf) if not sk_num == count_keypoints(m_skQ) and not count_keypoints(m_skQ) == np.sum(m_k_num): print('{0}, {1}, {2}'.format(sk_num, count_keypoints(m_skQ), np.sum(m_k_num))) sys.exit(1) median = np.nanmedian(m_k_num) list_merged_mesh_id = list(set(np.ravel(merged_map))) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) with Timer('Detection'): kpT, descT = affine_detect(detector, imgT, pool=pool, simu_param='test') with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = match_with_cross(matcher, m_sdQ, m_skQ, descT, kpT)
def test_result(self): s_kp, s_desc = splta.affine_detect_into_mesh(self.detector, self.splt_num, self.img1) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) kp2, desc2 = affine_detect(self.detector, self.img2, pool=pool) len_s_kp = 0 for kps in s_kp: len_s_kp += len(kps) print('imgQ - %d features, imgT - %d features' % (len_s_kp, len(kp2))) def calc_H(kp1, kp2, desc1, desc2): with Timer('matching'): raw_matches = self.matcher.knnMatch(desc2, desc1, 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None print( '%d matches found, not enough for homography estimation' % len(p1)) return kp_pairs, H, status def match_and_draw(win): list_kp_pairs = [] Hs = [] statuses = [] i = 0 for kps, desc in zip(s_kp, s_desc): assert type(desc) == type(desc2), "EORROR TYPE" with Timer('matching'): raw_matches = self.matcher.knnMatch(desc2, trainDescriptors=desc, k=2) #2 p2, p1, kp_pairs = filter_matches(kp2, kps, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p2, p1, cv2.RANSAC, 5.0) print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) list_kp_pairs.extend( [kpp for kpp, flag in zip(kp_pairs, status) if flag]) else: H, status = None, None print( '%d matches found, not enough for homography estimation' % len(p1)) Hs.append(H) statuses.extend(status) i += 1 vis = show(win, self.img2, self.img1, list_kp_pairs, statuses, Hs) match_and_draw('affine find_obj') cv2.waitKey() cv2.destroyAllWindows() self.assertEqual(1, 1)