Created
February 25, 2018 02:31
-
-
Save jkjung-avt/7c3bc57973c51d576157c33b1abbe16b to your computer and use it in GitHub Desktop.
Example OpenCV feature extracteor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
Hi there am getting this error while executing on multiple jpeg files
AttributeError: 'NoneType' object has no attribute 'flatten'