Beispiel #1
0
def detect_sift(detector, set_fn, logger, pool=None):
    fnQ, testcase, fnT = set_fn
    full_fnT = myfsys.getf_input(testcase, fnT)
    imgT = read_image(full_fnT)
    with Timer('Detection with SFIT'):
        kpT, descT = affine_detect(detector, imgT, pool=pool, simu_param='sift')
    return imgT, kpT, descT
Beispiel #2
0
 def test_splitkd(self):
     kp, desc = affine_detect(self.detector,
                              self.img1,
                              None,
                              None,
                              simu_param='test2')
     s_kp, s_desc = splta.split_kd(kp, desc, self.splt_num)
     self.assertIsNotNone(s_kp)
     self.assertIsNotNone(s_desc)
     self.assertEqual(len(s_kp), 64)
     self.assertEqual(len(s_desc), 64)
     lenskp = 0
     lensdescr = 0
     for skp, sdesc in zip(s_kp, s_desc):
         if not skp:
             self.assertTrue(False, "Keypoints is Empty")
         else:
             lenskp += len(skp)
             self.assertTrue(True)
         self.assertNotEqual(sdesc.size, 0, "Descriptor is Empty")
         lensdescr += sdesc.shape[0]
     print("{0} == {1}".format(lenskp, len(kp)))
     self.assertEqual(lenskp, len(kp), "Some keypoints were droped out.")
     self.assertEqual(lensdescr, len(desc),
                      "Some descriptors were droped out.")
Beispiel #3
0
 def test_cross_ORB(self):
     feature_name = self.opts.get('--feature', 'orb-flann')
     detector, matcher = init_feature(feature_name)
     with Timer('affine simulation detecting'):
         kp1, desc1 = ab.affine_detect(detector, self.img1) #ASIFT
     with Timer('Feature detecting'):
         h, w = self.img2.shape[:2]
         mask = np.zeros((h, w), np.uint8)
         mask[:] = 255
         kp2, desc2 = ab.affine_detect(detector, self.img2) #ASIFT
     print('imgQ - %d features, imgT - %d features' % (len(kp1), len(kp2)))
     with Timer('matching'):
         raw_matches12 = matcher.knnMatch(desc2, trainDescriptors=desc1, k=2) #2
         raw_matches21 = matcher.knnMatch(desc1, trainDescriptors=desc2, k=2) #2
     p2, p1, kp_pairs = filter_matches_wcross(kp1, kp2, raw_matches12, raw_matches21)
     print('matched points T=%d, Q=%d' % (len(p1), len(p2)))
     self.assertEqual(len(p1), len(p2), "Not same numbers")
def asift_detect(detector, fn):
    img = read_image(fn)
    pool = ThreadPool(processes=cv2.getNumberOfCPUs())
    with Timer('Detection with [ ASIFT ]'):
        splt_kp, splt_desc = affine_detect(detector,
                                           img,
                                           pool=pool,
                                           simu_param='asift')
    return img, splt_kp, splt_desc
Beispiel #5
0
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 affine_detect_into_mesh(detector,
                            split_num,
                            img1,
                            mask=None,
                            simu_param='default'):
    pool = ThreadPool(processes=cv2.getNumberOfCPUs())
    kp, desc = affine_detect(detector,
                             img1,
                             mask,
                             pool=pool,
                             simu_param=simu_param)
    return split_kd(kp, desc, split_num)
Beispiel #7
0
 def test_affine_detection(self):
     with Timer('affine simulation detecting'):
         kp1, desc1 = ab.affine_detect(self.detector, self.img1)  #ASIFT
     with Timer('Feature detecting'):
         h, w = self.img2.shape[:2]
         mask = np.zeros((h, w), np.uint8)
         mask[:] = 255
         kp2, desc2 = self.detector.detectAndCompute(self.img2, mask)
         # kpT, descT = ab.affine_detect(self.detector, self.imgT, simu_param='test') #SIFT
     print('imgQ - %d features, imgT - %d features' % (len(kp1), len(kp2)))
     with Timer('matching T -> Q'):
         raw_matches12 = self.matcher.knnMatch(desc2,
                                               trainDescriptors=desc1,
                                               k=2)  #2
     with Timer('matching Q -> T'):
         raw_matches21 = self.matcher.knnMatch(desc1,
                                               trainDescriptors=desc2,
                                               k=2)  #2
     p2, p1, kp_pairs = filter_matches_wcross(kp1, kp2, raw_matches12,
                                              raw_matches21)
     if len(p2) >= 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))
     vis = explore_match('test_affine_detection', self.img1, self.img2,
                         kp_pairs, None, H)
     print('Ckeck SIFT matching numbers')
     with Timer('Feature detecting'):
         h, w = self.img2.shape[:2]
         mask = np.zeros((h, w), np.uint8)
         mask[:] = 255
         kp3, desc3 = self.detector.detectAndCompute(self.img2, mask)
     with Timer('matching T -> Q'):
         raw_matches23 = self.matcher.knnMatch(desc3,
                                               trainDescriptors=desc2,
                                               k=2)  #2
     with Timer('matching Q -> T'):
         raw_matches32 = self.matcher.knnMatch(desc2,
                                               trainDescriptors=desc3,
                                               k=2)  #2
     p3, p2_, kp_pairs_ = filter_matches_wcross(kp2, kp3, raw_matches23,
                                                raw_matches32)
     self.assertEqual(
         len(kp_pairs), len(kp_pairs_),
         "keypoints pairs and sift_keypoints_pairs is not equal")
    def test_result(self):
        pool = ThreadPool(processes=cv2.getNumberOfCPUs())
        meshList_kpT, meshList_descT = splta2.affine_detect_into_mesh(
            self.detector, self.splt_num, self.img1, pool=pool)
        kpQ, descQ = affine_detect(self.detector, self.img2, pool=pool)

        def count_keypoints():
            c = 0
            for s_kpT in meshList_kpT:
                for kpT in s_kpT:
                    c += len(kpT)
            return c

        print('imgQ - %d features, imgT - %d features' %
              (count_keypoints(), len(kpQ)))

        mesh_pT, mesh_pQ, mesh_pairs = splta2.match_with_cross(
            self.matcher, meshList_descT, meshList_kpT, descQ, kpQ)
        self.assertTrue(True)
    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.")
Beispiel #10
0
              + ' 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)

    # Hs, statuses, pairs = calclate_Homography4splitmesh(mesh_pQ, mesh_pT, mesh_pairs)
    with Timer('estimation'):
        Hs, statuses, pairs = calclate_Homography4splitmesh(mesh_pQ, mesh_pT, mesh_pairs, median=median)

    vis = draw_matches_for_meshes(imgQ, imgT, temp_inf=temp_inf, Hs=Hs, list_merged_mesh_id=list_merged_mesh_id,
                                  merged_map=merged_map)
    cv2.imshow('view weak meshes', vis)
    cv2.imwrite('qrmarker_detection_merged.png', vis)
    cv2.waitKey()

    # viw = explore_match_for_meshes('affine find_obj', imgQ, imgT, pairs,
Beispiel #11
0
    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)