python opencv SIFT doesn't work for 8 bit images (JPEG)

自作多情 提交于 2019-12-24 04:22:16

问题


I used SIFT for all my other 24 bit JPEG images without any problems, however, the 8 bit one always give me this following error.

image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()

Does anyone know how to deal with it?

Here is my code:

import cv2 
import numpy as np 
import os 
import glob
import scipy.cluster
os.chdir('\mydirectory')
images = []

for infile in glob.glob('./*.jpg'):
  pic = cv2.imread(infile,0)
  images.append(pic)

my_set = images
descriptors = np.array([])
feaL=np.array([])

for pic in my_set:
  kp, des = cv2.SIFT().detectAndCompute(pic, None)
  feaL=np.append(feaL,des.shape[0])
  descriptors = np.append(descriptors, des)

Then the error "image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()" pops up.


回答1:


EDIT: After typing this I just saw the grayscale flag on imread. Try printing the images as they are read in, it sounds like imread may be silently failing and leaving you with empty Mats.

cv2.SIFT.detectAndCompute never takes anything other than 8-bit grayscale, so I'm not sure that you actually did use SIFT on a 24 bit image without problems.

cv2.SIFT.detectAndCompute

Python: cv2.SIFT.detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors

So to change to 8 bit grayscale immediately prior to detection and extraction:

for pic in my_set:
    pic = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
    kp, des = cv2.SIFT().detectAndCompute(pic, None)

Of course that is a dumb place to put it, but it's up to you to figure out if you need to keep the BGR originals or not, etc.




回答2:


I answered a different user that encountered the same error while using cv2.SIFT().detectAndCompute() in-depth in this stackoverflow post.

The sift functions (at least specifically .detectAndCompute() )
ONLY accepts images with 8 bit integer values.

Before using sift on an image, convert it into 8bit using something like
image8bit = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')



来源:https://stackoverflow.com/questions/28597580/python-opencv-sift-doesnt-work-for-8-bit-images-jpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!