笔迹鉴别程序

考试的笔迹鉴别程序,分辨出不同人写的笔迹
This commit is contained in:
yanshui177
2017-05-17 16:50:37 +08:00
parent abe00d2e02
commit 962de04ffb
205 changed files with 17672 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
/* 程序名Cjbsb.c
功能:读入图像文件,甄别图像的角标
参数设置:
img 大图
imgjbsb 角标图像头
jbwhite 未知参数1
jbblack 未知参数2
【返回文字的笔迹部分】
注意:这个方法不行,另外一个号的方法:取一个方块,移动方块,确定方块中的样子,如果有横线或者数显就确定为脚标
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
IplImage* Cjbsb(IplImage* img,IplImage* imgjbsb,int jbwhite,int jbblack){
/*定义变量*/
int i,j,ii,jj,sumjb1,sumjb2,jbi=0,jbj=0;
int height,width,step,channels;
uchar *data;
int brklab=0;
//1、 获取图像信息
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
//IplImage* imgjbsb = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopy(img,imgjbsb,NULL);
uchar *imgjbsbdata= (uchar *)imgjbsb->imageData;
//----------------------------------------//
//2、找脚标的位置
//----------------------------------------//
for(i=0;i<height/3;i++){
for(j=0;j<width/5;j++){
sumjb1=0;
for(ii=0;ii<=14;ii++) //计算特征点的下侧14与右侧14的白点数目(是因为脚标就是14的长度吧)
sumjb1=sumjb1+imgjbsbdata[(i+ii)*step+j*channels];//下侧14列的单点的白点数目
for(jj=0;jj<=14;jj++)
sumjb1=sumjb1+imgjbsbdata[i*step+(j+jj)*channels];//右侧14行的单点的白点数目
if(sumjb1<=255*jbwhite){ //jbwhite为允许角标上白点数第一次提取
sumjb2=0;
for(ii=i+2;ii<i+12;ii++)
for(jj=j+2;jj<j+12;jj++){
if(imgjbsbdata[ii*step+jj*channels]>=200)
sumjb2=sumjb2+imgjbsbdata[ii*step+jj*channels];
}
if(sumjb2>=255*(100-jbblack)){ //允许角标内黑点数,第二次提取
jbi=i;//脚标位置
jbj=j;
for(ii=i-2;ii<i+22;ii=ii+2) //标出位置
for(jj=j-2;jj<j+22;jj=jj+2){
imgjbsbdata[ii*step+jj*channels]=0;
}
brklab=1;
break;
}
}
}
if(1==brklab){
brklab=0;break;//退出标记
}
}
if(jbi==0 && jbj==0)
{
// jbi=142;
// jbj=25;
jbi=0;
jbj=0;
printf("\t\t\t甄别图像的角标失败,使用设定值Cjbsb角标识别\n");
}
cout<<"JB:"<<jbi<<" "<<jbj<<endl;
//----------------------------------------//
//3、以角标为起点进行裁剪与画框
//----------------------------------------//
// CvSize jbcjsize=cvSize(835,165); //角标裁剪框的大小宽为835象素高为165象素
CvSize jbcjsize=cvSize(833, 476);
IplImage* imgjbcj = cvCreateImage(jbcjsize,img->depth,img->nChannels);
uchar *imgjbcjdata= (uchar *)imgjbcj->imageData;
int jbcjstep = imgjbcj->widthStep;
int jbcjchannels = imgjbcj->nChannels;
for(i=0;i<476;i++){
for(j=0;j<833;j++){
imgjbcjdata[i*jbcjstep+j*jbcjchannels]=data[(i+jbi)*step+(j+jbj)*channels];
}
}
//此处是yss进行注释没发现有什么用处
for(i=0;i<476;i=i+2){
imgjbsbdata[(i+jbi)*step+jbj*channels]=0;
imgjbsbdata[(i+jbi)*step+(jbj+833)*channels]=0;
}
for(j=0;j<833;j=j+2){
imgjbsbdata[jbi*step+(j+jbj)*channels]=0;
imgjbsbdata[(jbi+476)*step+(j+jbj)*channels]=0;
}
return imgjbcj;
}

View File

@@ -0,0 +1,29 @@
/*
IplImage* Integral(IplImage* img, int width, int height)
{
unsigned long *columnSum = new unsigned long[width]; // sum of each column
// calculate integral of the first line
for(int i=0;i<width;i++)
{
columnSum[i]=inputMatrix[i];
outputMatrix[i] = inputMatrix[i];
if(i>0)
{
outputMatrix[i] += outputMatrix[i-1];
}
}
for (int i=1;i<height;i++)
{
int offset = i*width;
// first column of each line
columnSum[0] +=inputMatrix[offset];
outputMatrix[offset] = columnSum[0];
// other columns
for(int j=1;j<width;j++)
{
columnSum[j] += inputMatrix[offset+j];
outputMatrix[offset+j] = outputMatrix[offset+j-1] + columnSum[j];
}
}
return 0;
} */

View File

@@ -0,0 +1,9 @@
class Point{
private:
public:
int x;
int y;
void setpoint(int a,int b){x=a;y=b;}
};

View File

