资源简介
利用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培训教程
相关资源
- OpenCV2.4.3 windows版本
- 各种型号的 数码管封装库3D封装文件
- opencv stitching拼接
- OpenCV自带视频测试文件vtest.avi
- Mastering OpenCV with Practical Computer Visio
- opencv-3.4.0编译失败需要的boostdesc_bgm
- VS2015+Opencv310的X86动态库lib
- 基于OpenCV的红外运动目标检测与跟踪
- 手势检测基于OpenCV
- 基于fpga的超声波测距,数码管显示
- opencv 红绿灯识别 运动物体识别
- MAX7219数码管驱动89C52控制
- freeglut,包括.dll、.h和.lib文件,完美
- 基于图像识别的电熨斗点胶机系统
- Opencv摄像头校正
- 基于FPGA用VHDL语言设计的四位共阴数码
- OpenCV识别出两张图片的人脸,并比较
- 基于QtOpenCV实现GraphCuts算法
- 基于颜色识别的火焰检测算法的open
- opencv红绿灯识别样本北京市的红绿灯
- 基于OpenCV的火灾视频识别方法研究
- opencv人脸检测训练用到的正样本图像
- opencv haartraining 合并.vec文件的工具
- OpenCV3入门pdf
- 基于OpenCV的红点识别
- slam 开源代码ptam and ptamm
- opencv3.4.3 mingw32编译
- 数码管与DS18B20设计的温度报警器
- 圆形公章的定位检测
- 微机原理抢答器(双数码管)课程设
评论
共有 条评论