python-2.7 - 错误:(-5) 图像为空或函数 cv::SIFT::operator () 中的深度不正确 (!=CV_8U)

标签 python-2.7 numpy opencv sift

我正在尝试运行在对象检测教程中找到的基本脚本。我已经尝试了所有可以在网上找到的方法,但未能解决。已经尝试了不同的建议方法将图像转换为 CV_U8。也使用 8 位图像作为输入,仍然没有进展。这是代码:

import cv2
import numpy as np
MIN_MATCH_COUNT=30

detector=cv2.SIFT()

FLANN_INDEX_KDITREE=0
flannParam=dict(algorithm=FLANN_INDEX_KDITREE,tree=5)
flann=cv2.FlannBasedMatcher(flannParam,{})

trainImg=cv2.imread("TrainingData/TrainImg.jpeg",0)
trainKP,trainDesc=detector.detectAndCompute(trainImg,None)

cam=cv2.VideoCapture(0)
while True:
    ret, QueryImgBGR=cam.read()
    QueryImg=cv2.cvtColor(QueryImgBGR,cv2.COLOR_BGR2GRAY)
    queryKP,queryDesc=detector.detectAndCompute(QueryImg,None)
    matches=flann.knnMatch(queryDesc,trainDesc,k=2)

    goodMatch=[]
    for m,n in matches:
        if(m.distance<0.75*n.distance):
            goodMatch.append(m)
    if(len(goodMatch)>MIN_MATCH_COUNT):
        tp=[]
        qp=[]
        for m in goodMatch:
            tp.append(trainKP[m.trainIdx].pt)
            qp.append(queryKP[m.queryIdx].pt)
        tp,qp=np.float32((tp,qp))
        H,status=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)
        h,w=trainImg.shape
        trainBorder=np.float32([[[0,0],[0,h-1],[w-1,h-1],[w-1,0]]])
        queryBorder=cv2.perspectiveTransform(trainBorder,H)
        cv2.polylines(QueryImgBGR,[np.int32(queryBorder)],True,(0,255,0),5)
    else:
        print "Not Enough match found- %d/%d"%(len(goodMatch),MIN_MATCH_COUNT)
    cv2.imshow('result',QueryImgBGR)
    if cv2.waitKey(10)==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

这是错误:

enter image description here

我目前正在使用带有 opencv2.4.11 的 conda 环境。

最佳答案

筛选功能(至少具体 .detectAndCompute() )
仅接受具有 8 位整数值的图像。

在图像上使用 sift 之前,使用类似的方法将其转换为 8bitimage8bit = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype('uint8')
详情 void SIFT_Impl::detectAndCompute(InputArray _image, ... )的源代码
包含验证数据是否为 ​​8bit int 的异常检查。

if( image.empty() || image.depth() != CV_8U )
    CV_Error( Error::StsBadArg, "image is empty or has incorrect depth (!=CV_8U)" );

if( !mask.empty() && mask.type() != CV_8UC1 )
    CV_Error( Error::StsBadArg, "mask has incorrect type (!=CV_8UC1)" );

第二个检查只适用于可选的“InputArray _mask”参数,这个掩码只能是CV_8UC1这意味着它必须是 CV_8U 深度并且只有 1 个 channel (灰度如此之多)。

opencv page defining basic structure , 深度级别 CV_8U定义为

CV_8U - 8-bit unsigned integers ( 0..255 )



这意味着输入图像必须由值为 0-255 的 8 位整数定义。

筛选功能仅适用于灰度,因此输入也应为 1 channel 。如果输入有 3 或 4 个 channel 而不是 1 个(例如 RGB,或带 alpha 的 RGB),sift 将在运行其算法之前将输入图像转换为灰度。在使用 sift 之前,您可以自己转换为灰度

有关 opencv 如何定义图像深度 (CV_8U) 或类型 (CV_8UC1) 的更多信息,请访问 here .

附言我是stackoverflow的新手,请让我知道我的格式有任何问题/建议。谢谢!

关于python-2.7 - 错误:(-5) 图像为空或函数 cv::SIFT::operator () 中的深度不正确 (!=CV_8U),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50298329/

相关文章:

python - 修复 Python requests.exception.InvalidURL : Invalid percent-escape sequence 'u2' error?

python - 是否可以从列表中随机*删除*一定百分比/数量的项目,然后将它们*附加*到另一个列表?

访问数组某些部分的 Pythonic 方式

python - 查找无人驾驶车道,代码问题

opengl - glReadPixels 第二次读取失败

python - python中字符串的最后一个位置是什么?

python - VOLTTRON 失败的 Bacnet 代理程序

python - numpy 函数的值

python - 在Python中实现梯度算子

c++ - opencv 3,blobdetection,功能/特性未在detectAndCompute中实现()