-
大小: 1KB文件類型: .gz金幣: 1下載: 0 次發(fā)布日期: 2021-06-05
- 語(yǔ)言: 其他
- 標(biāo)簽: 機(jī)器學(xué)習(xí)??
資源簡(jiǎn)介
利用KNN識(shí)別LED數(shù)碼管,識(shí)別率大概99左右,需要的可以看下。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
using?namespace?cv;
using?namespace?std;
int?main()
{
????double?t;
????t=(double)cvGetTickCount();
????const?int?k?=?5;
????cv::Ptr?knn?=?ml::KNearest::create();
????knn->setDefaultK(k);
????knn->setIsClassifier(true);?????//分類問(wèn)題
????knn->setAlgorithmType(ml::KNearest::BRUTE_FORCE);???//設(shè)置/獲取KNN算法類型,目前支持兩種:brute_force和KDTree;
????Mat?datalabel;
????string?readName?=?“/home/tianjiazhao/圖片/LED_Number/“;
????for(int?i?=?1;?i?10;?i++)
????{
????????string?readName_?=?readName?+?to_string(i);
????????for(int?j?=?1;?j?<51;?j++)
????????{
????????????Mat?gray;
????????????gray?=?imread(readName_?+“/“+?to_string(j)?+?“.jpg“0);
????????????threshold(gray?gray?200?255?CV_THRESH_BINARY);
//????????????imshow(““gray);
//????????????waitKey(0);
????????????Mat?tmp;
????????????resize(graytmpSize(88));
????????????imshow(““tmp);
????????????data.push_back(tmp.reshape(01));
????????????label.push_back(i);
????????}
????}
????data.convertTo(dataCV_32F);????//char轉(zhuǎn)化為cv_32F
????knn->train(dataml::ROW_SAMPLElabel);
????knn->save(“LED2.xml“);
????//predict
????float?train_hr?=?0;
//????Mat?predoct_img?=imread(“/home/tianjiazhao/opencv/RuneDetector/“
//????????????????????????????“build-RunDetector-Desktop_Qt_5_9_0_GCC_64bit-Debug/160.jpg“0);
//????resize(predoct_imgpredoct_imgSize(1010));
//????imshow(“preditc“predoct_img);
//????Mat?sample;
//????sample.push_back(predoct_img.reshape(01));
//????cout< //????sample.convertTo(sampleCV_32F);
//????float?r?=?knn->predict(sample);
//????cout<<“r“< ????for(int?i?=?0;?i?????{
????????Mat?sample?=?data.row(i);
//????????cout< ????????float?r?=?knn->predict(sample);
????????r?=?std::abs(r?-?label.at(i))<=?FLT_EPSILON???1.f?:?0.f;
//????????cout<<“r“< ????????train_hr?+=?r;
????}
????train_hr?/=?data.rows;
????cout< //????waitKey(0);
????t=(double)cvGetTickCount()-t;
????printf(“used?time?is?%gms\n“(t/(cvGetTickFrequency()*1000)));
}
評(píng)論
共有 條評(píng)論