将KNN火车从Opencv 3转换为2 [英] Convert KNN train from Opencv 3 to 2

查看:117
本文介绍了将KNN火车从Opencv 3转换为2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读使用Opencv训练KNN的教程.该代码是为Opencv 3编写的,但我需要在Opencv 2中使用它.原始培训是:

I am reading a tutorial for training KNN using Opencv. The code is written for Opencv 3 but I need to use it in Opencv 2. The original training is:

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

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

The full code is here: https://github.com/MicrocontrollersAndMore/OpenCV_3_KNN_Character_Recognition_Python/blob/master/TrainAndTest.py

推荐答案

  • 与通用 CvStatModel::train() cv2.KNearest.train() 没有第二个可选参数int tflag,文档说:仅支持CV_ROW_SAMPLE数据布局".
    • 错误消息(在密码助记符为 OpenCV数据中类型)是由该函数试图将npaClassifications用作下一个参数sampleIdx引起的.
      • Unlike the generic CvStatModel::train(), cv2.KNearest.train() doesn't have the 2nd optional argument int tflag, and the docs say: "Only CV_ROW_SAMPLE data layout is supported".
        • The error message (btw the cryptic mnemonics are OpenCV data types) was thus caused by the function trying to use npaClassifications as the next argument, sampleIdx.
        • 解决此问题后的其他错误:

          Further errors after fixing this:

          • 仅返回2个值:→ contours, hierarchy(无论如何,您都不需要第三个值,imgContours).

          • cv2.findCountours() only returns 2 values: → contours, hierarchy (you don't need the 3rd one, imgContours, anyway).

          KNearest.findNearest() KNearest.find_nearest() .

          现在的结果是

          乌尔里希·斯特恩(Ulrich Stern)已经帮我提供了原始差异.

          这篇关于将KNN火车从Opencv 3转换为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