@@ -0,0 +1,327 @@
//**************************************************************************
//Thinner.cpp
//细化算法实现文件
//**************************************************************************
//#include "StdAfx.h"
#include <stdlib.h>
#include <malloc.h>
#include "Thinner.h"
#include <stdio.h>
//将图图像反色
void beforethin(unsigned char *ip, unsigned char *jp, unsigned long lx, unsigned long ly){
//void beforethin(char *ip, char *jp, unsigned long lx, unsigned long ly)
unsigned long i,j;
for(i=0; i<ly; i++){
for(j=0; j<lx; j++){
//这里要视前景是白点还是黑点而定,可以改动
//如果前景是白点,就是这样;反之反过来
//jp[i*lx+j]=ip[i*lx+j];
/* jp[i*lx+j]=255;*/
if(ip[i*lx+j]>0)
jp[i*lx+j]=0;
else
jp[i*lx+j]=255;
}
}
}
/////////////////////////////////////////////////////////////////////////
//Rosenfeld细化算法
//功能:对图象进行细化
//参数image代表图象的一维数组
// lx图象宽度
// ly图象高度
// 无返回值
void ThinnerRosenfeld(void *image, unsigned long lx, unsigned long ly){
char *f, *g;
char n[10];
char a[5] = {0, -1, 1, 0, 0};
char b[5] = {0, 0, 0, 1, -1};
char nrnd, cond, n48, n26, n24, n46, n68, n82, n123, n345, n567, n781;
short k, shori;
unsigned long i, j;
long ii, jj, kk, kk1, kk2, kk3, size;
size = (long)lx * (long)ly;
g = (char *)malloc(size);
if(g==NULL){
printf("error in alocating mmeory!\n");
return;
}
f = (char *)image;
for(kk=0l; kk<size; kk++){
g[kk] = f[kk];
}
do{
shori = 0;
for(k=1; k<=4; k++){
for(i=1; i<lx-1; i++){
ii = i + a[k];
for(j=1; j<ly-1; j++){
kk = i*ly + j;
if(!f[kk])
continue;
jj = j + b[k];
kk1 = ii*ly + jj;
if(f[kk1])
continue;
kk1 = kk - ly -1;
kk2 = kk1 + 1;
kk3 = kk2 + 1;
n[3] = f[kk1];
n[2] = f[kk2];
n[1] = f[kk3];
kk1 = kk - 1;
kk3 = kk + 1;
n[4] = f[kk1];
n[8] = f[kk3];
kk1 = kk + ly - 1;
kk2 = kk1 + 1;
kk3 = kk2 + 1;
n[5] = f[kk1];
n[6] = f[kk2];
n[7] = f[kk3];
nrnd = n[1] + n[2] + n[3] + n[4]
+n[5] + n[6] + n[7] + n[8];
if(nrnd<=1)
continue;
cond = 0;
n48 = n[4] + n[8];
n26 = n[2] + n[6];
n24 = n[2] + n[4];
n46 = n[4] + n[6];
n68 = n[6] + n[8];
n82 = n[8] + n[2];
n123 = n[1] + n[2] + n[3];
n345 = n[3] + n[4] + n[5];
n567 = n[5] + n[6] + n[7];
n781 = n[7] + n[8] + n[1];
if(n[2]==1 && n48==0 && n567>0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[6]==1 && n48==0 && n123>0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[8]==1 && n26==0 && n345>0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[4]==1 && n26==0 && n781>0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[5]==1 && n46==0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[7]==1 && n68==0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[1]==1 && n82==0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
if(n[3]==1 && n24==0){
if(!cond)
continue;
g[kk] = 0;
shori = 1;
continue;
}
cond = 1;
if(!cond)
continue;
g[kk] = 0;
shori = 1;
}
}
for(i=0; i<lx; i++){
for(j=0; j<ly; j++){
kk = i*ly + j;
f[kk] = g[kk];
}
}
}
}while(shori);
free(g);
}
/////////////////////////////////////////////////////////////////////////
//基于索引表的细化细化算法
//功能:对图象进行细化
//参数lpDIBBits代表图象的一维数组
// lWidth图象高度
// lHeight图象宽度
// 无返回值
/*
BOOL WINAPI ThiningDIBSkeleton (LPSTR lpDIBBits, LONG lWidth, LONG lHeight){
//循环变量
long i;
long j;
long lLength;
unsigned char deletemark[256] = {
0,0,0,0,0,0,0,1, 0,0,1,1,0,0,1,1,
0,0,0,0,0,0,0,0, 0,0,1,1,1,0,1,1,
0,0,0,0,0,0,0,0, 1,0,0,0,1,0,1,1,
0,0,0,0,0,0,0,0, 1,0,1,1,1,0,1,1,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0, 1,0,0,0,1,0,1,1,
1,0,0,0,0,0,0,0, 1,0,1,1,1,0,1,1,
0,0,1,1,0,0,1,1, 0,0,0,1,0,0,1,1,
0,0,0,0,0,0,0,0, 0,0,0,1,0,0,1,1,
1,1,0,1,0,0,0,1, 0,0,0,0,0,0,0,0,
1,1,0,1,0,0,0,1, 1,1,0,0,1,0,0,0,
0,1,1,1,0,0,1,1, 0,0,0,1,0,0,1,1,
0,0,0,0,0,0,0,0, 0,0,0,0,0,1,1,1,
1,1,1,1,0,0,1,1, 1,1,0,0,1,1,0,0,
1,1,1,1,0,0,1,1, 1,1,0,0,1,1,0,0
};//索引表
unsigned char p0, p1, p2, p3, p4, p5, p6, p7;
unsigned char *pmid, *pmidtemp;
unsigned char sum;
int changed;
bool bStart = true;
lLength = lWidth * lHeight;
unsigned char *pTemp = (unsigned char *)malloc(sizeof(unsigned char) * lWidth * lHeight);
// P0 P1 P2
// P7 P3
// P6 P5 P4
while(bStart){
bStart = false;
changed = 0;
//首先求边缘点(并行)
pmid = (unsigned char *)lpDIBBits + lWidth + 1;
memset(pTemp, (BYTE) 0, lLength);
pmidtemp = (unsigned char *)pTemp + lWidth + 1;
for(i = 1; i < lHeight -1; i++){
for(j = 1; j < lWidth - 1; j++){
if( *pmid == 0){
pmid++;
pmidtemp++;
continue;
}
p3 = *(pmid + 1);
p2 = *(pmid + 1 - lWidth);
p1 = *(pmid - lWidth);
p0 = *(pmid - lWidth -1);
p7 = *(pmid - 1);
p6 = *(pmid + lWidth - 1);
p5 = *(pmid + lWidth);
p4 = *(pmid + lWidth + 1);
sum = p0 & p1 & p2 & p3 & p4 & p5 & p6 & p7;
if(sum == 0){
*pmidtemp = 1;
}
pmid++;
pmidtemp++;
}
pmid++;
pmid++;
pmidtemp++;
pmidtemp++;
}
//现在开始串行删除
pmid = (unsigned char *)lpDIBBits + lWidth + 1;
pmidtemp = (unsigned char *)pTemp + lWidth + 1;
for(i = 1; i < lHeight -1; i++){
for(j = 1; j < lWidth - 1; j++){
if( *pmidtemp == 0){
pmid++;
pmidtemp++;
continue;
}
p3 = *(pmid + 1);
p2 = *(pmid + 1 - lWidth);
p1 = *(pmid - lWidth);
p0 = *(pmid - lWidth -1);
p7 = *(pmid - 1);
p6 = *(pmid + lWidth - 1);
p5 = *(pmid + lWidth);
p4 = *(pmid + lWidth + 1);
p1 *= 2;
p2 *= 4;
p3 *= 8;
p4 *= 16;
p5 *= 32;
p6 *= 64;
p7 *= 128;
sum = p0 | p1 | p2 | p3 | p4 | p5 | p6 | p7;
if(deletemark[sum] == 1){
*pmid = 0;
bStart = true;
}
pmid++;
pmidtemp++;
}
pmid++;
pmid++;
pmidtemp++;
pmidtemp++;
}
}
return true;
}
*/

View File

@@ -0,0 +1,11 @@
//***************************************************************************
// 文件Thinner.h
// 功能:四种不同的细化算法
//***************************************************************************
void beforethin(unsigned char *ip,unsigned char *jp, unsigned long lx, unsigned long ly);
void ThinnerHilditch(void *image, unsigned long lx, unsigned long ly);
void ThinnerPavlidis(void *image, unsigned long lx, unsigned long ly);
void ThinnerRosenfeld(void *image, unsigned long lx, unsigned long ly);
//注意该函数lWidth应该是Height
//BOOL WINAPI ThiningDIBSkeleton (LPSTR lpDIBBits, LONG lWidth, LONG lHeight);

View File

@@ -0,0 +1,46 @@
/* 程序名binary.c
功能:读入图像文件,进行二值化
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
int* binary(IplImage* img,int bithro)
{
int height,width,step,channels;
uchar *data;
int i,j;
static int black[1000]; //C语言不提倡返回一个局部变量的地址以外的功能所以你必须定义的局部变量如静态变量。
/* 获取图像信息*/
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
/*二值化,并统计黑像素的个数*/
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)//对图像每个点进行二值化,原值为128
data[i*step+j*channels]=(data[i*step+j*channels]>bithro)?255:0;
}
/*计算每一行的黑像素个数*/
int tempBlackPixel=0;
memset(black,0,1000); //##初始化内存这里用做清零black数组
for(i=height-1;i>0;i--)
{
for(int j=0;j<width;j++)
{
if(data[i*step+j*channels]==0) //计算黑色的像素数
tempBlackPixel+=1;
}
black[height-i]=tempBlackPixel; //black记录黑色像素数
tempBlackPixel=0;
}
//二值化,并统计黑像素的个数**********
return black;
}

