画像上のデジタル認識
手順:
1.画像分割→テンプレート作成
2.対象ピクチャの分割->対比認識;
1.画像分割→テンプレート作成
2.対象ピクチャの分割->対比認識;
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
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;
}
int getRowSum(Mat src, int row)
{
int sum = 0;
int height = src.rows;
int width = src.cols;
for (int i = 0; i < width; i++)
{
sum += src.at (row, i);
}
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);
//cout <0)
{
top = i;
break;
}
}
for (; i < src.rows; i++)
{
int colValue = getRowSum(src, i);
//cout << i << " th " << colValue << endl;
if (colValue == 0)
{
bottom = i;
break;
}
}
int height = bottom - top;
Rect rect(0, top, src.cols, height);
dstImg = src(rect).clone();
}
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);
//cout <0)
{
left = i;
break;
}
}
if (left == 0)
{
return 1;
}
for (; i < src.cols; i++)
{
int colValue = getColSum(src, i);
//cout << i << " th " << colValue << endl;
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;
}
void getPXSum(Mat &src, int &a)//
{
threshold(src, src, 100, 255, CV_THRESH_BINARY);
a = 0;
for (int i = 0; i < src.rows;i++)
{
for (int j = 0; j < src.cols; j++)
{
a += src.at (i, j);
}
}
}
int getSubtract(Mat &src, int TemplateNum) //
{
Mat img_result;
int min = 10000000;
int serieNum = 0;
for (int i = 0; i < TemplateNum; i++){
char name[20];
sprintf_s(name, "D:\\%dLeft.jpg", i);
Mat Template = imread(name, CV_LOAD_IMAGE_GRAYSCALE);
threshold(Template, Template, 200, 255, CV_THRESH_BINARY);
threshold(src, src, 80, 255, CV_THRESH_BINARY);
resize(src, src, Size(32, 48), 0, 0, CV_INTER_LINEAR);
resize(Template, Template, Size(32, 48), 0, 0, CV_INTER_LINEAR);
//imshow(name, Template);
absdiff(Template, src, img_result);
int diff = 0;
getPXSum(img_result, diff);
if (diff < min)
{
min = diff;
serieNum = i;
}
}
if(serieNum!=10)
{
/*
printf(" %d ", min);
printf(" %d %d
", serieNum,serieNum);*/
cout<> nameLeft;*/
// //
Mat srcTmp = rightImg;
getSubtract(leftImg, 11);
res = cutLeft(srcTmp, leftImg, rightImg);
}
cout<