def query_image(image, refName, show=False): if True: IMG = cv2.imread(image, 0) exif_focal = getFocal(image) img_heigh, img_width = IMG.shape[:2] query_kps_cv2, query_features = detector.detectAndCompute( IMG, None) ref = KnnIndex() ref.load(refName) dstPoints = [] srcPoints = [] obj_points = ref.query(query_features, K=8, mapped=True) for it, kp in enumerate(query_kps_cv2): L = obj_points[it] for o_pt in L: dstPoints.append(o_pt[0:3]) srcPoints.append(kp.pt) dstPoints2d = np.array(dstPoints, np.float32)[:, 0:2].copy() srcPoints = np.array(srcPoints, np.float32) H, mask = cv2.findHomography(srcPoints, dstPoints2d, cv2.RANSAC, 5.0) obj_pt = [] img_pt = [] for it, m in enumerate(mask): if m: obj_pt.append(dstPoints[it]) img_pt.append(srcPoints[it]) if True: obj_pt = np.array(obj_pt) img_pt = np.array(img_pt) return obj_pt, img_pt
def query_image(image,refName,show=False): if True: IMG=cv2.imread(image,0) exif_focal=getFocal(image) img_heigh,img_width=IMG.shape[:2] query_kps_cv2, query_features=detector.detectAndCompute(IMG,None ) ref=KnnIndex() ref.load(refName) dstPoints=[] srcPoints=[] obj_points=ref.query(query_features,K=8,mapped=True) for it,kp in enumerate(query_kps_cv2): L=obj_points[it] for o_pt in L: dstPoints.append(o_pt[0:3]) srcPoints.append(kp.pt) dstPoints2d=np.array(dstPoints,np.float32)[:,0:2].copy() srcPoints=np.array(srcPoints,np.float32) H,mask = cv2.findHomography(srcPoints, dstPoints2d,cv2.RANSAC,5.0) obj_pt=[] img_pt=[] for it,m in enumerate(mask): if m: obj_pt.append(dstPoints[it]) img_pt.append(srcPoints[it]) if True: obj_pt=np.array(obj_pt) img_pt=np.array(img_pt) return obj_pt,img_pt
def build(image, width=None, height=None): if not width and not height: name, x, y, _discard = re.split('x|y|\.', image) width = int(x) height = int(y) print name print 'width ', width, ' mm' print 'height ', height, ' mm' if not name: name = image.split('.')[0] + '.reference' IMG = cv2.imread(image, 0) img_heigh, img_width = IMG.shape[:2] x_scale = img_width / width y_scale = img_heigh / height kps_cv2, features = detector.detectAndCompute(IMG, None) obj_points = [] for kp in kps_cv2: _x, _y = kp.pt _x = _x / x_scale _y = _y / y_scale _z = 0. _o = kp.angle _s = kp.octave _v = [_x, _y, _z, _s, _o] obj_points.append(_v) obj_points = np.array(obj_points, np.float32) Index = KnnIndex() Index.train(features) Index.set_dataset_key_map(obj_points) Index.save(name) return name
def build(image,width=None,height=None): if not width and not height: name,x,y,_discard=re.split('x|y|\.',image) width=int(x) height=int(y) print name print 'width ',width,' mm' print 'height ',height,' mm' if not name: name=image.split('.')[0]+'.reference' IMG=cv2.imread(image,0) img_heigh,img_width=IMG.shape[:2] x_scale=img_width/width y_scale=img_heigh/height kps_cv2, features=detector.detectAndCompute(IMG ,None) obj_points=[] for kp in kps_cv2: _x,_y=kp.pt _x=_x/x_scale _y=_y/y_scale _z=0. _o=kp.angle _s=kp.octave _v=[_x,_y,_z,_s,_o] obj_points.append(_v) obj_points=np.array(obj_points,np.float32) Index=KnnIndex() Index.train(features) Index.set_dataset_key_map(obj_points) Index.save(name) return name