Merge pull request #5548 from berak:patch-2
This commit is contained in:
commit
a91dcb015d
@ -48,6 +48,8 @@ BRIEF in OpenCV
|
|||||||
|
|
||||||
Below code shows the computation of BRIEF descriptors with the help of CenSurE detector. (CenSurE
|
Below code shows the computation of BRIEF descriptors with the help of CenSurE detector. (CenSurE
|
||||||
detector is called STAR detector in OpenCV)
|
detector is called STAR detector in OpenCV)
|
||||||
|
|
||||||
|
note, that you need [opencv contrib](https://github.com/Itseez/opencv_contrib)) to use this.
|
||||||
@code{.py}
|
@code{.py}
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
@ -55,11 +57,11 @@ from matplotlib import pyplot as plt
|
|||||||
|
|
||||||
img = cv2.imread('simple.jpg',0)
|
img = cv2.imread('simple.jpg',0)
|
||||||
|
|
||||||
# Initiate STAR detector
|
# Initiate FAST detector
|
||||||
star = cv2.FeatureDetector_create("STAR")
|
star = cv2.xfeatures2d.StarDetector_create()
|
||||||
|
|
||||||
# Initiate BRIEF extractor
|
# Initiate BRIEF extractor
|
||||||
brief = cv2.DescriptorExtractor_create("BRIEF")
|
brief = cv2.BriefDescriptorExtractor_create()
|
||||||
|
|
||||||
# find the keypoints with STAR
|
# find the keypoints with STAR
|
||||||
kp = star.detect(img,None)
|
kp = star.detect(img,None)
|
||||||
|
@ -101,7 +101,7 @@ from matplotlib import pyplot as plt
|
|||||||
img = cv2.imread('simple.jpg',0)
|
img = cv2.imread('simple.jpg',0)
|
||||||
|
|
||||||
# Initiate FAST object with default values
|
# Initiate FAST object with default values
|
||||||
fast = cv2.FastFeatureDetector()
|
fast = cv2.FastFeatureDetector_create()
|
||||||
|
|
||||||
# find and draw the keypoints
|
# find and draw the keypoints
|
||||||
kp = fast.detect(img,None)
|
kp = fast.detect(img,None)
|
||||||
|
@ -44,7 +44,7 @@ img1 = cv2.imread('box.png',0) # queryImage
|
|||||||
img2 = cv2.imread('box_in_scene.png',0) # trainImage
|
img2 = cv2.imread('box_in_scene.png',0) # trainImage
|
||||||
|
|
||||||
# Initiate SIFT detector
|
# Initiate SIFT detector
|
||||||
sift = cv2.SIFT()
|
sift = cv2.xfeatures2d.SIFT_create()
|
||||||
|
|
||||||
# find the keypoints and descriptors with SIFT
|
# find the keypoints and descriptors with SIFT
|
||||||
kp1, des1 = sift.detectAndCompute(img1,None)
|
kp1, des1 = sift.detectAndCompute(img1,None)
|
||||||
@ -78,7 +78,7 @@ if len(good)>MIN_MATCH_COUNT:
|
|||||||
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
|
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
|
||||||
matchesMask = mask.ravel().tolist()
|
matchesMask = mask.ravel().tolist()
|
||||||
|
|
||||||
h,w = img1.shape
|
h,w,d = img1.shape
|
||||||
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
|
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
|
||||||
dst = cv2.perspectiveTransform(pts,M)
|
dst = cv2.perspectiveTransform(pts,M)
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ from matplotlib import pyplot as plt
|
|||||||
|
|
||||||
img = cv2.imread('simple.jpg',0)
|
img = cv2.imread('simple.jpg',0)
|
||||||
|
|
||||||
# Initiate STAR detector
|
# Initiate ORB detector
|
||||||
orb = cv2.ORB()
|
orb = cv2.ORB_create()
|
||||||
|
|
||||||
# find the keypoints with ORB
|
# find the keypoints with ORB
|
||||||
kp = orb.detect(img,None)
|
kp = orb.detect(img,None)
|
||||||
|
@ -104,7 +104,7 @@ greater than 0.8, they are rejected. It eliminaters around 90% of false matches
|
|||||||
|
|
||||||
So this is a summary of SIFT algorithm. For more details and understanding, reading the original
|
So this is a summary of SIFT algorithm. For more details and understanding, reading the original
|
||||||
paper is highly recommended. Remember one thing, this algorithm is patented. So this algorithm is
|
paper is highly recommended. Remember one thing, this algorithm is patented. So this algorithm is
|
||||||
included in Non-free module in OpenCV.
|
included in [the opencv contrib repo](https://github.com/Itseez/opencv_contrib)
|
||||||
|
|
||||||
SIFT in OpenCV
|
SIFT in OpenCV
|
||||||
--------------
|
--------------
|
||||||
@ -119,7 +119,7 @@ import numpy as np
|
|||||||
img = cv2.imread('home.jpg')
|
img = cv2.imread('home.jpg')
|
||||||
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
sift = cv2.SIFT()
|
sift = cv2.xfeatures2d.SIFT_create()
|
||||||
kp = sift.detect(gray,None)
|
kp = sift.detect(gray,None)
|
||||||
|
|
||||||
img=cv2.drawKeypoints(gray,kp)
|
img=cv2.drawKeypoints(gray,kp)
|
||||||
@ -151,7 +151,7 @@ Now to calculate the descriptor, OpenCV provides two methods.
|
|||||||
|
|
||||||
We will see the second method:
|
We will see the second method:
|
||||||
@code{.py}
|
@code{.py}
|
||||||
sift = cv2.SIFT()
|
sift = cv2.xfeatures2d.SIFT_create()
|
||||||
kp, des = sift.detectAndCompute(gray,None)
|
kp, des = sift.detectAndCompute(gray,None)
|
||||||
@endcode
|
@endcode
|
||||||
Here kp will be a list of keypoints and des is a numpy array of shape
|
Here kp will be a list of keypoints and des is a numpy array of shape
|
||||||
|
@ -80,7 +80,7 @@ examples are shown in Python terminal since it is just same as SIFT only.
|
|||||||
|
|
||||||
# Create SURF object. You can specify params here or later.
|
# Create SURF object. You can specify params here or later.
|
||||||
# Here I set Hessian Threshold to 400
|
# Here I set Hessian Threshold to 400
|
||||||
>>> surf = cv2.SURF(400)
|
>>> surf = cv2.xfeatures2d.SURF_create(400)
|
||||||
|
|
||||||
# Find keypoints and descriptors directly
|
# Find keypoints and descriptors directly
|
||||||
>>> kp, des = surf.detectAndCompute(img,None)
|
>>> kp, des = surf.detectAndCompute(img,None)
|
||||||
@ -92,12 +92,12 @@ examples are shown in Python terminal since it is just same as SIFT only.
|
|||||||
While matching, we may need all those features, but not now. So we increase the Hessian Threshold.
|
While matching, we may need all those features, but not now. So we increase the Hessian Threshold.
|
||||||
@code{.py}
|
@code{.py}
|
||||||
# Check present Hessian threshold
|
# Check present Hessian threshold
|
||||||
>>> print surf.hessianThreshold
|
>>> print surf.getHessianThreshold()
|
||||||
400.0
|
400.0
|
||||||
|
|
||||||
# We set it to some 50000. Remember, it is just for representing in picture.
|
# We set it to some 50000. Remember, it is just for representing in picture.
|
||||||
# In actual cases, it is better to have a value 300-500
|
# In actual cases, it is better to have a value 300-500
|
||||||
>>> surf.hessianThreshold = 50000
|
>>> surf.setHessianThreshold(50000)
|
||||||
|
|
||||||
# Again compute keypoints and check its number.
|
# Again compute keypoints and check its number.
|
||||||
>>> kp, des = surf.detectAndCompute(img,None)
|
>>> kp, des = surf.detectAndCompute(img,None)
|
||||||
@ -119,10 +119,10 @@ on wings of butterfly. You can test it with other images.
|
|||||||
Now I want to apply U-SURF, so that it won't find the orientation.
|
Now I want to apply U-SURF, so that it won't find the orientation.
|
||||||
@code{.py}
|
@code{.py}
|
||||||
# Check upright flag, if it False, set it to True
|
# Check upright flag, if it False, set it to True
|
||||||
>>> print surf.upright
|
>>> print surf.getUpright()
|
||||||
False
|
False
|
||||||
|
|
||||||
>>> surf.upright = True
|
>>> surf.setUpright(True)
|
||||||
|
|
||||||
# Recompute the feature points and draw it
|
# Recompute the feature points and draw it
|
||||||
>>> kp = surf.detect(img,None)
|
>>> kp = surf.detect(img,None)
|
||||||
@ -143,7 +143,7 @@ Finally we check the descriptor size and change it to 128 if it is only 64-dim.
|
|||||||
64
|
64
|
||||||
|
|
||||||
# That means flag, "extended" is False.
|
# That means flag, "extended" is False.
|
||||||
>>> surf.extended
|
>>> surf.getExtended()
|
||||||
False
|
False
|
||||||
|
|
||||||
# So we make it to True to get 128-dim descriptors.
|
# So we make it to True to get 128-dim descriptors.
|
||||||
|
Loading…
Reference in New Issue
Block a user