问题描述
我正在阅读使用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
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 argumentint tflag
, and the docs say: "OnlyCV_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
. cv2.findCountours()
only returns 2 values:→ contours, hierarchy
(you don't need the 3rd one,imgContours
, anyway).
解决此问题后的其他错误:
Further errors after fixing this:
KNearest.findNearest()
与KNearest.find_nearest()
.现在的结果是
乌尔里希·斯特恩(Ulrich Stern)已经帮我提供了原始差异.
这篇关于将KNN火车从Opencv 3转换为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- The error message (btw the cryptic mnemonics are OpenCV data types) was thus caused by the function trying to use
- 错误消息(在密码助记符为 OpenCV数据中类型)是由该函数试图将