为什么在使用Python的OpenCV中SIFT不能用于8位图像(JPEG)? [英] Why doesn't doesn't SIFT work for 8 bit images (JPEG) in OpenCV with Python?
问题描述
我将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 the 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 $上的灰度标志c $ c>。尝试在读入图像时打印图像,听起来像
imread
可能会自动失败,并留下空白的Mats。
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
除了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屋!