Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonK-最近邻类型错误:不支持样本数据类型=17_Python_Opencv_Sift_Knn - Fatal编程技术网

pythonK-最近邻类型错误:不支持样本数据类型=17

pythonK-最近邻类型错误:不支持样本数据类型=17,python,opencv,sift,knn,Python,Opencv,Sift,Knn,我尝试使用SIFT对一些图像进行分类,以检测和计算关键点和描述符,然后使用KNN对它们进行分类: 这是我的小代码: import os import cv2 ## Prepare images files rootpath = '/Some/Directory' files = [] for filedir, dirs, filess in os.walk(rootpath): for filename in filess: pathfile = os.path.joi

我尝试使用SIFT对一些图像进行分类,以检测和计算关键点和描述符,然后使用KNN对它们进行分类:

这是我的小代码:

import os
import cv2

## Prepare images files
rootpath = '/Some/Directory'
files = []
for filedir, dirs, filess in os.walk(rootpath):
    for filename in filess:
        pathfile = os.path.join(filedir, filename)
        files.append(pathfile) 

## Detect keypoints and compute descriptors for train images
kp_train = []
dsc_train = []
for file in files:
    ima = cv2.imread(file)
    gray=cv2.cvtColor(ima,cv2.COLOR_BGR2GRAY)
    kpts, des = sift.detectAndCompute(gray, None) 
    kp_train.append(kpts)
    dsc_train.append(des)

## Train knn
dsc_train = np.array(dsc_train)
responses = np.arange(len(kp_train),dtype = np.float32)
knn = cv2.ml.KNearest_create()
knn.train(dsc_train, cv2.ml.ROW_SAMPLE, responses)
但我有点被下一个错误缠住了

>>> knn.train(dsc_train,cv2.ml.ROW_SAMPLE,responses)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dsc_train data type = 17 is not supported
>knn.train(dsc\u train,cv2.ml.ROW\u样本,响应)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
类型错误:不支持dsc\U列车数据类型=17
文件是一个包含10个图像的列表,因此循环检测并计算每个图像的关键点和描述符。我给你一些图片。谢谢


无论如何,我觉得你少了一行

import numpy as np

在代码的某个地方

对于我来说,下面附上了实际再现您的问题的代码(包括由于我的CV 2.4.12版本而进行的一些更改)

然而,我担心您选择的方法根本不适用于K-最近邻(KNN)。KNN测量属于不同样本的特征向量之间的距离。然而,对于所有特征向量,向量的每个分量需要具有相同的含义(例如,一个特征是图像的平均亮度值)。因此,该功能始终需要显示在向量的相同位置

在SUFT中,您正在创建不同图像关键点的坐标。最重要的是,每个图像的特征向量长度不同,因此无法应用kNN。显然,这些坐标作为用于比较不同图像的特征向量的一部分没有意义

import os
import cv2 #Using CV 2.4.12
import numpy as np

## Prepare images files
rootpath = 'images/'
files = []
for filedir, dirs, filess in os.walk(rootpath):
    for filename in filess:
        pathfile = os.path.join(filedir, filename)
        files.append(pathfile) 

print files

## Detect keypoints and compute descriptors for train images
kp_train = []
dsc_train = []
sift = cv2.SIFT()
for file in files:
    ima = cv2.imread(file)
    print file
    gray=cv2.cvtColor(ima,cv2.COLOR_BGR2GRAY)
    kpts, des = sift.detectAndCompute(gray, None) #sift = cv2.xfeatures2d.SIFT_create()
    kp_train.append(kpts)
    dsc_train.append(des)

## Train knn
dsc_train = np.array(dsc_train)
responses = np.arange(len(kp_train),dtype = np.float32)
knn = cv2.KNearest()

#Next line does not work:
knn.train(dsc_train, responses)

你能用三张图片来演示一组简化的示例日期吗?如果代码完整(包括导入语句、文件名等)且数据可用,我们可以复制它,你可能会得到更快的响应。@tfv我编辑了这篇文章。检查一下。感谢您的建议,我的第一个想法是使用特性(例如纹理特性)而不是关键点来处理这类图像,但我会试着看看。您使用的是什么版本的cv2?哦,我明白了,是的,我忘了在代码中包含这些行,我的错误。谢谢,@tfv帮我澄清了很多事情,谢谢。OpenCV的版本是3.1.0,带有来自ITSEZ/OpenCV_contrib github存储库的额外模块
import os
import cv2 #Using CV 2.4.12
import numpy as np

## Prepare images files
rootpath = 'images/'
files = []
for filedir, dirs, filess in os.walk(rootpath):
    for filename in filess:
        pathfile = os.path.join(filedir, filename)
        files.append(pathfile) 

print files

## Detect keypoints and compute descriptors for train images
kp_train = []
dsc_train = []
sift = cv2.SIFT()
for file in files:
    ima = cv2.imread(file)
    print file
    gray=cv2.cvtColor(ima,cv2.COLOR_BGR2GRAY)
    kpts, des = sift.detectAndCompute(gray, None) #sift = cv2.xfeatures2d.SIFT_create()
    kp_train.append(kpts)
    dsc_train.append(des)

## Train knn
dsc_train = np.array(dsc_train)
responses = np.arange(len(kp_train),dtype = np.float32)
knn = cv2.KNearest()

#Next line does not work:
knn.train(dsc_train, responses)