python opencv cv2 基础操作4 Extracting Features

#coding=utf-8

import cv2  #cv2.__version__==3.2.0
import numpy as np

#Extracting Features

#Detecting the corners
# img=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# gray = np.float32(gray)
# dst = cv2.cornerHarris(gray, 4,5, 0.04) # to detect only sharp corners
# # dst = cv2.cornerHarris(gray, 14, 5, 0.04) # to detect soft corners
# # Result is dilated for marking the corners
# dst = cv2.dilate(dst,None)
# # Threshold for an optimal value, it may vary depending on the image.
# img[dst > 0.01*dst.max()] = [0,0,0]
# cv2.imshow('Harris Corners',img)
# cv2.waitKey()
#####################
# corners = cv2.goodFeaturesToTrack(gray, 7, 0.05, 25)
# corners = np.float32(corners)
# for item in corners:
#     x, y = item[0]
#     cv2.circle(img, (x,y), 5, 255, -1)
# cv2.imshow("Top 'k' features", img)
# cv2.waitKey()

####################################################
#SIFT
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
sift = cv2.xfeatures2d.SIFT_create()
keypoints1, descriptors1 = sift.detectAndCompute(gray_image,None)
print('SIFT features',len(keypoints1))
input_image = cv2.drawKeypoints(input_image, keypoints1,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT features', input_image)
# cv2.waitKey()

#######################################################
#SURF
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
surf = cv2.xfeatures2d.SURF_create()
keypoints1, descriptors1 = surf.detectAndCompute(gray_image,None)
print('SURF_create',len(keypoints1))
input_image = cv2.drawKeypoints(input_image, keypoints1,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SURF_create', input_image)
# cv2.waitKey()

#######################################################
#StarDetector
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
StarDetector = cv2.xfeatures2d.StarDetector_create()
keypoints=StarDetector.detect(gray_image)
# keypoints1, descriptors1 = descriptor_sift.detectAndCompute(gray_image,None)
print('Star_create',len(keypoints1))
input_image = cv2.drawKeypoints(input_image, keypoints1,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('Star_create', input_image)
# cv2.waitKey()


#FAST not have descriptor use BRIEF
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
fast = cv2.FastFeatureDetector_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
keypoints=fast.detect(gray_image)
print('FAST_create',len(keypoints))
# compute the descriptors with BRIEF
keypoints, descriptors = brief.compute(gray_image, keypoints)
input_image = cv2.drawKeypoints(input_image, keypoints,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('FAST_create', input_image)
# cv2.waitKey()

#######################################################
#ORB not have descriptor use BRIEF
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
orb = cv2.ORB_create()
keypoints, descriptors1=orb.detectAndCompute(gray_image,None)
# keypoints=org.detect(gray_image)
print('ORB_create',len(keypoints))
input_image = cv2.drawKeypoints(input_image, keypoints,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('ORB_create', input_image)
# cv2.waitKey()

#######################################################
#MSER not have descriptor use BRIEF
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
mser = cv2.MSER_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
keypoints=mser.detect(gray_image)
print('MSER_create:',len(keypoints))
# compute the descriptors with BRIEF
keypoints, descriptors = brief.compute(gray_image, keypoints)
input_image = cv2.drawKeypoints(input_image, keypoints,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('MSER_create', input_image)
# cv2.waitKey()

#######################################################
#BRISK not have descriptor use BRIEF
input_image=cv2.imread('corner.bmp',cv2.IMREAD_ANYCOLOR)
gray_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
dummy = np.zeros((1,1))
brisk = cv2.BRISK_create()
keypoints, descriptors1=brisk.detectAndCompute(gray_image,None)
input_image = cv2.drawKeypoints(input_image, keypoints,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('BRISK_create', input_image)
cv2.waitKey()

你可能感兴趣的:(opencv,python)