#!/usr/bin/env python2.7
from __future__ import absolute_import, division, print_function
import numpy as np
# Hesaff
from pyhesaff.tests import pyhestest
import pyhesaff
# Tools
import utool
from plottool import draw_func2 as df2
from plottool.viz_keypoints import _annotate_kpts, show_keypoints
from plottool.viz_featrow import draw_feat_row
import plottool
import vtool.patch as ptool
[docs]def TEST_keypoint(imgBGR, img_fpath, kpts, desc, sel, fnum=1, figtitle=''):
#----------------------#
# --- Extract Data --- #
#----------------------#
kp = kpts[sel]
# Extract patches, gradients, and orientations
print('[rotinvar] 1) Extract patch, gradients, and orientations')
wpatch, wkp = ptool.get_warped_patch(imgBGR, kp, gray=True)
gradx, grady = ptool.patch_gradient(wpatch)
gmag = ptool.patch_mag(gradx, grady)
gori = ptool.patch_ori(gradx, grady)
# Get orientation histogram
print('[rotinvar] 2) Get orientation histogram')
hist, centers = ptool.get_orientation_histogram(gori)
#----------------------#
# --- Draw Results --- #
#----------------------#
f1_loc = TEST_figure1(wpatch, gradx, grady, gmag, gori, hist, centers, fnum=fnum)
df2.set_figtitle(figtitle + 'Dominant Orienation Extraction')
TEST_figure2(imgBGR, kpts, desc, sel, fnum=fnum + 1)
df2.set_figtitle(figtitle)
# TEST_figure2(imgBGR, kpts2, Desc2, sel, fnum=fnum + 2)
# df2.set_figtitle('Rotation Invariant')
#df2.draw_keypoint_gradient_orientations(imgBGR, kp=kpts2[sel],
# sift=desc[sel], mode='vec',
# fnum=4)
#df2.draw_vector_field(gradx, grady, pnum=(1, 1, 1), fnum=4)
#df2.draw_kpts2(np.array([wkp]), sifts=desc[sel:sel + 1], ori=True)
return locals()
if __name__ == '__main__':
# Read data
print('[rotinvar] loading test data')
img_fpath = pyhestest.get_test_image()
[kpts1], [desc1] = pyhesaff.detect_kpts_list([img_fpath], rotation_invariance=False)
[kpts2], [desc2] = pyhesaff.detect_kpts_list([img_fpath], rotation_invariance=True)
np.set_printoptions(threshold=5000, linewidth=5000, precision=8, suppress=True)
print('kpts1.shape = %r' % (kpts1.shape,))
print('kpts2.shape = %r' % (kpts2.shape,))
print('desc1.shape = %r' % (desc1.shape,))
print('desc2.shape = %r' % (desc2.shape,))
print('\n----\n'.join([str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1[0:10], kpts2[0:10])]))
n = 4
clip = min(len(kpts1), n)
fxs = np.array(pyhestest.spaced_elements2(kpts2, n).tolist())
print('fxs=%r' % fxs)
kpts1 = kpts1[fxs]
kpts2 = kpts2[fxs]
desc1 = desc1[fxs]
desc2 = desc2[fxs]
print('\n----\n'.join([str(k1) + '\n' + str(k2) for k1, k2 in zip(kpts1, kpts2)]))
imgBGR = pyhestest.cv2.imread(img_fpath)
sel = min(len(kpts1) - 1, 3)
TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel, fnum=1, figtitle='Downward Rotation')
TEST_keypoint(imgBGR, img_fpath, kpts2, desc2, sel, fnum=9001, figtitle='Adapted Rotation')
#locals_ = TEST_keypoint(imgBGR, img_fpath, kpts1, desc1, sel)
#exec(utool.execstr_dict(locals_, 'locals_'))
#exec(utool.execstr_dict(f1_loc, 'f1_loc')) # NOQA
#pinteract.interact_keypoints(imgBGR, kpts2, desc, arrow=True, rect=True)
exec(df2.present())