资源简介
利用opencv对数码管进行识别,主要利用knn算法对数据进行训练

代码片段和文件信息
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::ml;
bool cmp(const Rect &a const Rect &b);
vector get_mats(Mat mt);
bool has_Point(Mat mt);
int main(){
string changename(int iint j);
string get_name(int i);
Mat traindata train_label tmp;
for (int j = 0; j < 10; j++){
for (int i = 0; i < 40; i++){
string name = changename(ji);
//cout << name << endl;
tmp = imread(name 0);
resize(tmp tmp Size(75 125));
traindata.push_back(tmp.reshape(0 1));
train_label.push_back(j);
}
}
traindata.convertTo(traindata CV_32F);
Ptr tData = TrainData::create(traindata ROW_SAMPLE train_label);
Ptr knn = KNearest::create();
knn->setDefaultK(3);
knn->setIsClassifier(true);
knn->train(tData);
Mat mymt = imread(“f:/RecordedImage_GO-5000C-USB__000.tif“);
vector dds=get_mats(mymt);
ostringstream oss;
for (int n = 0; n < dds.size(); n++){
dds[n].copyTo(tmp);
resize(tmp tmp Size(75 125));
tmp = tmp.reshape(0 1);
tmp.convertTo(tmp CV_32F);
int result = knn->predict(tmp);
oss << result;
if (has_Point(dds[n])) oss << “.“;
}
cout << oss.str() << endl;
}
string changename(int iint j){
ostringstream oss;
oss <<“f:/“<< i << “_out/“ < string name = oss.str();
return name;
}
string get_name(int i){
ostringstream oss;
oss << “f:/tmp/“ << i << “.jpg“;
return oss.str();
}
vector get_mats(Mat mt){
vector channelsresult_datas;
Mat image_bin result ele image_bin2 temp;
split(mt channels);
//阈值选取很重要
threshold(channels[2] image_bin2 50 255 THRESH_BINARY);
ele = getStructuringElement(MORPH_RECT Size(5 5)); // 膨胀
dilate(image_bin2 image_bin ele);
image_bin.copyTo(temp);
vector> conts;
vector hire;
//轮廓提取
findContours(image_bin conts hire RETR_EXTERNAL CHAIN_APPROX_NONE);
vector rects;
vector datas;
temp.copyTo(image_bin);
//threshold(mtimage_bin150255THRESH_BINARY);
for (int i = 0; i Rect t = boundingRect(Mat(conts[i]));
if (t.width<40 || t.height<50) continue;
rects.push_back(t);
//rectangle(mtt(255255255));
}
// imshow(“kkkl“mt);
//sort numbers
sort(rects.begin() rects.end() cmp);
//cut pictures
for (int i = 0; i
image_bin(rects[i]).copyTo(result);
result_datas.push_back(result);
// datas.push_back(result);
}
return result_datas;
}
bool cmp(const Rect &a const Rect &b){
if (a.x return true;
}
else{
return false;
}
}
//取5X5方块检验小数点
bool has_Point(Mat mt){
int with = mt.rows;
int height = mt.cols;
Rect r(height - 5 with - 5 5 5);
mt(r).copyTo(mt);
Mat_::iterator i
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-01-10 23:34 ConsoleApplication3\
目录 0 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\
文件 14811136 2018-01-10 23:34 ConsoleApplication3\ConsoleApplication3.sdf
文件 1003 2018-01-10 12:58 ConsoleApplication3\ConsoleApplication3.sln
文件 17408 2018-01-10 23:34 ConsoleApplication3\ConsoleApplication3.v12.suo
文件 4347 2018-01-10 13:03 ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.vcxproj
文件 946 2018-01-10 13:03 ConsoleApplication3\ConsoleApplication3\ConsoleApplication3.vcxproj.filters
目录 0 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\
目录 0 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\
文件 880 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\cl.command.1.tlog
文件 11496 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\CL.read.1.tlog
文件 936 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\CL.write.1.tlog
文件 214 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\ConsoleApplication3.lastbuildstate
文件 1648 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\li
文件 2646 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\li
文件 912 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleA.90DD938F.tlog\li
文件 3301 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\ConsoleApplication3.log
文件 624955 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\hello.obj
文件 527360 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\vc120.idb
文件 995328 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\Debug\vc120.pdb
文件 3237 2018-01-10 23:33 ConsoleApplication3\ConsoleApplication3\hello.cpp
目录 0 2018-01-10 23:27 ConsoleApplication3\Debug\
文件 159744 2018-01-10 23:33 ConsoleApplication3\Debug\ConsoleApplication3.exe
文件 1209304 2018-01-10 23:33 ConsoleApplication3\Debug\ConsoleApplication3.ilk
文件 2231296 2018-01-10 23:33 ConsoleApplication3\Debug\ConsoleApplication3.pdb
文件 7134203 2018-01-10 23:32 RecordedImage_GO-5000C-USB__000.tif
文件 9896 2018-01-10 23:34 捕获.PNG
- 上一篇:企业进销存系统源代码(B/S结构)
- 下一篇:易康ecognition培训教程
相关资源
- [免费]图像识别c 源码
- 【单片机】51单片机数码管显示万年历
- 25.数码管显示0-99
- STC12C5A60S2 CH4521 驱动数码管 显示
- 基于OpenCV的数字识别468815
- 使用opencv去掉二值化图像中黑色面积
- CH454键盘与数码管显示程序
- 基于EWB软件的数码管显示控制器
- opencv环境配置
- win10 64位下编译的opencv4.5.5库,opencv
- 单片机电子琴 数码管显示
- 74hc244驱动数码管显示电路及程序
- 数码管字体
- STM32F407实时时钟_DS1302实时时钟模块和
- 基于STM32F103的实时时钟程序,采用D
- 51单片机_DS1302时钟芯片_共阴数码管
- NVIDIAOpticalFlowSDK-79c6cee80a2df9a196f20afd6
- opencv_contrib-3.4.0.zip
- 倒计时与选手数码管编号显示的四人
- opencv2.4.9源码分析——SIFT
- 用两个摄像头实现,双目标定,双目
- opencv_traincascade训练分类器,手势识别
- opencv3.0交叉编译用parallel.cpp
- 基于opencv的图像识别识别图像中的色
- 基于openCV的识别特定颜色区域
- 基于OpenCV的分水岭算法实现
- QT+opencv+OCR 身份证号码,银行卡号识别
- opencv视频特定颜色区域识别
- 把RGB转换为HSV和HSI然后根据黄色和蓝
- opencv视觉测距
评论
共有 条评论