View File

@@ -0,0 +1,37 @@
/* 程序名getFiles.c
功能:返回一个文件夹下的所有文件名
*/
#include<io.h>
#include <stdio.h>
#include<vector>
#include<iostream>
using namespace std;
#include <string.h>
void getFiles(string path, vector<string>& files ){
using namespace std;//引入整个名空间
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
//如果是目录,迭代之
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
} //如果不是,加入列表
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}

View File

@@ -0,0 +1,38 @@
/* 程序名getFolders.c
功能:返回一个文件夹下的所有文件夹的名称
*/
#include<io.h>
#include <stdio.h>
#include<vector>
#include<iostream>
using namespace std;
#include <string.h>
int getFolders(string path, vector<string>& files )
{
using namespace std;//引入整个名空间
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
int i=0;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
printf("文件夹:%s\n",files[i].c_str());
i++;
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return 0;
}

View File

@@ -0,0 +1,65 @@
/* 程序名getFolders.c
功能:返回一个文件夹下的所有文件夹的名称
*/
#include<io.h>
#include <stdio.h>
#include<vector>
#include<iostream>
using namespace std;
#include <string.h>
int getFolders(string path, vector<string>& files )
{
using namespace std;//引入整个名空间
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
/*
hFile=_findfirst(p.assign(path).append("\\*").c_str(),&fileinfo); //第一次查找 to_search???
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
if(-1==hFile)return -1; //当前文件夹下没有子文件
printf("%s\n",fileinfo.name); //打印出找到的文件的文件名
int i=0;
while(!_findnext(hFile,&fileinfo)) //循环查找其他符合的文件,知道找不到其他的为止
{
printf("%s\n",files[i].c_str());
i++;
}
_findclose(hFile); //别忘了关闭句柄
system("pause");
return 0;
*/
int i=0;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
//if((fileinfo.attrib & _A_SUBDIR))
//{
// if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
// getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
//}
//else
//{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
printf("文件夹:%s\n",files[i].c_str());
i++;
}
//}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return 0;
}

View File

@@ -0,0 +1,27 @@
/*程序名getType.c
功能:读入图像文件名,得到图像类型
*/
#include <stdio.h>
#include <string.h>
char * getType(char fileName[], char type[])
{
int i=strlen(fileName)-1, j;
char ch;
for(type[0]='\0';i>=0;i--)
{
if(fileName[i] == '.')
{// 遇到文件类型分隔符
for(j=i; fileName[j]!='\0'; j++)
{
ch = fileName[j];
type[j-i] = ('A'<=ch && ch<='Z') ? (ch+'a'-'A'): ch;
}
type[j-i] = '\0';
break;
}
else if(fileName[i] == '/' || fileName[i]=='\\') break;// 遇到目录分割符,退出
}
return type;
}

View File

