首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将KNN训练从Opencv 3转换为2

将KNN训练从Opencv 3转换为2
EN

Stack Overflow用户
提问于 2016-09-20 23:56:14
回答 2查看 1.6K关注 0票数 1

我正在阅读使用Opencv训练KNN的教程。代码是为Opencv 3编写的,但我需要在Opencv 2中使用它。最初的训练是:

代码语言:javascript
复制
cv2.ml.KNearest_create().train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

我试着使用这个:

代码语言:javascript
复制
cv2.KNearest().train(npaFlattenedImages, cv2.CV_ROW_SAMPLE, npaClassifications)

但错误是:

Unsupported index array data type (it should be 8uC1, 8sC1 or 32sC1) in function cvPreprocessIndexArray

完整的代码在这里:https://github.com/MicrocontrollersAndMore/OpenCV_3_KNN_Character_Recognition_Python/blob/master/TrainAndTest.py

EN

回答 2

Stack Overflow用户

发布于 2016-09-25 14:59:58

下面是让我在OpenCV 2.4.13上使用full code的一些更改:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2016-09-25 15:49:41

作为下一个参数的函数

修复此问题后的其他错误:

→ contours, hierarchyimgContours

现在的结果是:

Ulrich Stern already did me a favor to provide a raw diff

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39598724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档