I'm trying to use OpenCV to extract SURF descriptors from an image. I'm using OpenCV 2.4 and Python 2.7, but am struggling to find any documentation that provides any information about how to use the functions. I've been able to use the following code to extract features, but I can't find any sensible way to extract descriptors:
import cv2
img = cv2.imread("im1.jpg")
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
surf = cv2.FeatureDetector_create('SURF')
detector = cv2.GridAdaptedFeatureDetector(surf, 50) # max number of features
fs = detector.detect(img2)
The code I tried for extracting descriptors is:
import cv2
img = cv2.imread("im3.jpg")
sd = cv2.FeatureDetector_create("SURF")
surf = cv2.DescriptorExtractor_create("SURF")
keypoints = []
fs = surf.compute(img, keypoints) # returns empty result
sd.detect(img) # segmentation faults
Does anyone have any sample code that does this kind of thing, or pointers to any documentation that provides samples?
OpenCV can be utilised to solve image classification problems. The OpenCV library offers a Deep Neural Network (DNN) module, which facilitates the use of deep learning related functions within OpenCV.
OpenCV is a great tool for image processing and performing computer vision tasks. It is an open-source library that can be used to perform tasks like face detection, objection tracking, landmark detection, and much more. It supports multiple languages including python, java C++.
What is OpenCV-Python? It's a package that contains pre-built OpenCV with dependencies and Python bindings, so there's no need to install OpenCV separately. Thanks so much to package maintainer Olli-Pekka Heinisuo (@skvark), who developed this solution and kindly agreed to transfer it to OpenCV.
OpenCV 3 works with Python 2.7, 3.4, 3.5, 3.6 and 3.7. The unofficial OpenCV PyPi wheels work for Linux, Mac and Windows.
Here's an example of some code I've written for extracting SURF features using Python 2.7 and OpenCV 2.4.
im2 = cv2.imread(imgPath)
im = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
surfDetector = cv2.FeatureDetector_create("SURF")
surfDescriptorExtractor = cv2.DescriptorExtractor_create("SURF")
keypoints = surfDetector.detect(im)
(keypoints, descriptors) = surfDescriptorExtractor.compute(im,keypoints)
This works and returns a set of descriptors. Unfortunately since cv2.SURF() doesn't work in 2.4, you have to go through this tedious process.
Here is a simple bit of code I did for uni fairly recently. It captures the image from a camera and displays the detected keypoints on the output image in real-time. I hope it is of use to you.
There is some documentation here.
Code:
import cv2
#Create object to read images from camera 0
cam = cv2.VideoCapture(0)
#Initialize SURF object
surf = cv2.SURF(85)
#Set desired radius
rad = 2
while True:
#Get image from webcam and convert to greyscale
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Detect keypoints and descriptors in greyscale image
keypoints, descriptors = surf.detect(gray, None, False)
#Draw a small red circle with the desired radius
#at the (x, y) location for each feature found
for kp in keypoints:
x = int(kp.pt[0])
y = int(kp.pt[1])
cv2.circle(img, (x, y), rad, (0, 0, 255))
#Display colour image with detected features
cv2.imshow("features", img)
#Sleep infinite loop for ~10ms
#Exit if user presses <Esc>
if cv2.waitKey(10) == 27:
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With