使用opencv (python)时"orb.detectAndCompute"时出错

Error when "orb.detectAndCompute" when using opencv (python)

我正在尝试使用 ORB 来查找先前生成的频谱图的关键点和描述符。我有这个代码:

start = time.time()
x = 1
img = range(101)
imgname = range(101)

# Directory Images
os.chdir("/home/undead/Documents/TempSongSpectro/") #1,2,3
for file in glob.glob("*.png"):
    img[x] = cv2.imread(file, 0)  # trainImage
    imgname[x] = os.path.splitext(file)[0]
# print "%s: %d " % (os.path.splitext(file)[0],(x))
x = x + 1

# Initiate ORB detector
orb = cv2.ORB_create(3000)

# find the keypoints and descriptors with ORB
a = 1
des = range(101)
kp = range(101)

for a in range(1, 101):
    kp[a], des[a] = orb.detectAndCompute(img[a], None)

end = time.time()
print("Initialize time: %f seconds" % (end - start))

但是,我收到错误消息:

OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /home/undead/opencv/opencv-3.2.0/modules/imgproc/src/color.cpp, line 9710

Traceback (most recent call last):
  File "/home/undead/PycharmProjects/KavTest/Test3.py", line 37, in <module>
    kp[a], des[a] = orb.detectAndCompute(img[a], None)

cv2.error: /home/undead/opencv/opencv-3.2.0/modules/imgproc/src/color.cpp:9710: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor

据我了解,错误可能与kp、des或img的数据类型有关,但我不太确定如何解决它。有人可以提供一些帮助吗?

我认为您的代码有问题。

这是我的代码几乎相同。

start = time.time()

imgnames = glob.glob("/home/undead/Documents/TempSongSpectro/*.png")
#imgnames = imgnames[:100]

sz = len(imgnames)
imgs = list(range(sz))

for i, name in enumerate(imgnames, start=0):
    imgs[i] =  cv2.imread(name, 0)

orb = cv2.ORB_create(3000)

# find the keypoints and descriptors with ORB
des = list(range(sz))
kp = list(range(sz))

for i in range(sz):
    kp[i], des[i] = orb.detectAndCompute(img[i], None)

end = time.time()
print("Initialize time: {:.4f} seconds".format(end - start))