def test(): (hs, qcx, cx, fm, fs, rchip1, rchip2, kpts1, kpts2) = ld2.get_sv_test_data() # READ IMAGE AND ROI cx2_roi = hs.tables.cx2_roi cx2_gx = hs.tables.cx2_gx gx2_gname = hs.tables.gx2_gname #--- roi = cx2_roi[cx] gx = cx2_gx[cx] img_fname = gx2_gname[gx] img_path = os.path.join(hs.dirs.img_dir, img_fname) #--- #print('Showing Input') ## Original Image #df2.imshow(mask_input, fignum=1, plotnum=122) #df2.present() # Crop out chips #chip = img[ry:(ry+rh), rx:(rx+rw)] #chip_mask = mask[ry:(ry+rh), rx:(rx+rw)] # IMAGE AND MASK #df2.imshow(img, plotnum=121, fignum=2, doclf=True) #df2.imshow(mask, plotnum=122, fignum=2) # MASKED CHIP #df2.figure(fignum=3, doclf=True) #df2.imshow(chip, plotnum=131, fignum=3, doclf=True) #df2.imshow(color_mask, plotnum=132, fignum=3) #df2.imshow(seg_chip, plotnum=133, fignum=3) seg_chip = segment(img_path, roi) df2.show_img(hs, cx, fignum=1, plotnum=121, title='original', doclf=True) df2.imshow(seg_chip, fignum=1, plotnum=122, title='segmented') df2.present()
#---- df2.imshow(rchip, plotnum=(2,3,3), title='2 identity') draw_kpts3(kpts1.copy(), 2) df2.update() #---- df2.imshow(rchip, plotnum=(2,3,4), title='3 identity') draw_kpts3(kpts1.copy(), 3) df2.update() #---- df2.imshow(rchip, plotnum=(2,3,5), title='inv sqrtm 4') draw_kpts3(kpts1.copy(), 4) df2.update() #---- df2.imshow(rchip, plotnum=(2,3,6), title='inv sqrtm 5') draw_kpts3(kpts1.copy(), 5) df2.update() # TEST hprint = helpers.horiz_print invA1 = inv(A1) hprint('invE = ', invE) hprint('A1 = ', A1) hprint('invA1 = ', invA1) exec(df2.present())
def test(): weird_A = np.array([(2.7, 4.2, 10), (1.7, 3.1, 20), (2.2, 2.4, 1.1)]) num_mat = 2000 frac_in = .5 num_inl = int(num_mat * frac_in) kpts1_m = np.random.rand(5, num_mat) kpts2_m = np.random.rand(5, num_mat) kpts2_cor = kpts1_m[:, 0:num_inl] x_cor = kpts2_cor[0] y_cor = kpts2_cor[1] z_cor = np.ones(num_inl) a_cor = kpts2_cor[2] zeros = np.zeros(num_inl) c_cor = kpts2_cor[3] d_cor = kpts2_cor[4] kpts2_mats = np.array([(a_cor, zeros, x_cor), (c_cor, d_cor, y_cor), (zeros, zeros, z_cor)]).T # Do some weird transform on some keypoints import scipy.linalg for x in xrange(num_inl): mat = weird_A.dot(kpts2_mats[x].T) mat /= mat[2,2] kpts2_m[0,x] = mat[0,2] kpts2_m[1,x] = mat[1,2] kpts2_m[2,x] = mat[0,0] kpts2_m[3,x] = mat[1,0] kpts2_m[4,x] = mat[1,2] # Find some inliers? xy_thresh_sqrd = 7 H, inliers = H_homog_from_DELSAC(kpts1_m, kpts2_m, xy_thresh_sqrd) #aff_inliers1 = sv2.__affine_inliers(x1_m, y1_m, x2_m, y2_m, #acd1_m, acd2_m, xy_thresh_sqrd, scale_thresh) H_prime = sv2.compute_homog(x1_mn, y1_mn, x2_mn, y2_mn) H = linalg.solve(T2, H_prime).dot(T1) # Unnormalize Hdet = linalg.det(H) # Estimate final inliers acd1_m = kpts1_m[2:5,:] # keypoint shape matrix [a 0; c d] matches acd2_m = kpts2_m[2:5,:] # Precompute the determinant of lower triangular matrix (a*d - b*c); b = 0 det1_m = acd1_m[0] * acd1_m[2] det2_m = acd2_m[0] * acd2_m[2] # Matrix Multiply xyacd matrix by H # [[A, B, X], # [C, D, Y], # [E, F, Z]] # dot # [(a, 0, x), # (c, d, y), # (0, 0, 1)] # = # [(a*A + c*B + 0*E, 0*A + d*B + 0*X, x*A + y*B + 1*X), # (a*C + c*D + 0*Y, 0*C + d*D + 0*Y, x*C + y*D + 1*Y), # (a*E + c*F + 0*Z, 0*E + d*F + 0*Z, x*E + y*F + 1*Z)] # = # [(a*A + c*B, d*B, x*A + y*B + X), # (a*C + c*D, d*D, x*C + y*D + Y), # (a*E + c*F, d*F, x*E + y*F + Z)] # # IF x=0 and y=0 # = # [(a*A + c*B, d*B, 0*A + 0*B + X), # (a*C + c*D, d*D, 0*C + 0*D + Y), # (a*E + c*F, d*F, 0*E + 0*F + Z)] # = # [(a*A + c*B, d*B, X), # (a*C + c*D, d*D, Y), # (a*E + c*F, d*F, Z)] # --- # A11 = a*A + c*B # A21 = a*C + c*D # A31 = a*E + c*F # A12 = d*B # A22 = d*D # A32 = d*F # A31 = X # A32 = Y # A33 = Z # # det(A) = A11*(A22*A33 - A23*A32) - A12*(A21*A33 - A23*A31) + A13*(A21*A32 - A22*A31) det1_mAt = det1_m * Hdet # Check Error in position and scale xy_sqrd_err = (x1_mAt - x2_m)**2 + (y1_mAt - y2_m)**2 scale_sqrd_err = det1_mAt / det2_m # Check to see if outliers are within bounds xy_inliers = xy_sqrd_err < xy_thresh_sqrd s1_inliers = scale_sqrd_err > scale_thresh_low s2_inliers = scale_sqrd_err < scale_thresh_high _inliers, = np.where(np.logical_and(np.logical_and(xy_inliers, s1_inliers), s2_inliers)) xy1_mHt = transform_xy(H, xy1_m) # Transform Kpts1 to Kpts2-space sqrd_dist_error = np.sum( (xy1_mHt - xy2_m)**2, axis=0) # Final Inlier Errors inliers = sqrd_dist_error < xy_thresh_sqrd df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers1], kpts2_m.T[aff_inliers1], title=title, fignum=2, vert=False) df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers2], kpts2_m.T[aff_inliers2], title=title, fignum=3, vert=False) df2.present(wh=(600,400)) # This new function is much faster .035 vs .007 # EXPLOITS LOWER TRIANGULAR MATRIXES # Precompute the determinant of matrix 2 (a*d - b*c), but b = 0 # Need the inverse of acd2_m: 1/det * [(d, -b), (-c, a)] # Precompute lower triangular affine tranforms inv2_m (dot) acd1_m # [(a2*a1), (c2*a1+d2*c1), (d2*d1)] # IN HOMOGRAPHY INLIERS ''' y_data = [] x_data = [] for xy_thresh in np.linspace(.001, .005, 50): xy_thresh_sqrd = img2_diaglen_sqrd * xy_thresh aff_inliers = __affine_inliers(x1_m, y1_m, acd1_m, x2_m, y2_m, acd2_m, xy_thresh_sqrd, scale_thresh_low, scale_thresh_high) y_data.append(len(aff_inliers)) x_data.append(xy_thresh) plt.plot(x_data, y_data) df2.show_matches2(rchip1, rchip2, kpts1, kpts2, fm=fm[aff_inliers,:], fignum=3, vert=True) ''' '''
def test_realdata2(): from helpers import printWARN, printINFO import warnings import numpy.linalg as linalg import numpy as np import scipy.sparse as sparse import scipy.sparse.linalg as sparse_linalg import load_data2 import params import draw_func2 as df2 import helpers import spatial_verification #params.reload_module() #load_data2.reload_module() #df2.reload_module() db_dir = load_data2.MOTHERS hs = load_data2.HotSpotter(db_dir) assign_matches = hs.matcher.assign_matches qcx = 0 cx = hs.get_other_cxs(qcx)[0] fm, fs, score = hs.get_assigned_matches_to(qcx, cx) # Get chips rchip1 = hs.get_chip(qcx) rchip2 = hs.get_chip(cx) # Get keypoints kpts1 = hs.get_kpts(qcx) kpts2 = hs.get_kpts(cx) # Get feature matches kpts1_m = kpts1[fm[:, 0], :].T kpts2_m = kpts2[fm[:, 1], :].T title='(qx%r v cx%r)\n #match=%r' % (qcx, cx, len(fm)) df2.show_matches2(rchip1, rchip2, kpts1, kpts2, fm, fs, title=title) np.random.seed(6) subst = helpers.random_indexes(len(fm),len(fm)) kpts1_m = kpts1[fm[subst, 0], :].T kpts2_m = kpts2[fm[subst, 1], :].T df2.reload_module() df2.SHOW_LINES = True df2.ELL_LINEWIDTH = 2 df2.LINE_ALPHA = .5 df2.ELL_ALPHA = 1 df2.reset() df2.show_keypoints(rchip1, kpts1_m.T, fignum=0, plotnum=121) df2.show_keypoints(rchip2, kpts2_m.T, fignum=0, plotnum=122) df2.show_matches2(rchip1, rchip2, kpts1_m.T, kpts2_m.T, title=title, fignum=1, vert=True) spatial_verification.reload_module() with helpers.Timer(): aff_inliers1 = spatial_verification.aff_inliers_from_ellshape2(kpts1_m, kpts2_m, xy_thresh_sqrd) with helpers.Timer(): aff_inliers2 = spatial_verification.aff_inliers_from_ellshape(kpts1_m, kpts2_m, xy_thresh_sqrd) # Homogonize+Normalize xy1_m = kpts1_m[0:2,:] xy2_m = kpts2_m[0:2,:] (xyz_norm1, T1) = spatial_verification.homogo_normalize_pts(xy1_m[:,aff_inliers1]) (xyz_norm2, T2) = spatial_verification.homogo_normalize_pts(xy2_m[:,aff_inliers1]) H_prime = spatial_verification.compute_homog(xyz_norm1, xyz_norm2) H = linalg.solve(T2, H_prime).dot(T1) # Unnormalize Hdet = linalg.det(H) # Estimate final inliers acd1_m = kpts1_m[2:5,:] # keypoint shape matrix [a 0; c d] matches acd2_m = kpts2_m[2:5,:] # Precompute the determinant of lower triangular matrix (a*d - b*c); b = 0 det1_m = acd1_m[0] * acd1_m[2] det2_m = acd2_m[0] * acd2_m[2] # Matrix Multiply xyacd matrix by H # [[A, B, X], # [C, D, Y], # [E, F, Z]] # dot # [(a, 0, x), # (c, d, y), # (0, 0, 1)] # = # [(a*A + c*B + 0*E, 0*A + d*B + 0*X, x*A + y*B + 1*X), # (a*C + c*D + 0*Y, 0*C + d*D + 0*Y, x*C + y*D + 1*Y), # (a*E + c*F + 0*Z, 0*E + d*F + 0*Z, x*E + y*F + 1*Z)] # = # [(a*A + c*B, d*B, x*A + y*B + X), # (a*C + c*D, d*D, x*C + y*D + Y), # (a*E + c*F, d*F, x*E + y*F + Z)] # # IF x=0 and y=0 # = # [(a*A + c*B, d*B, 0*A + 0*B + X), # (a*C + c*D, d*D, 0*C + 0*D + Y), # (a*E + c*F, d*F, 0*E + 0*F + Z)] # = # [(a*A + c*B, d*B, X), # (a*C + c*D, d*D, Y), # (a*E + c*F, d*F, Z)] # --- # A11 = a*A + c*B # A21 = a*C + c*D # A31 = a*E + c*F # A12 = d*B # A22 = d*D # A32 = d*F # A31 = X # A32 = Y # A33 = Z # # det(A) = A11*(A22*A33 - A23*A32) - A12*(A21*A33 - A23*A31) + A13*(A21*A32 - A22*A31) det1_mAt = det1_m * Hdet # Check Error in position and scale xy_sqrd_err = (x1_mAt - x2_m)**2 + (y1_mAt - y2_m)**2 scale_sqrd_err = det1_mAt / det2_m # Check to see if outliers are within bounds xy_inliers = xy_sqrd_err < xy_thresh_sqrd s1_inliers = scale_sqrd_err > scale_thresh_low s2_inliers = scale_sqrd_err < scale_thresh_high _inliers, = np.where(np.logical_and(np.logical_and(xy_inliers, s1_inliers), s2_inliers)) xy1_mHt = transform_xy(H, xy1_m) # Transform Kpts1 to Kpts2-space sqrd_dist_error = np.sum( (xy1_mHt - xy2_m)**2, axis=0) # Final Inlier Errors inliers = sqrd_dist_error < xy_thresh_sqrd df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers1], kpts2_m.T[aff_inliers1], title=title, fignum=2, vert=False) df2.show_matches2(rchip1, rchip2, kpts1_m.T[best_inliers2], kpts2_m.T[aff_inliers2], title=title, fignum=3, vert=False) df2.present(wh=(600,400))
elif cmd in ['fig']: state.fnum_offset += 1 elif cmd in ['smin', 'scale_min']: state.scale_min = int(ans[1]) update_valid() elif cmd in ['smax', 'scale_max']: state.scale_max = int(ans[1]) update_valid() else: print('I dont understand the answer. I hope you know what you are doing'); print(raw) exec raw in globals(), locals() print('>>>') except Exception as ex: print(repr(ex)) if 'doraise' in vars(): raise if __name__ == '__main__': from multiprocessing import freeze_support import draw_func2 as df2 freeze_support() print('[interact] __main__ ') main_locals = dev.dev_main() exec(helpers.execstr_dict(main_locals, 'main_locals')) fnum = 1 interact1(hs, qon_list, fnum) #df2.update() exec(df2.present()) #**df2.OooScreen2()
if __name__ == '__main__': print("[patch] __name__ == 'extract_patch.py'") if not 'hs' in vars(): if len(sys.argv) == 1: db_dir = params.GZ qcx = 111 low = 0 high=6 else: db_dir = params.DEFAULT qcx = helpers.get_arg_after('--qcx', type_=int) low = helpers.get_arg_after('--low', type_=int) high = helpers.get_arg_after('--high', type_=int) if qcx is None: raise Exception('fds') qcx = 1 if low is None: low = 0 if high is None: high = 6 hs = ld2.HotSpotter() hs.load_all(db_dir) #test(hs, qcx) #test3() test2(hs, qcx, low=low, high=high) df2.all_figures_tight_layout() exec(df2.present(no_tile=True))