@@ -0,0 +1,58 @@
/* 程序名gif2ipl.c
功能输入gif图像。得到相应的rgb图像
*/
#include <cv.h>
#include <highgui.h>
#include "FreeImage.h"
#include <stdio.h>
IplImage* gif2ipl(const char* filename)
{
FreeImage_Initialise(); //load the FreeImage function lib
FREE_IMAGE_FORMAT fif = FIF_GIF;
FIBITMAP* fiBmp = FreeImage_Load(fif,filename,GIF_DEFAULT);
FIMULTIBITMAP * pGIF=FreeImage_OpenMultiBitmap(fif,filename,0,1,0,GIF_PLAYBACK);
// FIBITMAPINFO fiBmpInfo = getfiBmpInfo(fiBmp);
int gifImgCnt=FreeImage_GetPageCount(pGIF);
FIBITMAP * pFrame;
int width,height;
width=FreeImage_GetWidth(fiBmp);
height=FreeImage_GetHeight(fiBmp);
IplImage * iplImg = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
iplImg->origin = 1;//should set to 1-top-left structure(Windows bitmap style)
RGBQUAD* ptrPalette =new RGBQUAD; // = FreeImage_GetPalette(fiBmp);
BYTE intens;
BYTE* pIntensity = &intens;
//cvNamedWindow("gif",0);
//printf("gif包含图片的数目%d \n",gifImgCnt);
for (int curFrame=0;curFrame<gifImgCnt;curFrame++)
{
pFrame= FreeImage_LockPage(pGIF,curFrame);
//ptrPalette = FreeImage_GetPalette(pFrame);
char * ptrImgDataPerLine;
for (int i=0;i<height;i++)
{
ptrImgDataPerLine = iplImg->imageData + i*iplImg->widthStep;
for(int j=0;j<width;j++)
{
//get the pixel index
//FreeImage_GetPixelIndex(pFrame,j,i,pIntensity);
FreeImage_GetPixelColor(pFrame,j,i,ptrPalette);
ptrImgDataPerLine[3*j] = ptrPalette->rgbBlue;
ptrImgDataPerLine[3*j+1] = ptrPalette->rgbGreen;
ptrImgDataPerLine[3*j+2] = ptrPalette->rgbRed;
//ptrImgDataPerLine[3*j] = ptrPalette[intens].rgbBlue;
//ptrImgDataPerLine[3*j+1] = ptrPalette[intens].rgbGreen;
//ptrImgDataPerLine[3*j+2] = ptrPalette[intens].rgbRed;
}
}
//printf("转换结束的图片序号: %d \n",curFrame);
// cvShowImage("gif",iplImg);
// cvWaitKey(30);
// FreeImage_UnlockPage(pGIF,pFrame,1);
}
FreeImage_Unload(fiBmp);
FreeImage_DeInitialise();
return iplImg;
}

View File

@@ -0,0 +1,146 @@
# Microsoft Developer Studio Project File - Name="handwriting" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=handwriting - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "handwriting.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "handwriting.mak" CFG="handwriting - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "handwriting - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "handwriting - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "handwriting - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x804 /d "NDEBUG"
# ADD RSC /l 0x804 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "handwriting - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x804 /d "_DEBUG"
# ADD RSC /l 0x804 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 FreeImage.lib cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "handwriting - Win32 Release"
# Name "handwriting - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\binary.cpp
# End Source File
# Begin Source File
SOURCE=.\Cjbsb.cpp
# End Source File
# Begin Source File
SOURCE=.\getFiles.cpp
# End Source File
# Begin Source File
SOURCE=.\getFloders.cpp
# End Source File
# Begin Source File
SOURCE=.\getType.cpp
# End Source File
# Begin Source File
SOURCE=.\gif2ipl.cpp
# End Source File
# Begin Source File
SOURCE=.\read_scanf.cpp
# End Source File
# Begin Source File
SOURCE=.\searchDir.cpp
# End Source File
# Begin Source File
SOURCE=.\segmentation.cpp
# End Source File
# Begin Source File
SOURCE=.\singlefeature.cpp
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\Cword.h
# End Source File
# Begin Source File
SOURCE=.\Point.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

View File

@@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# 警告: 不能编辑或删除该工作区文件!
###############################################################################
Project: "handwriting"=.\handwriting.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

View File

@@ -0,0 +1,49 @@
<html>
<body>
<pre>
<h1>Build Log</h1>
<h3>
--------------------Configuration: handwriting - Win32 Debug--------------------
</h3>
<h3>Command Lines</h3>
Creating temporary file "C:\Users\闫帅帅\AppData\Local\Temp\RSP9F62.tmp" with contents
[
/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/handwriting.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c
"D:\CODE\HWCV\测试\转换图像文件到jpg\segmentation.cpp"
]
Creating command line "cl.exe @"C:\Users\闫帅帅\AppData\Local\Temp\RSP9F62.tmp""
Creating temporary file "C:\Users\闫帅帅\AppData\Local\Temp\RSP9F63.tmp" with contents
[
FreeImage.lib cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/handwriting.pdb" /debug /machine:I386 /out:"Debug/handwriting.exe" /pdbtype:sept
".\Debug\binary.obj"
".\Debug\Cjbsb.obj"
".\Debug\getFiles.obj"
".\Debug\getFloders.obj"
".\Debug\getType.obj"
".\Debug\gif2ipl.obj"
".\Debug\read_scanf.obj"
".\Debug\searchDir.obj"
".\Debug\segmentation.obj"
".\Debug\singlefeature.obj"
]
Creating command line "link.exe @"C:\Users\闫帅帅\AppData\Local\Temp\RSP9F63.tmp""
<h3>Output Window</h3>
Compiling...
segmentation.cpp
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(43) : warning C4101: 'end' : unreferenced local variable
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(44) : warning C4101: 'cost' : unreferenced local variable
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(60) : warning C4101: 'ii' : unreferenced local variable
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(60) : warning C4101: 'jj' : unreferenced local variable
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(60) : warning C4101: 'j' : unreferenced local variable
d:\code\hwcv\测试\转换图像文件到jpg\segmentation.cpp(83) : warning C4101: 'x' : unreferenced local variable
c:\program files\vc\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\vc\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Linking...
<h3>Results</h3>
handwriting.exe - 0 error(s), 0 warning(s)
</pre>
</body>
</html>

