资源简介
对印刷数字识别。
过程:
1对图片进行灰度化二值化.
2对图片上的数字进行切割
3.制作匹配印刷体数字模板
4.平方和最小原则对数字识别
配置环境:
VS2013+,OPENCV2.4.xx都可以(opencv3不支持,可以配置多版本opencv)
注意opencv的配置:如果没有需要先下载。
随后改项目属性:
1.VC++目录 包含目录:I:\opencv2.413\build\include
I:\opencv2.413\build\include\opencv
I:\opencv2.413\build\include\opencv2
2.库目录:I:\opencv2.413\build\x64\vc14\lib
3.链接器-输入-附加依赖项:
opencv_core2413d.lib
opencv_imgproc2413d.lib
opencv_highgui2413d.lib
代码片段和文件信息
/*****************************************************
图片数字识别
*****************************************************/
#include “cv.h“
#include “highgui.h“
#include “cxcore.h“
#include
#include
using namespace std;
using namespace cv;
int getColSum(Mat src int col); //列求和
int cutLeft(Mat& src Mat& leftImg Mat& rightImg);//左右切割
void cutTop(Mat& src Mat& dstImg);//上下切割
int getRowSum(Mat src int row); //航求和
void getPXSum(Mat &src int &a);//获取所有像素点和
int getSubtract(Mat &src int TemplateNum);
void pictureCut(); //模板数字图片切割
LPCWSTR dirName = L“.\\AAA“; //保存分割后照片的文件夹
int main()
{
pictureCut();
//创建文件夹保存分割后的照片
BOOL flag = CreateDirectory(dirName NULL);
Mat src = imread(“.\\sss.png“ CV_LOAD_IMAGE_GRAYSCALE);
threshold(src src 100 255 CV_THRESH_BINARY_INV);
imshow(“原始灰度图二值处理后“ src);
printf(“识别的数字是:\n“);
Mat leftImg rightImg;
int res = cutLeft(src leftImg rightImg);
int i = 0;
while (res == 0)
{
char nameLeft[10];
sprintf(nameLeft “%dLeft“ i);
Mat srcTmp = rightImg;
getSubtract(leftImg 10);//数字识别
res = cutLeft(srcTmp leftImg rightImg);
}
waitKey(0);
return 0;
}
//模板数字图片切割
void pictureCut()
{
Mat src = imread(“.\\base.png“ CV_LOAD_IMAGE_GRAYSCALE);
threshold(src src 100 255 CV_THRESH_BINARY_INV);
Mat leftImg rightImg;
int res = cutLeft(src leftImg rightImg);
int i = 0;
while (res == 0)
{
char nameLeft[10];
sprintf(nameLeft “%dLeft“ i);
char nameRight[10];
sprintf(nameRight “%dRight“ i);
i++;
stringstream ss;
ss << nameLeft;
imwrite(“.\\AAA\\“ + ss.str() + “.jpg“ leftImg);//保存截取图片做为模板
ss >> nameLeft;
Mat srcTmp = rightImg;
res = cutLeft(srcTmp leftImg rightImg);
}
}
int cutLeft(Mat& src Mat& leftImg Mat& rightImg)//左右切割
{
int left right;
left = 0;
right = src.cols;
int i;
for (i = 0; i < src.cols; i++)
{
int colValue = getColSum(src i);
if (colValue > 0)
{
left = i;
break;
}
}
if (left == 0)
{
return 1;
}
for (; i < src.cols; i++)
{
int colValue = getColSum(src i);
if (colValue == 0)
{
right = i;
break;
}
}
int width = right - left;
Rect rect(left 0 width src.rows);
leftImg = src(rect).clone();
Rect rectRight(right 0 src.cols - right src.rows);
rightImg = src(rectRight).clone();
cutTop(leftImg leftImg);
return 0;
}
int getColSum(Mat src int col)
{
int sum = 0;
int height = src.rows;
int width = src.cols;
for (int i = 0; i < height; i++)
{
sum = sum + src.at (i col);
}
return sum;
}
void cutTop(Mat& src Mat& dstImg)//上下切割
{
int top bottom;
top = 0;
bottom = src.rows;
int i;
for (i = 0; i < src.rows; i++)
{
int colValue = getRowSum(src i);
if (colValue > 0)
{
top = i;
break;
}
}
for (; i < src.rows; i++)
{
int colValue = getRowSum(src i);
if (colValue == 0)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-11-06 16:21 testNumber\
目录 0 2017-11-02 21:26 testNumber\.vs\
目录 0 2017-11-02 21:26 testNumber\.vs\testNumber\
目录 0 2017-11-02 21:26 testNumber\.vs\testNumber\v14\
文件 31744 2018-11-06 16:21 testNumber\.vs\testNumber\v14\.suo
文件 51118080 2018-11-06 16:21 testNumber\testNumber.sdf
文件 1312 2017-11-02 17:43 testNumber\testNumber.sln
目录 0 2018-11-06 16:21 testNumber\testNumber\
目录 0 2017-11-02 21:56 testNumber\testNumber\AAA\
文件 1499 2018-11-06 16:21 testNumber\testNumber\AAA\0Left.jpg
文件 834 2018-11-06 16:21 testNumber\testNumber\AAA\1Left.jpg
文件 1242 2018-11-06 16:21 testNumber\testNumber\AAA\2Left.jpg
文件 1339 2018-11-06 16:21 testNumber\testNumber\AAA\3Left.jpg
文件 1069 2018-11-06 16:21 testNumber\testNumber\AAA\4Left.jpg
文件 1246 2018-11-06 16:21 testNumber\testNumber\AAA\5Left.jpg
文件 1472 2018-11-06 16:21 testNumber\testNumber\AAA\6Left.jpg
文件 1037 2018-11-06 16:21 testNumber\testNumber\AAA\7Left.jpg
文件 1748 2018-11-06 16:21 testNumber\testNumber\AAA\8Left.jpg
文件 1652 2018-11-06 16:21 testNumber\testNumber\AAA\9Left.jpg
文件 4754 2017-11-02 18:28 testNumber\testNumber\ba
文件 4459 2018-11-06 16:21 testNumber\testNumber\main.cpp
文件 5018 2017-11-02 18:42 testNumber\testNumber\sss.png
文件 7629 2018-11-06 16:15 testNumber\testNumber\testNumber.vcxproj
文件 945 2017-11-02 17:44 testNumber\testNumber\testNumber.vcxproj.filters
目录 0 2018-11-06 16:12 testNumber\testNumber\x64\
目录 0 2018-11-06 16:21 testNumber\testNumber\x64\Debug\
文件 715977 2018-11-06 16:21 testNumber\testNumber\x64\Debug\main.obj
文件 1540 2018-11-06 16:21 testNumber\testNumber\x64\Debug\testNumber.log
目录 0 2018-11-06 16:21 testNumber\testNumber\x64\Debug\testNumber.tlog\
文件 612 2018-11-06 16:21 testNumber\testNumber\x64\Debug\testNumber.tlog\CL.command.1.tlog
文件 46214 2018-11-06 16:21 testNumber\testNumber\x64\Debug\testNumber.tlog\CL.read.1.tlog
............此处省略12个文件信息
相关资源
- 基于opencv的模板匹配代码
- opencv图片扫描以及校正
- opencv手部轮廓识别以及轨迹识别
- opencv2 3D标定.cpp
- 基于opencv漫水填充算法综合
- opencv激光中心线的提取
- OpenCV Computer Vision Application Programming
- 基于图割的图像分割OpenCV+MFC实现
- 识别魔方颜色
- opencv版俄罗斯方块源码
- VS2013 / MFC + OpenCV 2.4.9实现视频的播放
- 粒子滤波器+目标跟踪的C++实现,VS2
- 张平OpenCV算法精讲基于python和C++教材
- 虹膜识别开源代码OSIRIS4.1基于opencv
- Sift特征点提取与匹配opencv库
- YCbCr、混合高斯以及YCbCg肤色检测模型
- 光流法代码
- OpenCV打开摄像机显示在MFC窗口工程源
- 使用c++读取图像到二维矩阵
- 三维点云的圆柱面拟合
- MFC+OPENCV摄像机标定程序
- 基于特征脸的人脸识别MFC+OpenCV
- opencv图像处理MFC
- OPENCV人脸检测加角点检测并输出坐标
- FillHole.rar
- 道路提取算法 c++ opencv
- PCA代码实现详解
- HandWriting Predict 手写数字识别小项目
- opencv卡尔曼滤波
- SeamCarving opencv c++
评论
共有 条评论