我正在阅读使用Opencv训练KNN的教程。代码是为Opencv 3编写的,但我需要在Opencv 2中使用它。最初的训练是:
cv2.ml.KNearest_create().train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)我试着使用这个:
cv2.KNearest().train(npaFlattenedImages, cv2.CV_ROW_SAMPLE, npaClassifications)但错误是:
Unsupported index array data type (it should be 8uC1, 8sC1 or 32sC1) in function cvPreprocessIndexArray
发布于 2016-09-25 14:59:58
下面是让我在OpenCV 2.4.13上使用full code的一些更改:
60c60
< kNearest = cv2.ml.KNearest_create() # instantiate KNN object
---
> kNearest = cv2.KNearest() # instantiate KNN object
62c62
< kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)
---
> kNearest.train(npaFlattenedImages, npaClassifications)
85c85
< imgContours, npaContours, npaHierarchy = cv2.findContours(imgThreshCopy, # input image, make sure to use a copy since the function will modify this image in the course of finding contours
---
> npaContours, npaHierarchy = cv2.findContours(imgThreshCopy, # input image, make sure to use a copy since the function will modify this image in the course of finding contours
125c125
< retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k = 1) # call KNN function find_nearest
---
> retval, npaResults, neigh_resp, dists = kNearest.find_nearest(npaROIResized, k = 1) # call KNN function find_nearest发布于 2016-09-25 15:49:41
CvStatModel::train()不同,cv2.KNearest.train()没有第二个可选参数int tflag,文档上说:“只支持CV_ROW_SAMPLE数据布局”。因此,尝试使用sampleIdx.作为下一个参数的函数
修复此问题后的其他错误:
cv2.findCountours()只返回两个值: anyway).KNearest.findNearest() (你不需要第三个值,KNearest.find_nearest().是→ contours, hierarchy,imgContours
现在的结果是:

https://stackoverflow.com/questions/39598724
复制相似问题