【问题标题】:OpenCV SURF for live streaming from webcam in PythonOpenCV SURF 用于在 Python 中从网络摄像头实时流式传输
【发布时间】:2014-11-17 09:29:56
【问题描述】:

我正在使用 python 在 opencv 中进行 surf 实现,它将检测给定图像中的模板。我已经修改了代码,以便它将从连接的网络摄像头获取视频并转换为图像,然后在其上应用冲浪。以下是修改后的代码。

import cv2
import numpy as np


cap = cv2.VideoCapture(0)

while(True):

        ret ,img = cap.read()

# Convert them to grayscale
        imgg =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# SURF extraction
    surf = cv2.SURF()
    kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False)

# Setting up samples and responses for kNN
    samples = np.array(descritors) 
    responses = np.arange(len(kp),dtype = np.float32)

# kNN training
    knn = cv2.KNearest()
    knn.train(samples,responses)

# Now loading a template image and searching for similar keypoints
    template = cv2.imread('template.png')
    templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
    keys,desc = surf.detect(templateg,None,useProvidedKeypoints = False)

    for h,des in enumerate(desc):
            des = np.array(des,np.float32).reshape((1,128))
            retval, results, neigh_resp, dists = knn.find_nearest(des,1)
            res,dist =  int(results[0][0]),dists[0][0]

            if dist<0.1: # draw matched keypoints in red color
                color = (0,0,255)
            else:  # draw unmatched in blue color
                print dist
                color = (255,0,0)

    #Draw matched key points on original image
            x,y = kp[res].pt
            center = (int(x),int(y))
            cv2.circle(img,center,2,color,-1)

    #Draw matched key points on template image
            x,y = keys[h].pt
            center = (int(x),int(y))
            cv2.circle(template,center,2,color,-1)

    cv2.imwrite('img',img)
    cv2.imwrite('tm',template)
    cv2.waitKey(0)
cap.release()

但是即将到来的错误是 knn.train(样本,响应) TyepError: 不支持数据类型 = 17

有人对此有任何想法吗?

【问题讨论】:

    标签: python opencv surf


    【解决方案1】:

    CV 可能需要常规数组,但您传递的是 numpy 数组。试试这个

    knn.train(samples.tolist(),responses.tolist())
    

    【讨论】:

    • 错误仍然存​​在。TypeError: &lt;unknown&gt; is not a numpy array
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 2018-03-12
    • 2014-06-18
    • 2012-06-16
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多