#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
//宏定义字数,最多150
#define WORDSNUM 105
//宏定义行数,最多7
#define ROWSNUM 3
//int END=0;
int rows=0;
//为了方便,存字的文件名列表定义为全局变量
const char *rowfilenamelist[7]=
{"row0.jpg","row1.jpg","row2.jpg","row3.jpg","row4.jpg","row5.jpg","row6.jpg"};
const char *wordfilenamelist[180]=
{"col0.jpg","col1.jpg","col2.jpg","col3.jpg","col4.jpg","col5.jpg","col6.jpg",
"col7.jpg","col8.jpg","col9.jpg","col10.jpg","col11.jpg","col12.jpg","col13.jpg",
"col14.jpg","col15.jpg","col16.jpg","col17.jpg","col18.jpg","col19.jpg","col20.jpg",
"col21.jpg","col22.jpg","col23.jpg","col24.jpg","col25.jpg","col26.jpg","col27.jpg",
"col28.jpg","col29.jpg","col30.jpg","col31.jpg","col32.jpg","col33.jpg","col34.jpg",
"col35.jpg","col36.jpg","col37.jpg","col38.jpg","col39.jpg","col40.jpg","col41.jpg",
"col42.jpg","col43.jpg","col44.jpg","col45.jpg","col46.jpg","col47.jpg","col48.jpg",
"col49.jpg","col50.jpg","col51.jpg","col52.jpg","col53.jpg","col54.jpg","col55.jpg",
"col56.jpg","col57.jpg","col58.jpg","col59.jpg","col60.jpg","col61.jpg","col62.jpg",
"col63.jpg","col64.jpg","col65.jpg","col66.jpg","col67.jpg","col68.jpg","col69.jpg",
"col70.jpg","col71.jpg","col72.jpg","col73.jpg","col74.jpg","col75.jpg","col76.jpg",
"col77.jpg","col78.jpg","col79.jpg","col80.jpg","col81.jpg","col82.jpg","col83.jpg",
"col84.jpg","col85.jpg","col86.jpg","col87.jpg","col88.jpg","col89.jpg","col90.jpg",
"col91.jpg","col92.jpg","col93.jpg","col94.jpg","col95.jpg","col96.jpg","col97.jpg",
"col98.jpg","col99.jpg","col100.jpg","col101.jpg","col102.jpg","col103.jpg","col104.jpg",
"col105.jpg","col106.jpg","col107.jpg","col108.jpg","col109.jpg","col110.jpg","col111.jpg",
"col112.jpg","col113.jpg","col114.jpg","col115.jpg","col116.jpg","col117.jpg","col118.jpg",
"col119.jpg","col120.jpg","col121.jpg","col122.jpg","col123.jpg","col124.jpg","col125.jpg",
"col126.jpg","col127.jpg","col128.jpg","col129.jpg","col130.jpg","col131.jpg","col132.jpg",
"col133.jpg","col134.jpg","col135.jpg","col136.jpg","col137.jpg","col138.jpg","col139.jpg",
"col140.jpg","col141.jpg","col142.jpg","col143.jpg","col144.jpg","col145.jpg","col146.jpg",
"col147.jpg","col148.jpg","col149.jpg"};//文件名列表
//将图像img中的矩形Rect部分存到文件filename中去
void savetofile(const char* filename,CvRect Rect,IplImage* img);
//找一行文字切分上顶点像素行
int findN1(IplImage *image);
//找一行文字切分时行底像素行
int findN2(IplImage *image,int I);
//找一列文字切分时左边像素列
int findColleft(IplImage *image);
//找一列文字切分时右边像素列
int findColright(IplImage *image,int I);
//生成矩形
CvRect makeRect(CvRect rect,int x,int y,int w,int h);
//行切割时,将图像img中i,j像素行之间的文字存到文件filename中,其余的部分存放到tempfile文件中
void cutRowandSave(int i,int j,IplImage* img,const char* filename,const char* tempfile);
////////找给定图片的字的平均宽宽
int findEvenwidth(IplImage* img);
////清理白条
void Clear(const char* wordfilenamelist,double min);
///得到字图片最大高度和宽度
int getMaxwidth();
int getMaxheight();
///重设字图片大小为统一
void resize(int maxWidth,int maxHeight);
///行切分
void rowDivide(IplImage *img);
//列切分
void colDivide();
int findN1(IplImage *image)//找第一行文字上边沿行
{
int i,j;
int height = image->height;
int width = image->width;
int step = image->widthStep;
int channels = image->nChannels;
unsigned char* data = (uchar *)image->imageData;//取图像数据,数据经过二值化后是0-255之间
// printf("Processing a %dx%d image with %d channels\n",height,width,channels); //输出图像信息
int count=0;//用来记录一行中黑点数
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
if(data[i*step+j*channels+1]==0)//如果是黑点
{
count=count+1;//黑点计数加1
}
}
if(count>=5)//如果一行中超过五个黑点,则说明是一行文字的顶部
{
// printf("%d,%d\n",i,j);
return i;
}
count=0;//换一行扫瞄时计数器清零
}
return 0;//否则返回0,说明图中没有文字
}
int findN2(IplImage *image,int I)//找一行文字切分的底部像素行
{
int i,j;
int height = image->height;
int width = image->width;
int step = image->widthStep;
int channels = image->nChannels;
unsigned char* data = (uchar *)image->imageData;
int count=0;
for(i=I;i<height;i++)
{
for(j=0;j<width;j++)
{
if(data[i*step+j*channels+1]==0)//如果是黑点
{
count=count+1;//黑点计数加1
}
}
if(count<5)//如果某一行黑点小于5个则说明这一行文件到底了,可以根据需要改动这个参数。
{
return i;
}
count=0;//否则下一行开始扫瞄时,计数器需要清零
}
return 0;
}
int findColleft(IplImage *image)//找一列字左部开始处
{
int i,j;
int height = image->height;
int width = image->width;
int step = image->widthStep;
int channels = image->nChannels;
unsigned char* data = (uchar *)image->imageData;
// printf("Processing a %dx%d image with %d channels\n",height,width,channels); //输出图像信息
int count=0;
int pre=0;//用来记录是否连续几列像素都是黑点较多,用以确定是否真的开始了一列
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
if(data[j*step+i*channels+1]<=10)//由于有躁声,小于11的值都认为是黑点
{
count=count+1;//如果是黑点则计数器加1
}
}
if(count>2)//如果一列多于两个黑点
{
pre=pre+1;//记录下来
}
if(pre>2)
{
// printf("\n找到了right:%d\n",i);//如果连续三列多于两个黑点则返回
return i-2;
}
count=0;//换列时候计数器清零
}
return 0;
}
//////////////////
int findColright(IplImage *image,int I)//找一列字的结束处
{
int i=I;
int j;
int height = image->height;
int width = image->width;
int step = image->widthStep;
int channels = image->nChannels;
unsigned char* data = (uchar *)image->imageData;
int count=0;
int pre=0;//用来记录是否连续几列都是空白,可有效区分部首偏旁
for(i=I+1;i<width;i++)
{
for(j=0;j<height;j++)
{
if(data[j*step+i*channels+1]<=10)
{
count=count+1;
// printf("Count=:%d ",count);
}
// printf("%d,",data[j*step+i*channels+1]);
}
if(count<1)//某一列黑点个数极少
{
pre++;
}
if(pre>1)//连续多列黑点个数极少则返回
{
return i-1;
}
count=0;
}
// printf("已经没有汉字了\n");
// END=1;
return -1;
}
///为矩形框赋值,此函数用处不大
CvRect makeRect(CvRect rect,int x,int y,int w,int h)
{
rect.x=x;
rect.y=y;
rect.width=w;
rect.height=h;
return rect;
}
/////将i,j像素行之间的图像剪下并存入filename中,剩下部分存入tempfile中
//可以再扩展一个将i,j像素列之间图像剪下并存入filename中的函数
void cutRowandSave(int i,int j,IplImage* img,const char* filename,const char* tempfile)
{
IplImage* localImg=img;
IplImage* pImgRect=0;
IplImage* temp=0;
CvRect Rect;
Rect.x=0;Rect.y=0;Rect.width=0;Rect.height=0;//初始化
Rect=makeRect(Rect,0,i,localImg->width,j-i);//为矩形框赋值
savetofile(filename,Rect,img);//存入文件中
Rect.x=0;
Rect.y=j;
Rect.height=img->height-j;
savetofile(tempfile,Rect,img);//存剩余部分入文件中
}
//将图像img中的矩形Rect部分存入文件filename中
void savetofile(const char* filename,CvRect Rect,IplImage* img)
{
IplImage* localImg=img;
IplImage* pImgRect=0;
pImgRect = cvCreateImage(cvSize(Rect.width, Rect.height), IPL_DEPTH_8U,1);
cvSetImageROI(localImg,Rect);
cvCopy(localImg,pImgRect);
cvSaveImage(filename,pImgRect);
}//
////用一行字的图片img来估计字的平均宽度
int findEvenwidth(IplImage* img)
{
IplImage* localImg=img;
printf("开始估计字的平均宽度\n");
int I=findColleft(img);
int J=findColright(img,I);
CvRect Rect;
Rect.x=0;Rect.y=0;Rect.width=0;Rect.height=0;//初始化
Rect=makeRect(Rect,J,0,img->width-J,img->height);
savetofile("temp.jpg",Rect,img);
for(int k=1;k<50;k++)
{
img=cvLoadImage("temp.jpg",0);
I=findColleft(img);
J=findColright(img,I);
if(I==0&&J==1)
{
printf("得到平均字宽的估计值为:%d\n",localImg->width/k);
return localImg->width/k;
}
Rect=makeRect(Rect,J,0,img->width-J,im
- 1
- 2
- 3
- 4
前往页