我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用cv2.DescriptorExtractor_create()。
def im_detect_and_describe(img, mask=None, detector='dense', descriptor='SIFT', colorspace='gray', step=4, levels=7, scale=np.sqrt(2)): """ Describe image using dense sampling / specific detector-descriptor combination. """ detector = get_detector(detector=detector, step=step, levels=levels, scale=scale) extractor = cv2.DescriptorExtractor_create(descriptor) try: kpts = detector.detect(img, mask=mask) kpts, desc = extractor.compute(img, kpts) if descriptor == 'SIFT': kpts, desc = root_sift(kpts, desc) pts = np.vstack([kp.pt for kp in kpts]).astype(np.int32) return pts, desc except Exception as e: print 'im_detect_and_describe', e return None, None
def obtainSimilarityScore(img1,img2): detector = cv2.FeatureDetector_create("SIFT") descriptor = cv2.DescriptorExtractor_create("SIFT") skp = detector.detect(img1) skp, sd = descriptor.compute(img1, skp) tkp = detector.detect(img2) tkp, td = descriptor.compute(img2, tkp) num1 = 0 for i in range(len(sd)): kp_value_min = np.inf kp_value_2min = np.inf for j in range(len(td)): kp_value = 0 for k in range(128): kp_value = (sd[i][k]-td[j][k]) *(sd[i][k]-td[j][k]) + kp_value if kp_value < kp_value_min: kp_value_2min = kp_value_min kp_value_min = kp_value if kp_value_min < 0.8*kp_value_2min: num1 = num1+1 num2 = 0 for i in range(len(td)): kp_value_min = np.inf kp_value_2min = np.inf for j in range(len(sd)): kp_value = 0 for k in range(128): kp_value = (td[i][k]-sd[j][k]) *(td[i][k]-sd[j][k]) + kp_value if kp_value < kp_value_min: kp_value_2min = kp_value_min kp_value_min = kp_value if kp_value_min < 0.8*kp_value_2min: num2 = num2+1 K1 = num1*1.0/len(skp) K2 = num2*1.0/len(tkp) SimilarityScore = 100*(K1+K2)*1.0/2 return SimilarityScore
def calculate_feature(bin_data): """ calculate the feature data of an image parameter : 'bin_data' is the binary stream format of an image return value : a tuple of ( keypoints, descriptors, (height,width) ) keypoints is like [ pt1, pt2, pt3, ... ] descriptors is a numpy array """ buff=numpy.frombuffer(bin_data,numpy.uint8) img_obj=cv2.imdecode(buff,cv2.CV_LOAD_IMAGE_GRAYSCALE) surf=cv2.FeatureDetector_create("SURF") surf.setInt("hessianThreshold",400) surf_extractor=cv2.DescriptorExtractor_create("SURF") keypoints=surf.detect(img_obj,None) keypoints,descriptors=surf_extractor.compute(img_obj,keypoints) res_keypoints=[] for point in keypoints: res_keypoints.append(point.pt) del buff del surf del surf_extractor del keypoints return res_keypoints,numpy.array(descriptors),img_obj.shape
def __init__(self, detector_name, feat_type): self.feat_type = feat_type self.detector = cv2.FeatureDetector_create(detector_name) self.descriptor_ex = cv2.DescriptorExtractor_create(feat_type)
def main(image_file): image = Image.open(image_file) if image is None: print 'Could not load image "%s"' % sys.argv[1] return image = np.array(image.convert('RGB'), dtype=np.uint8) image = image[:, :, ::-1].copy() winSize = (200, 200) stepSize = 32 roi = extractRoi(image, winSize, stepSize) weight_map, mask_scale = next(roi) samples = [(rect, scale, cv2.cvtColor(window, cv2.COLOR_BGR2GRAY)) for rect, scale, window in roi] X_test = [window for rect, scale, window in samples] coords = [(rect, scale) for rect, scale, window in samples] extractor = cv2.FeatureDetector_create('SURF') detector = cv2.DescriptorExtractor_create('SURF') affine = AffineInvariant(extractor, detector) saved = pickle.load(open('classifier.pkl', 'rb')) feature_transform = saved['pipe'] model = saved['model'] print 'Extracting Affine transform invariant features' affine_invariant_features = affine.transform(X_test) print 'Matching features with template' features = feature_transform.transform(affine_invariant_features) rects = classify(model, features, coords, weight_map, mask_scale) for (left, top, right, bottom) in non_max_suppression_fast(rects, 0.4): cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 0), 10) cv2.rectangle(image, (left, top), (right, bottom), (32, 32, 255), 5) plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.show()