将KNN列车从Opencv 3转换为2

2024-09-30 02:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

cv2.ml.KNearest_create().train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

我试着用这个:

^{pr2}$

但错误是:

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


Tags: sample代码createtrain教程cv2opencvml
2条回答

在这里,似乎使full code适用于OpenCV 2.4.13的更改:

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
  • 与泛型的^{}不同,^{}没有第二个可选参数int tflag,文档说:“只支持CV_ROW_SAMPLE数据布局”。
    • 错误消息(顺便说一句,神秘助记符是OpenCV data types)是由于函数试图使用npaClassifications作为下一个参数sampleIdx引起的。在

修复后的其他错误:

  • ^{}只返回2个值:→ contours, hierarchy(无论如何,您不需要第三个值,imgContours)。

  • KNearest.findNearest()^{}

现在的结果是:

result

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

相关问题 更多 >

    热门问题