def main_2(expt_name, fn1, fn2, feature='sift', **template_information): import os kw = {'fn1':fn1, 'fn2':fn2, 'feature':feature, 'template_information':template_information} imgQ, imgT, detector, matcher, temp_inf = expt_setting(**kw) print('using', feature) temp_inf = slac.TmpInf(**template_information) a = temp_inf.tmp_img try: with Timer('Lording pickle'): splt_k, splt_d = slac.affine_load_into_mesh(os.path.splitext(temp_inf.tmp_img)[0], 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_k, splt_d = slac.affine_detect_into_mesh(detector, temp_inf.get_splitnum(), imgQ, simu_param='default') img = imgQ.copy() def plot_kps(img, keypoints): if not len(keypoints) == 0: out_img = cv2.drawKeypoints(img, keypoints.pop(0), None, color=(0,255,0)) return plot_kps(out_img, keypoints) return img out_img = plot_kps(img, splt_k) output_dir = slac.myfm.setup_output_directory(expt_name, "plots") cv2.imwrite(os.path.join(output_dir, 'analyse_'+temp_inf.tmp_img+'.png'), out_img)
def dump_matching_result(fn2, testset_full_path): #外部ファイルに出力する #個々のファイル毎にデータを出力する fn, ext = os.path.splitext(fn2) testcase_full_path = os.path.join(testset_full_path, fn2) imgT = cv2.imread(testcase_full_path, 0) if imgT is None: logger.info('Failed to load fn2:', testcase_full_path) raise ValueError('Not found the file') logger.info("Using Training: {}".format(fn2)) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) with Timer('Detection'): kpT, descT = spla.affine_detect(detector, imgT, pool=pool, simu_param='test') logger.info('imgQ - %d features, imgT - %d features' % (spla.count_keypoints(splt_kpQ), len(kpT))) with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = spla.match_with_cross( matcher, splt_descQ, splt_kpQ, descT, kpT) index_mesh_pairs = format4pickle_pairs(mesh_pairs) import joblib dump_match_testcase_dir = myfsys.setup_output_directory(dump_match_dir, fn) joblib.dump(mesh_pQ, os.path.join(dump_match_testcase_dir, 'mesH_pQ.pikle'), compress=True) joblib.dump(mesh_pT, os.path.join(dump_match_testcase_dir, 'mesH_pT.pikle'), compress=True) import pickle with open(os.path.join(dump_match_testcase_dir, 'mesh_pairs.pickle'), 'wb') as f: pickle.dump(index_mesh_pairs, f) f.close() # for i, mesh_pair in enumerate(index_mesh_pairs): # joblib.dump(mesh_pair, os.path.join(dump_detected_testcase_dir, "mesh_pairs_{0:02d}.pikle".format(i)), # compress=True) with Timer('estimation'): Hs, statuses, pairs = spla.calclate_Homography4splitmesh(mesh_pQ, mesh_pT, mesh_pairs, median=median) joblib.dump(Hs, os.path.join(dump_match_testcase_dir, 'Hs.pikle'), compress=True) joblib.dump(statuses, os.path.join(dump_match_testcase_dir, 'statuses.pikle'), compress=True) index_pairs = tuple( tuple((p.pt, p.size, p.angle, p.response, p.octave, p.class_id) for p in pair) for pair in pairs) with open(os.path.join(dump_match_testcase_dir, 'pairs.pickle'), 'wb') as f: pickle.dump(index_pairs, f)
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 exam(testset_full_path, s_kpQ, s_descQ ): testcase_fns = os.listdir(testset_full_path) testcase_fns.sort() # testcase_fns = emod.only(testcase_fns, '288_010-350.png') def clc(testcase_fn): logger.info('Test Case:{}'.format(testcase_fn)) testcase_full_path = os.path.join(testset_full_path, testcase_fn) imgT, kpT, descT = emod.detect(detector, testcase_full_path) if len(kpT) == 0: return np.zeros((row_num, column_num, 3)) with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = saf.match_with_cross(matcher, s_descQ, s_kpQ, descT, kpT) def f(*pQpTp): inlier_pairs, H, status = calclate_Homography(pQpTp[0], pQpTp[1], pQpTp[2]) if status is None: status = [] return [len(inlier_pairs), len(status), len(pQpTp[2])] pairs_on_meshes = np.array(list(map(f, mesh_pQ, mesh_pT, mesh_pairs))) return pairs_on_meshes.reshape(row_num, column_num, 3) with Timer('matching'): results = list(map(clc, testcase_fns)) # results = np.array(list(map(clc, testcase_fns))) keywords = list(map(lambda z: os.path.splitext(z)[0], testcase_fns)) return dict(zip(keywords, results))
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
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 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
def calculate_each_mesh(column_num, detector, input_fns, matcher, results, row_num, s_descQ, s_kpQ): imgT, kpT, descT = emod.detect(detector, input_fns) with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = saf.match_with_cross(matcher, s_descQ, s_kpQ, descT, kpT) def f(pQ, pT, p): inlier_pairs, H, status = calclate_Homography(pQ, pT, p) if status is None: status = [] return [len(inlier_pairs), len(status), len(p)] pairs_on_meshes = np.array(list(map(f, zip(mesh_pQ, mesh_pT, mesh_pairs)))) # pool = ThreadPool(processes=cv2.getNumberOfCPUs()) # pairs_on_mesh_list = np.array(pool.imap(f, zip(range(len(mesh_pQ)), mesh_pQ, mesh_pT, mesh_pairs))) # pairs_on_mesh = pairs_on_mesh.reshape(row_num, column_num) results.append(pairs_on_meshes.reshape(row_num, column_num))
def clc(testcase_fn): logger.info('Test Case:{}'.format(testcase_fn)) testcase_full_path = os.path.join(testset_full_path, testcase_fn) imgT, kpT, descT = emod.detect(detector, testcase_full_path) if len(kpT) == 0: return np.zeros((row_num, column_num, 3)) with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = saf.match_with_cross(matcher, s_descQ, s_kpQ, descT, kpT) def f(*pQpTp): inlier_pairs, H, status = calclate_Homography(pQpTp[0], pQpTp[1], pQpTp[2]) if status is None: status = [] return [len(inlier_pairs), len(status), len(pQpTp[2])] pairs_on_meshes = np.array(list(map(f, mesh_pQ, mesh_pT, mesh_pairs))) return pairs_on_meshes.reshape(row_num, column_num, 3)
if detector is None: logger.info('unknown feature:{}'.format(feature_name)) sys.exit(1) split_num = column_num * row_num img_q, splt_kp_q, splt_desc_q = split_asift_detect(detector, fn1, split_num) logger.debug('using {}'.format(feature_name)) img_t, kp_t, desc_t = emod.detect(detector, fn2) print('imgQ - %d features, imgT - %d features' % (saf.count_keypoints(splt_kp_q), len(kp_t))) with Timer('matching'): mesh_pQ, mesh_pT, mesh_pairs = saf.match_with_cross( matcher, splt_desc_q, splt_kp_q, desc_t, kp_t) list_H, statuses, kp_pairs, pairs_on_meshes = calculate_hompgraphy( mesh_pQ, mesh_pT, mesh_pairs) mt_p = pairs_on_meshes[:, :, 0] mt_s = pairs_on_meshes[:, :, 1] mt_m = pairs_on_meshes[:, :, 2] ratio_ps = mt_p / mt_s ratio_pm = mt_p / mt_m logger.info(mt_p) logger.info(mt_s) logger.info(mt_m) logger.info(ratio_ps)
sys.exit(1) template_full_fn = myfsys.get_template_file_full_path_(template_fn) try: imgQ = get_img(template_full_fn) except ValueError as e: print(e) print('テンプレートがないだけなので続ける') continue template_information = { "_fn": "tmp.png", "template_img": template_fn, "_cols": 800, "_rows": 600, "_scols": 8, "_srows": 8, "_nneighbor": 4 } temp_inf = slac.TmpInf(**template_information) pool = ThreadPool(processes=cv2.getNumberOfCPUs()) with Timer('calculate Keypoints Descriptors'): kp, des = asift.affine_detect(detector, imgQ, pool=pool) ##キーポイントを出力する index = [] for p in kp: temp = (p.pt, p.size, p.angle, p.response, p.octave, p.class_id) index.append(temp) pickle_path = myfsys.get_pikle_path(fn) with open(pickle_path, mode='wb') as f: pickle.dump((index, des), f)
def match(matcher, kpQ, descQ, kpT, descT, id): with Timer('matching', logger): mesh_pQ, mesh_pT, mesh_pairs = spltA.match_with_cross(matcher, descQ, kpQ, descT, kpT) return mesh_pQ, mesh_pT, mesh_pairs, id
srows = 8 h, w = imgQ.shape[:2] template_fn, ext = os.path.splitext(fn1) template_information = { "_fn": "tmp.png", "template_img": template_fn, "_cols": w, "_rows": h, "_scols": scols, "_srows": srows, "_nneighbor": 4 } temp_inf = TmpInf.TemplateInfo(**template_information) try: with Timer('Lording pickle'): splt_kpQ, splt_descQ = spla.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 Timer('Detection and dividing'): splt_kpQ, splt_descQ = spla.affine_detect_into_mesh( detector, temp_inf.get_splitnum(), imgQ, simu_param='default') mesh_k_num = np.array([len(keypoints) for keypoints in splt_kpQ ]).reshape(temp_inf.get_mesh_shape()) median = np.nanmedian(mesh_k_num) expt_name = os.path.basename(expt_path)
def main_1(expt_name, fn1, fn2, feature='sift', **template_information): kw = {'fn1':fn1, 'fn2':fn2, 'feature':feature, 'template_information':template_information} imgQ, imgT, detector, matcher, temp_inf = expt_setting(**kw) print('using', feature) temp_inf = slac.TmpInf(**template_information) try: with Timer('Lording pickle'): splt_k, splt_d = slac.affine_load_into_mesh(os.path.splitext(temp_inf.tmp_img)[0], 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_k, splt_d = slac.affine_detect_into_mesh(detector, temp_inf.get_splitnum(), imgQ, simu_param='default') mesh_k_num = np.array([len(keypoints) for keypoints in splt_k]).reshape(temp_inf.get_mesh_shape()) # mean, median, max, min, peak2peak, standard_deviation, variance = analysis_num(mesh_k_num) print("plot mesh keypoint heatmap") al_vals = slac.analysis_num(mesh_k_num) print("平均, 中央値, 最大値, 最小値, 値の範囲, 標準偏差, 分散") print("{0:4f}, {1:4f}, {2:4d}, {3:4d}, {4:4d}, {5:4f}, {6:4f}".format(*al_vals)) output_dir = slac.myfm.setup_output_directory(expt_name, "plots") plt.figure(figsize=(16, 12)) sns.set("paper", "whitegrid", "dark", font_scale=3) h = sns.heatmap(mesh_k_num, annot=True, fmt='d', cmap='Blues') h.set(xlabel="x") h.set(ylabel="y") h.set(title="Heatmap of keypoint amounts -" + temp_inf.tmp_img) h_fig = h.get_figure() pp = PdfPages(os.path.join(output_dir, 'analyse_'+temp_inf.tmp_img+'.pdf')) h_fig.savefig(pp, format='pdf') df = slac.analysis_kp(splt_k, temp_inf) # with Timer('plotting Kernel De'): # for i in range(temp_inf.get_splitnum()): # ax = sns.kdeplot(df.query('mesh_id == ' + str(i))['x'], df.query('mesh_id == ' + str(i))['y'], shade=True) # ax.set(ylim=(600, 0)) # ax.set(xlim=(0, 800)) # ax.set(xlabel="x") # ax.set(ylabel="y") # ax.set(title="Kernel density estimation") plt.figure(figsize=(16, 12)) sns.set("paper", "whitegrid", "dark", font_scale=3) g = sns.kdeplot(df['x'], df['y'], shade=True, shade_lowest=False) g.set(ylim=(600, 0)) g.set(xlim=(0, 800)) g.set(xlabel="Width of image") g.set(ylabel="Height of image") g.set(title="Kernel density estimation-"+temp_inf.tmp_img) g_fig = g.get_figure() g_fig.savefig(pp, format='pdf') # logger.info('show mesh map') # plt.figure() # sns.set("paper", "whitegrid", "dark", font_scale=1.5) # mesh_map = temp_inf.get_mesh_map() # mmap_ax = sns.heatmap(mesh_map, annot=True, fmt="d") # mmap_ax.set(xlabel="x") # mmap_ax.set(ylabel="y") # mmap_ax.set(title="Mesh map -" + temp_inf.tmp_img) # mmap_ax_fig = h.get_figure() # mmap_ax_fig.savefig(pp, format='pdf') with Timer('merging'): msplt_k, msplt_d, mmesh_k_num, mmesh_map = slac.combine_mesh(splt_k, splt_d, temp_inf) logger.info('show merged mesh map') plt.figure(figsize=(16, 12)) sns.set("paper", "whitegrid", "dark", font_scale=3) merged_map_ax = sns.heatmap(mmesh_map, annot=True, fmt="d") merged_map_ax.set(xlabel="x") merged_map_ax.set(ylabel="y") merged_map_ax.set(title="Merged mesh map -" + temp_inf.tmp_img) merged_map_ax_fig = merged_map_ax.get_figure() merged_map_ax_fig.savefig(pp, format='pdf') plt.figure(figsize=(16, 12)) sns.set("paper", "whitegrid", "dark", font_scale=3) mh = sns.heatmap(mmesh_k_num, annot=True, fmt='d', cmap='Blues') mh.set(xlabel="x") mh.set(ylabel="y") mh.set(title="Heatmap of merged keypoint amounts -" + temp_inf.tmp_img) mh_fig = mh.get_figure() mh_fig.savefig(pp, format='pdf') # pp.savefig() pp.close()