View File

@@ -0,0 +1,46 @@
/* 程序名outline.c
功能:输入文字图像。得到相应的轮廓图
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
IplImage* outline(IplImage* imgbj)
{
/*定义变量*/
int i,j;
int height,width,step,channels;
uchar *data;
/*定义新的图像*/
IplImage* imglk = cvCreateImage(cvGetSize(imgbj),imgbj->depth,imgbj->nChannels);
/* 获取图像信息*/
height = imgbj->height;
width = imgbj->width;
step = imgbj->widthStep;
channels = imgbj->nChannels;
data = (uchar *)imgbj->imageData;
for(j=0;j<height;j++)
{
for(int i=0;i<width;i++)
imglk->imageData[j*step+i*channels]=255;
for( i=0;i<width-1;i++)
if(data[j*step+(i+1)*channels]-data[j*step+i*channels]==255) //竖线右侧框
imglk->imageData[j*step+i*channels]=0;
else if(data[j*step+i*channels]-data[j*step+(i+1)*channels]==255) //竖线左侧框
imglk->imageData[j*step+(i+1)*channels]=0;
}
for(i=0;i<width;i++)
for(j=0;j<height-1;j++)
if(data[j*step+i*channels]-data[(j+1)*step+i*channels]==255) //横线下侧框
imglk->imageData[(j+1)*step+i*channels]=0;
else if(data[(j+1)*step+i*channels]-data[j*step+i*channels]==255) //横线上侧框
imglk->imageData[j*step+i*channels]=0;
return imglk;
}

View File

