DrawKeypoints 函数不适用于 OpenCV3.0/Python 2.7

DrawKeypoints function not working on OpenCV3.0/Python 2.7

我已经尝试使用 SIFT 检测器大约一个星期了,当我终于让它工作时,cv2.drawKeypoints() 函数出现错误。代码如下:

import cv2
import numpy as np

img = cv2.imread('\home\gaiarsa\matrix\poste.jpg')
gray = cv2.imread('\home\gaiarsa\matrix\poste.jpg', 1)

sift = cv2.xfeatures2d.SIFT_create()

dummy = np.zeros((1,1))

kp = sift.detect(gray, None)

img = cv2.drawKeypoints(gray, kp,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imwrite('sift_keypoints.jpg', img)

在我 运行 之后我得到错误:

OpenCV Error: Assertion failed (!outImage.empty()) in drawKeypoints, file /home/gaiarsa/opencv/modules/features2d/src/draw.cpp, line 113
Traceback (most recent call last):  
  File "sift.py", line 14, in <module>
    img = cv2.drawKeypoints(gray, kp,dummy, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.error: /home/gaiarsa/opencv/modules/features2d/src/draw.cpp:113: error: (-215) !outImage.empty() in function drawKeypoints

当我尝试从函数参数中删除 dummy 数组时,出现以下错误:

Traceback (most recent call last):
  File "sift.py", line 14, in <module>
    img = cv2.drawKeypoints(gray, kp, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
TypeError: Required argument 'outImage' (pos 3) not found

问题是将 png 文件读取为 jpeg。我一改就成功了

只需检查您的文件名。如果它们是错误的,你将不会得到很好的错误消息,而只会得到 NULL 指针或其他任何东西,程序只会中断,给出这样的消息...

路径不存在!(如果扩展名是.png相同)

'\home\gaiarsa\matrix\poste.jpg'

使用前检查路径:

import os
if not os.path.exists('path/file'):
    print('Incorrect path or file missing')
    exit(0)