I am using openCV 3.1.0 in Python 2.7.
I have successfully detected keypoints using the FAST detector. Now I would like to compute the features for each of these points using the FAST.compute() method.
However I did not manage to have this function running, it returns :
Traceback (most recent call last):
File "<ipython-input-82-1b8e23b25e3b>", line 1, in <module>
Fast.compute(Image8,Kp)
error: ..\..\..\modules\features2d\src\feature2d.cpp:144: error: (-213) in function cv::Feature2D::detectAndCompute
I haven't manage to run the detectAndCompute method work neither, even by playing the keypoints properties (like class_id...). Thanks for the help,
Here is the full code:
# -*- coding: utf-8 -*-
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Function for 16-bit to 8-bit conversion
def map_uint16_to_uint8(img, lower_bound=None, upper_bound=None):
'''
Map a 16-bit image trough a lookup table to convert it to 8-bit.
Parameters
----------
img: numpy.ndarray[np.uint16]
image that should be mapped
lower_bound: int, optional
lower bound of the range that should be mapped to ``[0, 255]``,
value must be in the range ``[0, 65535]`` and smaller than `upper_bound`
(defaults to ``numpy.min(img)``)
upper_bound: int, optional
upper bound of the range that should be mapped to ``[0, 255]``,
value must be in the range ``[0, 65535]`` and larger than `lower_bound`
(defaults to ``numpy.max(img)``)
Returns
-------
numpy.ndarray[uint8]
'''
if not(0 <= lower_bound < 2**16) and lower_bound is not None:
raise ValueError(
'"lower_bound" must be in the range [0, 65535]')
if not(0 <= upper_bound < 2**16) and upper_bound is not None:
raise ValueError(
'"upper_bound" must be in the range [0, 65535]')
if lower_bound is None:
lower_bound = np.min(img)
if upper_bound is None:
upper_bound = np.max(img)
if lower_bound >= upper_bound:
raise ValueError(
'"lower_bound" must be smaller than "upper_bound"')
lut = np.concatenate([
np.zeros(lower_bound, dtype=np.uint16),
np.linspace(0, 255, upper_bound - lower_bound).astype(np.uint16),
np.ones(2**16 - upper_bound, dtype=np.uint16) * 255
])
return lut[img].astype(np.uint8)
# Main
Image = cv2.imread("C:\Users\Laurent Thomas\Documents\Acquifer\DataSet\IM03_BISCHOFF_DORSAL_2ndGO\BrightField\WE00006---A006--PO01--LO001--CO6--SL001--PX32500--PW0040--IN0020--TM246--X059308--Y011163--Z215816--T1373998365.tif",-1)
Image8 = map_uint16_to_uint8(Image) # Conversion to 8-bit for FAST detection
ImageRGB = cv2.cvtColor(Image8,cv2.COLOR_GRAY2RGB) # RGB necessary if we want to see the detected points in color
# Get Keypoints
Fast = cv2.FastFeatureDetector_create()
Kp = Fast.detect(Image8)
# Overlay Keypoints on image
ImagePoint = cv2.drawKeypoints(ImageRGB,Kp,ImageRGB,color=(255,0,0),flags=4) # flag 4 to be able to plaw with the size of the detected spot
# View Overlay
plt.imshow(ImagePoint)
# Compute features
Features = Fast.compute(Image8,Kp)
Well apparently the FAST descriptor is not implemented like for the STAR descriptor. Only the detection works : How to use STAR detector in openCV 3 with python?