@@ -0,0 +1,262 @@
/* 程序名outline.c
功能:输入文字另外由于轮廓图像。返回相应的轮廓特征值
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include "Point.h"
int outlinefeature(IplImage* imglk,int feature[ ][50]){
/*定义变量*/
int i,j;
int height,width,step,channels;
uchar *data;
int feat[50][50]={0}; //特征值初始化
Point featblk[32]; //标记相同H的黑点坐标
int featk; //标记相同H的黑点数目
int m; //for 里面的变量
/* 获取图像信息*/
height = imglk->height;
width = imglk->width;
step = imglk->widthStep;
channels = imglk->nChannels;
data = (uchar *)imglk->imageData;
//初始化特征矩阵 最大值为47 非空的特征字有1081个
int outllab[9][9]={\
{ 3,37,10,36, 2,35, 9,34, 1},\
{38, 3,21,20, 2,19,18, 1,33},\
{11,22, 3,10, 2, 9, 1,17, 8},\
{39,23,11, 3, 2, 1, 8,16,32},\
{ 4, 4, 4, 4, 0, 0, 0, 0, 0},\
{40,24,12, 5, 6, 7,15,31,47},\
{12,25, 5,13, 6,14, 7,30,15},\
{41, 5,26,27, 6,28,29, 7,46},\
{ 5,42,13,43, 6,44,14,45, 7}};
for(i=4;i<=width-5;i++)
{
for(j=4;j<=height-5;j++)
{
if(data[j*step+i*channels]==0)
{
//**************H=1
memset(featblk, 0, sizeof(Point)*32); //归零
featk=0;
if(data[j*step+(i+1)*channels]==0) //右侧点
{
featblk[featk].x=i+1;
featblk[featk].y=j;
featk++;
}
for(m=i+1;m>=i-1;m--) //上排点
{
if(data[(j-1)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j-1;
featk++;
}
}
if(data[j*step+(i-1)*channels]==0) //左侧点
{
featblk[featk].x=i-1;
featblk[featk].y=j;
featk++;
}
for(m=i-1;m<=i+1;m++) //下排点
{
if(data[(j+1)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j+1;
featk++;
}
}
//统计特征点
//****************************************************
if(featk>=2)
{
for(m=1;m<=featk-1;m++)
{
feat[outllab[featblk[m-1].x-i+4][featblk[m-1].y-j+4]][outllab[featblk[m].x-i+4][featblk[m].y-j+4]]++;
}
}
//H=1
//H=2
memset(featblk, 0, sizeof(Point)*32); //归零
featk=0;
for(m=j+1;m>=j-2;m--)
{
if(data[m*step+(i+2)*channels]==0) //右排点
{
featblk[featk].x=i+2;
featblk[featk].y=m;
featk++;
}
}
for(m=i+1;m>=i-2;m--) //上排点
{
if(data[(j-2)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j-2;
featk++;
}
}
for(m=j-1;m<=j+2;m++) //左侧点
{
if(data[m*step+(i-2)*channels]==0)
{
featblk[featk].x=i-2;
featblk[featk].y=m;
featk++;
}
}
for(m=i-1;m<=i+2;m++) //下排点
{
if(data[(j+2)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j+2;
featk++;
}
}
//统计特征点
//****************************************************
if(featk>=2)
{
for(m=1;m<=featk-1;m++)
{
feat[outllab[featblk[m-1].x-i+4][featblk[m-1].y-j+4]][outllab[featblk[m].x-i+4][featblk[m].y-j+4]]++;
}
}
//H=2
//H=3
memset(featblk, 0, sizeof(Point)*32); //归零
featk=0;
for(m=j+2;m>=j-3;m--)
{
if(data[m*step+(i+3)*channels]==0) //右排点
{
featblk[featk].x=i+3;
featblk[featk].y=m;
featk++;
}
}
for(m=i+2;m>=i-3;m--) //上排点
{
if(data[(j-3)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j-3;
featk++;
}
}
for(m=j-2;m<=j+3;m++) //左侧点
{
if(data[m*step+(i-3)*channels]==0)
{
featblk[featk].x=i-3;
featblk[featk].y=m;
featk++;
}
}
for(m=i-2;m<=i+3;m++) //下排点
{
if(data[(j+3)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j+3;
featk++;
}
}
//统计特征点
//******************************************
if(featk>=2)
{
for(m=1;m<=featk-1;m++)
{
feat[outllab[featblk[m-1].x-i+4][featblk[m-1].y-j+4]][outllab[featblk[m].x-i+4][featblk[m].y-j+4]]++;
}
}
//H=3
//H=4
memset(featblk, 0, sizeof(Point)*32); //归零
featk=0;
for(m=j+3;m>=j-4;m--)
{
if(data[m*step+(i+4)*channels]==0) //右排点
{
featblk[featk].x=i+4;
featblk[featk].y=m;
featk++;
}
}
for(m=i+3;m>=i-4;m--) //上排点
{
if(data[(j-4)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j-4;
featk++;
}
}
for(m=j-3;m<=j+4;m++) //左侧点
{
if(data[m*step+(i-4)*channels]==0)
{
featblk[featk].x=i-4;
featblk[featk].y=m;
featk++;
}
}
for(m=i-3;m<=i+4;m++) //下排点
{
if(data[(j+4)*step+m*channels]==0)
{
featblk[featk].x=m;
featblk[featk].y=j+4;
featk++;
}
}
//统计特征点
if(featk>=2)
{
for(m=1;m<=featk-1;m++)
{
feat[ outllab[featblk[m-1].x-i+4][featblk[m-1].y-j+4]] [outllab[featblk[m].x-i+4][featblk[m].y-j+4] ]++;
}
}
//H=4***********************
}
}
}
//****注最终特征值为feat(x,y)+feat(y,x)放入feat(x,y)中x<y
for(i=1;i<50;i++)
for(j=0;j<i;j++)
{
feat[j][i]=feat[i][j]+feat[j][i];
feat[i][j]=0;
}
memcpy(feature,feat,2500*4); //int有四个字节
return 0;
}

View File

@@ -0,0 +1,26 @@
// 功能将filename 中的数据共cols列读取到_vector中_vector可视为二维数组
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int read_scanf(const string &filename,const int &cols,vector<double *> &_vector)
{
FILE *fp=fopen(filename.c_str(),"r");
bool flag=true;
int i=0;
if(!fp) { cout<<"File open error!\n"; return 0; }
while(flag)
{
double *ptr=new double[cols];
for(i=0;i<cols;i++)
{ //读取数据存在_vector[cols]中
if(EOF==fscanf(fp,"%lf",&ptr[i])){flag=false;break;};
if(EOF==fgetc(fp)){flag=false;i++;break;}
}
if(cols==i) _vector.push_back(ptr);
}
fclose(fp);
return 1;
}

View File

@@ -0,0 +1,44 @@
/* 程序名getFiles.c
功能:返回一个文件夹下的所有文件名
*/
#include<io.h>
#include <stdio.h>
#include<vector>
#include<iostream>
#include <string.h>
#include <string>
using namespace std;
int searchDir( char *path, vector<string> &dir)
{
using namespace std;
struct _finddata_t fa;//创建找到的结构体
long handle;
int flag=0;
char temp[100]={0};
string path_temp=path;
// path_temp=path_temp.substr(0,path_temp.length()-1);
if((handle = _findfirst(strcat(path,"*"),&fa)) == -1L)//如果不是目录的话
return 0;
do//是目录,先执行循环
{
if( fa.attrib == _A_SUBDIR && ~strcmp(fa.name,".")&& ~strcmp(fa.name,".."))
{
strcat( temp, path_temp.c_str());
strcat( temp, fa.name);
if(flag++)
dir.push_back(temp);
else;
memset(temp,0,100);
}
}while(_findnext(handle,&fa) == 0); /* 成功找到时返回0*/
_findclose(handle);
return 0;
}

View File

@@ -0,0 +1,94 @@
/* 程序名segmentation.c
功能:总程序:读入图像文件,分析特征,输出效果
*/
//#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include "Point.h"
#include "FreeImage.h" //用于读gif的图像,将gif图像转换为png
#include<io.h> //下面的5个用于读取文件夹下的所有文件名
#include<vector>
#include<iostream>
#include <string.h>
#include<windows.h> //用于弹出提示框,,,切记!当调用<windows.h>时不要调用MFCAfx.h)
#include<string.h>
using namespace std;
#ifdef WIN32 //屏蔽VC6对STL的一些不完全支持造成
#pragma warning (disable: 4514 4786)
#endif
/*-----------各种声明-----------------------*/
void getFiles(string path, vector<string>& files );//9、读取文件名下所有文件名
char* getType(char fileName[], char type[]); //2、获取图像类型
int* binary(IplImage* img,int bithro); //3、二值化图像
int getFolders(string path, vector<string>& files );//11、读取文件名下所有文件夹的名称
int read_scanf(const string &filename,const int &cols,vector<double *> &_vector);//12、读取已经存好的特征值
int searchDir(char* path, vector<string> &dir);//获取目录下一层的所有文件夹
IplImage* Cjbsb(IplImage* img,IplImage* imgjbsb,int jbwhite,int jbblack);//4、图像角标识别
IplImage* gif2ipl(const char* filename); //1、读取gif的外部函数
IplImage* singlefeature(char* path,int feature[ ][50],int flag);//10、得出单个文件的特征值
int pos_x=0,pos_y=0;
bool pos_flag=false;
IplImage* src;
int picAll=0,picSus=0;
int main()
{
time_t start ,end ;//计时
double cost; time(&start);
//定义变量
//------------------------------------------------------//
char path[100] = "D:\\xiangmu\\Img\\imgjiaobiao3\\";
vector<string> dir; //存储目录
int conti=1; //对比图像的标号
int size_dir,num_dir;
searchDir(path, dir);//获取filePath下的所有一级目录并存储到dir中
size_dir=dir.size(); //dir的大小就是学生的数量
//开始转换
//-------------------------------------------------------------//
for(num_dir=0;num_dir<size_dir;num_dir++)//对每一个学生目录进行循环
{
int size,i,j,ii,jj; //通用变量
char str[80]; //存储地址
int featx[30][50];
vector<string> files; //存储文件路径
getFiles(dir[num_dir].c_str(), files ); //遍历当前文件夹下的所有文件
//输出
printf("\n第%d目录%s",num_dir,dir[num_dir].c_str());
size = files.size(); //图像的数目
//输出
printf("\t个数:%d\t",size);
//开始对每一张图片进行处理
//------------------------------------------------------//
int flag=1;
for (i = 0;i < size;i++)
{
memset(str,0,sizeof(str));
strcpy(str,files[i].c_str());
try
{
singlefeature(str,featx,flag);//str图片路径 featx图片特征值存在变量中featx中
}catch(int x)
{
}
flag=0;
}
}
return 0;
}

View File

@@ -0,0 +1,135 @@
/* 程序名singlefeature.c
功能:分总程序:读入图像文件,得出单个文件的特征值
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include "Point.h"
#include "FreeImage.h" //用于读gif的图像
#include<io.h> //下面的5个用于读取文件夹下的所有文件名
#include<vector>
#include<iostream>
using namespace std;
#include <string.h>
#include <direct.h>
#include"Thinner.h"
/*各种声明*/
void getFiles(string path, vector<string>& files );//读取文件名下所有文件
char* getType(char fileName[], char type[]); //获取图像类型
int* binary(IplImage* img,int bithro); //二值化图像
int outlinefeature(IplImage* imglk,int feature[ ][50]);//计算图像的轮廓特征值
IplImage* Cjbsb(IplImage* img,IplImage* imgjbsb,int jbwhite,int jbblack);//图像角标识别
IplImage* gif2ipl(const char* filename); //读取gif的外部函数
IplImage* singlefeature(char* path,int feature[ ][50],int flag){
//定义变量
IplImage* img = 0; //原图
IplImage* imglk = 0; //轮廓图
IplImage* imggj = 0; //骨架图
IplImage* imgjbsb = 0; //角标识别图
IplImage* imgbj = 0; //只提取笔记部分的图像
char imgtype[10]; //判断图像类型
int height,width,step,channels;
uchar *data;
int *black; //用于返回图像每行黑像素的个数
//int feature[50][50]={0}; //特征值初始化
getType(path, imgtype);
if(strcmp(".gif", imgtype) == 0)
{
IplImage* Iplimg=gif2ipl(path); //gif 转 rgb 三维
img=cvCreateImage(cvGetSize(Iplimg),Iplimg->depth,1);
cvCvtColor(Iplimg,img,CV_RGB2GRAY); //rgb 转灰度
cvReleaseImage(&Iplimg); //释放临时图像的内存
cvFlip(img,NULL, 0); //由于得到的灰度图是翻转的,所以再翻转回来
}
else if(strcmp(".jpg", imgtype) == 0 || strcmp(".png", imgtype) == 0)
img=cvLoadImage(path,0);
else
return NULL;
if(!img)
{
printf("Could not load image file: %s\n",path);
exit(0);
}
// 获取图像信息
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
//开始处理
//图像放大
IplImage* imgbig = 0; //原图的放大图
CvSize dst_cvsize; //目标图像的大小
float scale=1;
if(width<850){
scale=(float)850/width;
dst_cvsize.width=850;
dst_cvsize.height=(int)(height*scale);
}
else
{
dst_cvsize.width=width;
dst_cvsize.height=height;
}
imgbig=cvCreateImage(dst_cvsize,img->depth,img->nChannels);
cvResize(img,imgbig,CV_INTER_LINEAR); // CV_INTER_NN - 最近邻插值,
//CV_INTER_LINEAR - 双线性插值 (缺省使用),
//CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。
//CV_INTER_CUBIC - 立方插值.
//二值化
int bithro=230; //输入二值化的阈值 (0--255)
black=binary(imgbig,bithro); //二值化,并统计黑像素的个数,返回每行黑像素的个数(black)
//cvNamedWindow("二值化结果图",CV_WINDOW_AUTOSIZE); //显示图像
//cvShowImage("二值化结果图",img);
//printf("二值化求解完成!!\n");
//角标识别,裁切出图像
int jbwhite=5,jbblack=4;
imgjbsb = cvCreateImage(cvGetSize(imgbig),imgbig->depth,imgbig->nChannels);
imgbj=Cjbsb(imgbig,imgjbsb,jbwhite,jbblack); //返回文字的笔迹部分
//存储裁剪后的图像
//D:/xiangmu/Img/imgjiaobiao/010211100059x/0359.gif
char isavePath[200]="E";
//此处需要新建一个新建文件夹的选项
string name=path;
strcat(isavePath, name.substr(1, 39).c_str());
string md="mkdir ";
md += isavePath;
//组合出新名字
strcat(isavePath, name.substr(40, 5).c_str());
strcat(isavePath, ".jpg");
//创建文件夹
if(flag) system(md.c_str());
//保存图像
cvSaveImage(isavePath,imgbj);
cvReleaseImage(&imgbig);
cvReleaseImage(&img );
cvReleaseImage(&imgbj );
cvDestroyAllWindows();
cout<<".";
return imggj;
}

View File

@@ -0,0 +1,136 @@
/* 程序名Cjbsb.c
功能:读入只有文字区域的图像文件,将文字划分开来
输入参数:只有文字区域的图像文件,行阈值,列阈值,划格后的行标与列标
默认hthro=10wthro=6
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
IplImage* worddivide(IplImage* imgbj,int hthro,int wthro,int *gridx,int *gridy,int *gxx,int *gyy){
/*定义变量*/
int height,width,step,channels;
uchar *data;
int i,j,black[1000];
int blackend=0; //标记分割线结束
int mi=0,mx=500; //标记分割线内含黑色最少的线号与值
int gx=0,gy=0; //记录该画线的网线的行号与列号 gridx[10],gridy[30],
memset(gridx,0,10); //初始化内存,这里用做清零
memset(gridy,0,30); //初始化内存,这里用做清零
/*定义新的图像*/
IplImage* imgbjhf = cvCreateImage(cvGetSize(imgbj),imgbj->depth,imgbj->nChannels); //笔迹划分图
cvCopy(imgbj,imgbjhf,NULL);
/* 获取图像信息*/
height = imgbjhf->height;
width = imgbjhf->width;
step = imgbjhf->widthStep;
channels = imgbjhf->nChannels;
data = (uchar *)imgbjhf->imageData;
/*横向的表格*/
/*计算每一行的黑色像素点数(此参数不能使用二值化得到的)*/
int tempBlackPixelx=0; //循环记录每一行的黑色像素点数
memset(black,0,1000); //初始化内存,这里用做清零
for(j=0;j<height;j++){
for(i=0;i<width;i++){
if(data[j*step+i*channels]==0) //计算黑色的像素数
tempBlackPixelx+=1;
}
black[j]=tempBlackPixelx; //black记录黑色像素数
tempBlackPixelx=0;
//printf("The %dth black num is %d \n",j,black[j]);
}
/*计算横线位置*/
for(i=0;i<height;i++){
if(black[i]<=hthro && blackend==0){
blackend=1;
if(black[i]<=mx){ //更新黑色最少的的线标
mx=black[i];
mi=i;
}
}
else if((blackend==1 && black[i]>hthro) || i==height-1){
blackend=0;
gridx[gx]=mi;
//printf("<行标:%d>",gridx[gx]);
gx++;
mx=500;
mi=i;
}
}
/*纵向的表格*/
//计算每一列的黑像素个数
int tempBlackPixely=0;
memset(black,0,1000); //初始化内存,这里用做清零
for(i=0;i<width;i++) {
for(j=0;j<height;j++){
if(data[j*step+i*channels]==0) //计算黑色的像素数
tempBlackPixely+=1;
}
black[i]=tempBlackPixely; //black记录黑色像素数
tempBlackPixely=0;
}
/*计算纵线位置*/
for(i=0;i<width;i++){
if(black[i]<=wthro){
if(blackend==0){
blackend=1;
}
if(black[i]<=mx){ //更新黑色最少的的线标
mx=black[i];
mi=i;
}
}
else if((blackend==1 && black[i]>wthro)){
blackend=0;
if(gy==0){
gridy[gy]=mi; //记下黑色最少的的线位置
gy++;
}
else if(mi-gridy[gy-1]<=25){ //考虑方格太小的情况,将其分入上一个方格中
gridy[gy-1]=mi; //
}
else{
gridy[gy]=mi; //记下黑色最少的的线位置
//printf("<列标:%d>",gridy[gy]);
gy++;
}
mx=500;
mi=i;
}
}
gridy[gy]=mi; //对最后一列进行处理
gy++;
//for(j=0;j<gy;j++)
// printf("The %dth row is %d \n",j,gridy[j]);
//for(i=0;i<gx;i++)
// printf("The %dth line is %d \n",i,gridx[i]);
/*笔迹划分图上画上方格*/
for(i=0;i<height;i++)
for(j=0;j<gy;j++)
data[i*step+gridy[j]*channels]=0;
for(i=0;i<width;i++)
for(j=0;j<gx;j++)
data[gridx[j]*step+i*channels]=0;
*gxx=gx;
*gyy=gy;
//printf("分割完成\n");
return imgbjhf;
}

View File

@@ -0,0 +1,81 @@
/* 程序名wordrecorgnize.c
功能:输入文字图像及方格位置。识别含有有效字符的方格
*/
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include "Point.h"
#include "Cword.h"
IplImage* wordrecognize(IplImage* imgbj,int *gridx,int *gridy,Cword *wordbox,int gx,int gy){
/*定义变量*/
int i,j,ni,numw,nblack=0,wnum=0;
//Cword wordbox[150];
int sumnum=(gx-1)*(gy-1);
int height,width,step,channels;
uchar *data;
/*定义新的图像*/
IplImage* imgwzbj = cvCreateImage(cvGetSize(imgbj),imgbj->depth,imgbj->nChannels);
cvCopy(imgbj,imgwzbj,NULL);
uchar *wzbjdata = (uchar *)imgwzbj->imageData;
/* 获取图像信息*/
height = imgbj->height;
width = imgbj->width;
step = imgbj->widthStep;
channels = imgbj->nChannels;
data = (uchar *)imgbj->imageData;
/*开始处理*/
for(i=0;i<gx-1;i++)
for(j=0;j<gy-1;j++){
numw=i*(gy-1)+j+1;
wordbox[numw].wbegin.x=gridx[i];
wordbox[numw].wbegin.y=gridy[j];
wordbox[numw].wend.x=gridx[i+1];
wordbox[numw].wend.y=gridy[j+1];
//printf("The %dth word*** \n",numw);
}
//printf("The %dth word \n",numw);
//printf("The sum of words: %d \n",sumnum);
for(ni=1;ni<=sumnum;ni++){
for(i=wordbox[ni].wbegin.x;i<wordbox[ni].wend.x;i++)
for(j=wordbox[ni].wbegin.y;j<wordbox[ni].wend.y;j++){
if(data[i*step+j*channels]==0) //计算黑色的像素数
nblack+=1;
}
if(nblack>80){
wordbox[ni].isword=true;
wnum++;
wordbox[ni].nn=wnum;
//printf("x= %d;;;y=%d \n",wordbox[ni].wbegin.x,wordbox[ni].wbegin.y);
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+2)*channels]=0; //标在一幅图上,是文字,标一个+
wzbjdata[(wordbox[ni].wbegin.x+1)*step+(wordbox[ni].wbegin.y+2)*channels]=0;
wzbjdata[(wordbox[ni].wbegin.x+3)*step+(wordbox[ni].wbegin.y+2)*channels]=0;
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+1)*channels]=0;
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+3)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x)*step+(wordbox[ni].wbegin.y+2)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x+4)*step+(wordbox[ni].wbegin.y+2)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+4)*channels]=0;
}
else{
wordbox[ni].isword=false;
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+2)*channels]=0; //标在一幅图上,不是文字,标一个-
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+1)*channels]=0;
wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+3)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y)*channels]=0;
//wzbjdata[(wordbox[ni].wbegin.x+2)*step+(wordbox[ni].wbegin.y+4)*channels]=0;
}
wordbox[ni].blacknum=nblack;
nblack=0;
}
return imgwzbj;
}