Skip to content

Instantly share code, notes, and snippets.

@jkjung-avt
Created February 25, 2018 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkjung-avt/7c3bc57973c51d576157c33b1abbe16b to your computer and use it in GitHub Desktop.
Save jkjung-avt/7c3bc57973c51d576157c33b1abbe16b to your computer and use it in GitHub Desktop.
Example OpenCV feature extracteor
# Feature extractor
def extract_features(image_path, vector_size=32):
image = imread(image_path, mode="RGB")
try:
# Using KAZE, cause SIFT, ORB and other was moved to additional module
# which is adding addtional pain during install
alg = cv2.KAZE_create()
# Dinding image keypoints
kps = alg.detect(image)
# Getting first 32 of them.
# Number of keypoints is varies depend on image size and color pallet
# Sorting them based on keypoint response value(bigger is better)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
# computing descriptors vector
kps, dsc = alg.compute(image, kps)
# Flatten all of them in one big vector - our feature vector
dsc = dsc.flatten()
# Making descriptor of same size
# Descriptor vector size is 64
needed_size = (vector_size * 64)
if dsc.size < needed_size:
# if we have less the 32 descriptors then just adding zeros at the
# end of our feature vector
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
except cv2.error as e:
print 'Error: ', e
return None
return dsc
@antri21
Copy link

antri21 commented May 7, 2019

Hi there am getting this error while executing on multiple jpeg files

AttributeError: 'NoneType' object has no attribute 'flatten'

Screenshot 2019-05-08 at 12 17 16 AM

@tomups
Copy link

tomups commented Jul 2, 2019

@AnTri

Hi there am getting this error while executing on multiple jpeg files

AttributeError: 'NoneType' object has no attribute 'flatten'

Got this too, I think because of using too small images. I fixed this by increasing the threshold in KAZE_create:

alg = cv2.KAZE_create(threshold=0.0001)

Try different thresholds until you have no failing images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment