【问题标题】:my application crashes at KNearest::find_nearest我的应用程序在 KNearest::find_nearest 崩溃
【发布时间】:2014-07-29 12:10:23
【问题描述】:

我想实现一个 OCR 功能。

我已经收集了一些样本,我想使用 K-Nearest 来实现它。

所以,我使用下面的代码来加载数据并初始化 KNearest

KNearest knn = new KNearest;
Mat mData, mClass;
for (int i = 0; i <= 9; ++i)
{
    Mat mImage = imread( FILENAME ); // the filename format is '%d.bmp', presenting a 15x15 image
    Mat mFloat;
    if (mImage.empty()) break; // if the file doesn't exist

    mImage.convertTo(mFloat, CV_32FC1);
    mData.push_back(mFloat.reshape(1, 1));
    mClass.push_back( '0' + i );
}

knn->train(mData, mClass);

然后,我调用代码以找到最佳结果

for (vector<Mat>::iterator it = charset.begin(); it != charset.end(); ++it)
{
    Mat mFloat;
    it->convertTo(mFloat, CV_32FC1); // 'it' presents a 15x15 gray image
    float result = knn->find_nearest(mFloat.reshape(1, 1), knn->get_max_k());
}

但是,我的应用程序在 find_nearest 处崩溃。

谁能帮帮我?

【问题讨论】:

  • 尝试了解问题是 find_nearest() 还是 reshape(),例如只调用里面的第二个 for
  • @Velthune 我认为问题出在 find_nearest() 因为我让它们成为每行一个调用。 reshape() 似乎工作正常。

标签: c++ c opencv


【解决方案1】:

我似乎找到了问题...

我的示例图像是通过 cvtColor 转换的灰度图像,但我的输入图像不是。

添加后

cvtColor(mImage, mImage, COLOR_BGR2GRAY);

之间

if (mImage.empty()) break;
mImage.convertTo(mFloat, CV_32FC1);

find_nearest() 返回一个值,我的应用程序很好。

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    相关资源
    最近更新 更多