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

标签 python opencv orb

我正在尝试使用 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))

关于python - 使用opencv(python)时出现 "orb.detectAndCompute"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514455/

相关文章:

python - 从 HTML 中的脚本元素中提取对象键的值

Python Curve_Fit 指数/幂/对数曲线 - 改善结果

c++ - 视频中的Opencv马赛克

python - Python OpenCV ORB 图像对齐的掩码问题

python - 从不同的 JSON 文件创建语料库

python - python的Hadoop命令

python - 以桌面屏幕作为输入的实时 yolov5 检测

c++ - 检查 OpenCV 矩阵是否为浮点分量类型

python - OpenCV 无法绘制关键点

c++ - 如何改进 orb 特征匹配?