-
大小: 30KB文件类型: .rar金币: 2下载: 1 次发布日期: 2021-08-22
- 语言: C/C++
- 标签: OpenCV2.4.9 C++
资源简介
利用OpenCV的图像处理技术,对图像进行灰度处理,二值分析,降噪,滤波等,识别照片中的数字,完美精确, C++ VS2015代码,有详细的代码注释,完整源码。
代码片段和文件信息
/*****************************************************
图片数字识别
*****************************************************/
#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)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 23552 2017-11-02 22:10 testNumber\.vs\testNumber\v14\.suo
文件 1499 2017-11-02 22:00 testNumber\testNumber\AAA\0Left.jpg
文件 834 2017-11-02 22:00 testNumber\testNumber\AAA\1Left.jpg
文件 1242 2017-11-02 22:00 testNumber\testNumber\AAA\2Left.jpg
文件 1339 2017-11-02 22:00 testNumber\testNumber\AAA\3Left.jpg
文件 1069 2017-11-02 22:00 testNumber\testNumber\AAA\4Left.jpg
文件 1246 2017-11-02 22:00 testNumber\testNumber\AAA\5Left.jpg
文件 1472 2017-11-02 22:00 testNumber\testNumber\AAA\6Left.jpg
文件 1037 2017-11-02 22:00 testNumber\testNumber\AAA\7Left.jpg
文件 1748 2017-11-02 22:00 testNumber\testNumber\AAA\8Left.jpg
文件 1652 2017-11-02 22:00 testNumber\testNumber\AAA\9Left.jpg
文件 4754 2017-11-02 18:28 testNumber\testNumber\ba
文件 4459 2017-11-02 22:07 testNumber\testNumber\main.cpp
文件 5018 2017-11-02 18:42 testNumber\testNumber\sss.png
文件 7248 2017-11-02 17:44 testNumber\testNumber\testNumber.vcxproj
文件 945 2017-11-02 17:44 testNumber\testNumber\testNumber.vcxproj.filters
文件 1312 2017-11-02 17:43 testNumber\testNumber.sln
目录 0 2017-11-02 21:26 testNumber\.vs\testNumber\v14
目录 0 2017-11-02 21:26 testNumber\.vs\testNumber
目录 0 2017-11-02 21:56 testNumber\testNumber\AAA
...D.H. 0 2017-11-02 21:26 testNumber\.vs
目录 0 2017-11-02 22:13 testNumber\testNumber
目录 0 2017-11-02 22:14 testNumber
----------- --------- ---------- ----- ----
60426 23
- 上一篇:小波分析的c语言程序
- 下一篇:C语言课程设计 俄罗斯方块源程序
评论
共有 条评论