#include "cv.h"
#include "highgui.h"
#include<stack>
using namespace cv;
using namespace std;
#define PI 3.1415926
#define RADIAN(angle) ((angle)*PI/180.0)
struct pattern
{
int number; //该类型数字样品个数
double feature[200][25];//该类型数字样品特征,每个特征为25维
pattern()
{
number=0;
}
};
struct Position
{
int bottom;
int top;
int left;
int right;
};
pattern ptns[10]; //一共10个数字
double testsample[25]; //手写数字的特征 25维
//判断手写数字可否保存为cls类别的一个样品 同一类别不能有重复
bool savable(int cls)
{
for(int i=0;i<ptns[cls].number;i++)
{
bool flag=true;
for(int j=0;j<25;j++)
flag&=(fabs(ptns[cls].feature[i][j]-testsample[j])<0.0000001 ? true:false);
if(flag) return false;
}
return true;
}
//将手写数字保存为cls类别中的一个样品
void Save(int cls)
{
for(int i=ptns[cls].number;i>0;i--)
for(int j=0;j<25;j++)
ptns[cls].feature[i][j]=ptns[cls].feature[i-1][j];
for(int j=0;j<25;j++)
ptns[cls].feature[0][j]=testsample[j];
ptns[cls].number++;
}
//计算手写数字的在图片中出现的位置
Position GetPosition(Mat& mat)
{
int M=mat.rows;
int N=mat.cols;
bool flag=false;
int i,j;
Position ret;
//top
for(i=0;i<M-1;i++)
{
flag=false;
for(j=0;j<N;j++)
{
if(mat.at<uchar>(i,j)<128)
{
flag=true;
break;
}
}
if(flag) break;
}
ret.top=i;
//bottom
for(i=M-1;i>0;i--)
{
flag=false;
for(j=0;j<N;j++)
{
if(mat.at<uchar>(i,j)<128)
{
flag=true;
break;
}
}
if(flag) break;
}
ret.bottom=i;
//left
for(j=0;j<N-1;j++)
{
flag=false;
for(i=0;i<M;i++)
{
if(mat.at<uchar>(i,j)<128)
{
flag=true;
break;
}
}
if(flag) break;
}
ret.left=j;
for(j=N-1;j>0;j--)
{
flag=false;
for(i=0;i<M;i++)
{
if(mat.at<uchar>(i,j)<128)
{
flag=true;
break;
}
}
if(flag) break;
}
ret.right=j;
return ret;
}
double Cal(int row,int col,Position pos,Mat& mat)
{
int w,h;
w=(pos.right-pos.left)/5;
h=(pos.bottom-pos.top)/5;
int count=0;
for(int i=pos.top+row*h;i<pos.top+(row+1)*h;i++)
{
for(int j=pos.left+col*w;j<pos.left+(col+1)*w;j++)
{
if(mat.at<uchar>(i,j)<128)
count++;
}
}
return ((double)count)/(w*h);
}
//将手写数字特征保存在变量testsample中
void SetFeature(Mat& mat,Position pos)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
testsample[5*i+j]=Cal(i,j,pos,mat);
}
}
}
//计算两个特征之间的距离
double distance(double s1[],double s2[])
{
double d=0.0;
for(int i=0;i<25;i++)
d+=(s1[i]-s2[i])*(s1[i]-s2[i]);
return d;
}
//寻找样品库中距离最小的模版
int Match()
{
int ret;
double minD=INT_MAX;
for(int i=0;i<10;i++)
{
for(int j=0;j<ptns[i].number;j++)
{
double curd=distance(ptns[i].feature[j],testsample);
if(curd<minD)
{
minD=curd;
ret=i;
}
}
}
return ret;
}
int main()
{
//读取图像
Mat image;
//样本库特征提取
int cls;
char filename[100]="a.txt";
char filePath[100];
FILE* fp=fopen(filename,"r");
while(fscanf(fp,"%d%s",&cls,filePath))
{
if(cls==-1)
break;
Mat image=imread(filePath,CV_LOAD_IMAGE_GRAYSCALE);
Position pos=GetPosition(image);
SetFeature(image,pos);
if(savable(cls))
Save(cls);
}
fclose(fp);
image=imread("testsample.png",CV_LOAD_IMAGE_GRAYSCALE);
//计算手写数字的位置
Position pos=GetPosition(image);
//计算手写数字的特征
SetFeature(image,pos);
printf("%d\n",Match());
//cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
//imshow("test",image);
waitKey(0);
return 0;
}
图像模式识别之模版匹配分类器
最新推荐文章于 2021-06-14 14:34:33 发布