python opencv SIFT不适用于8位图像(JPEG) [英] python opencv SIFT doesn't work for 8 bit images (JPEG)
问题描述
我将SIFT用于所有其他24位JPEG图像,没有任何问题,但是8位总是给我以下错误.
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.
图像在功能cv :: SIFT :: operator()中为空或深度不正确(!= CV_8U)
image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()
有人知道如何处理吗?
这是我的代码:
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)
然后弹出错误图像为空或功能cv :: SIFT :: operator()中的深度不正确(!= CV_8U)".
Then the error "image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()" pops up.
推荐答案
键入此内容后,我只看到了imread上的灰度标记.尝试在读入图像时打印图像,这听起来像是读入可能会静默失败并留下空白的Mats.
cv2.SIFT.detectAndCompute除了8位灰度外都不会使用任何其他内容,因此我不确定您是否确实在24位图像上使用了SIFT没问题.
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.
Python: cv2.SIFT.detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors
因此,在检测和提取之前立即更改为8位灰度:
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)
当然这是一个愚蠢的地方,但是要弄清楚是否需要保留BGR原件,等等.
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.
这篇关于python opencv SIFT不适用于8位图像(JPEG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!