#include <alloc.h>
#include <stdio.h>
#include <graphics.h>
#include <math.h>
int maxopenleft=30;
int eyeclosenum=0;
void save(unsigned char image_in[100][100])
{FILE *fp; char p;
int m,n;
if((fp=fopen("c:\\save.txt","w"))==NULL)
{printf("can not open file\n");
return;
}
p='\n';
for(m=0;m<100;m++)
for(n=0;n<100;n++)
{
if(fprintf(fp,"%d,",image_in[m][n])==NULL) printf("file write error\n");
if(n==99) fprintf(fp,"%c",p);
}
}
void setbmp(FILE *fp,unsigned char image_in[100][100]) /*设置图像数据*/
{
int a,m,n,num;
unsigned char image[30000],k1,k2,k3,b;
for(m=0;m<30000;m++)
{ fseek(fp,54+m,0);
fread(&image[m],1,1,fp);
}
for(m=0;m<100;m++)
for(n=0;n<100;n++)
{ a=m*300+n*3;
k1=image[a];
a++;
k2=image[a];
a++;
k3=image[a];
image_in[m][n]=(unsigned char)(k3*0.299+k2*0.587 +k1*0.114);
}
for(m=0;m<50;m++)
for(n=0;n<100;n++)
{ b=image_in[99-m][n];
image_in[99-m][n]=image_in[m][n];
image_in[m][n]=b;
}
}
void sub(unsigned char image1_in[100][100],unsigned char image2_in[100][100],unsigned char image_in[100][100])
{
int i,j;
int a;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
{a=(int)image2_in[i][j]-(int)image1_in[i][j];
if(a<0) image_in[i][j]=0;
else image_in[i][j]=(unsigned char)a;
}
}
void smooth(unsigned char image_in[100][100]) /*高斯平滑*/
{
int a,b,buf,c;
unsigned char image_out[100][100];
for(a=1;a<99;a++)
for(b=1;b<99;b++)
{
buf=(int)image_in[a-1][b-1]
+2*(int)image_in[a-1][b]
+(int)image_in[a-1][b+1]
+2*(int)image_in[a][b-1]
+4*(int)image_in[a][b]
+2*(int)image_in[a][b+1]
+(int)image_in[a+1][b-1]
+2*(int)image_in[a+1][b]
+(int)image_in[a+1][b+1];
image_out[a][b]=(unsigned char)(buf/16);
}
for(a=1;a<99;a++)
for(b=1;b<99;b++)
image_in[a][b]=image_out[a][b];
}
void highlow(unsigned char image_in[100][100]) /*二值化*/
{
int i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
if(image_in[i][j]<=50)
image_in[i][j]=0;
else
image_in[i][j]=1;
}
void arrayline(unsigned char image_in[100][100])/*行列算法*/
{
int i,j,k=0,k1,k2;
int p=0,p1,p2;
/*对列进行处理�是阈值,超过4的像素归为0*/
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
if(image_in[i][j]==1)
k=k+1;
else if(image_in[i][j]==0)
{
if(k<=4)
k=0;
else if(k>4)
{
k2=i-1;
for(k1=0;k1<k;k1++)
{
image_in[k2][j]=0;
k2=k2-1;
}
k=0;
}
}
}
}
/*对行进行处理,6是阈值,超过6的像素归为0*/
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
if(image_in[i][j]==1)
p=p+1;
else if(image_in[i][j]==0)
{
if(p<=6)
p=0;
else if(p>6)
{
p2=j-1;
for(p1=0;p1<p;p1++)
{
image_in[i][p2]=0;
p2=p2-1;
}
p=0;
}
}
}
}
}
void regionfind(unsigned char image_in[100][100],int *p1)
{ int r[100][100];
int i,j,m,n;
int counter[100];
int maxlabel=0;
int tempvalue=0;
int maxtop=0;
int maxbottom=0;
int prepoint=0;
int latterpoint=0;
int maxopensize=0;
int k=1;
for(i=0;i<100;i++) /*初始化r数组*/
for(j=0;j<100;j++)
r[i][j]=0;
for(i=1;i<100;i++)
for(j=1;j<100;j++)
if(image_in[i][j]==1)
{
if(image_in[i-1][j]==0&&image_in[i][j-1]==0)
{
r[i][j]=k;
k++;
}
else if(image_in[i-1][j]==1&&image_in[i][j-1]==0)
r[i][j]=r[i-1][j];
else if(image_in[i-1][j]==0&&image_in[i][j-1]==1)
r[i][j]=r[i][j-1];
else if(image_in[i-1][j]==1&&image_in[i][j-1]==1)
{
r[i][j]=r[i-1][j];
if(r[i-1][j]!=r[i][j-1])
{prepoint=r[i-1][j];
latterpoint=r[i][j-1];
for(m=1;m<=i;m++)
{for(n=1;n<100;n++)
{if(m==i&&n==j) break;
else if(r[m][n]==latterpoint)
r[m][n]=prepoint;
}
if(m==i&&n==j) break;
}
k--;
}
}
}
else r[i][j]=0;
maxlabel=1;
for(i=1;i<100;i++)
counter[i]=0;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
counter[r[i][j]]++;
tempvalue=counter[1];
for(i=2;i<=k;i++)
if(counter[i]>tempvalue)
{
maxlabel=i;
tempvalue=counter[i];
}
for(i=1;i<100;i++)
{for(j=1;j<100;j++)
if(r[i][j]==maxlabel)
{maxtop=i;
break;
}
if(r[i][j]==maxlabel) break;
}
for(i=99;i>=1;i--)
{ for(j=1;j<100;j++)
if(r[i][j]==maxlabel)
{
maxbottom=i;
break;
}
if(r[i][j]==maxlabel) break;
}
maxopensize=maxbottom-maxtop;
printf("%d,%d\n",maxtop,maxbottom);
*p1=maxopensize;
}
void geteyeclosenum(int x)
{ float i;
i=(float)(x)/(float)(maxopenleft);
if(i<0.1) eyeclosenum++;
}
void getmaxopen(int openleft)
{
unsigned char image3[100][100],image4[100][100],imagesub2[100][100];
unsigned char image5[100][100],image6[100][100],imagesub3[100][100];
int x2=0,x3=0;
int *p3,*p4;
FILE *fp3,*fp4,*fp5,*fp6;
fp3=fopen("c:\\3.bmp","rb");
fp4=fopen("c:\\4.bmp","rb");
fp5=fopen("c:\\5.bmp","rb");
fp6=fopen("c:\\6.bmp","rb");
if(fp3&&fp4&&fp5&&fp6)
{
setbmp(fp3,image3);
setbmp(fp4,image4);
sub(image3,image4,imagesub2);
smooth(imagesub2);
highlow(imagesub2);
p3=&x2;
regionfind(imagesub2,p3);
x2=*p3;
setbmp(fp5,image5);
setbmp(fp6,image6);
sub(image5,image6,imagesub3);
smooth(imagesub3);
highlow(imagesub3);
p4=&x3;
regionfind(imagesub3,p4);
x3=*p4;
if(openleft>maxopenleft) maxopenleft=openleft;
if(x2>maxopenleft) maxopenleft=x2;
if(x3>maxopenleft) maxopenleft=x3;
geteyeclosenum(x2);
geteyeclosenum(x3);
}
fclose(fp3);
fclose(fp4);
fclose(fp5);
fclose(fp6);
}
void recognizeimage(int x)
{
int j;
float i;
j=x;
geteyeclosenum(j);
i=(float)eyeclosenum/(float)3;
if(i<0.075) printf("status is 1");
else printf("status is 0");
}
void main()
{
unsigned char image1[100][100],image2[100][100],imagesub[100][100]; /*存放图像数据*/
int openleft,m,n,i;
int *p1,*p2;
FILE *fp1,*fp2;
if((fp1=fopen("c:\\3.bmp","rb"))==NULL) /*打开位图1*/
{
printf("cannot open 1.bmp\n");
exit(1);
}
setbmp(fp1,image1);
if((fp2=fopen("c:\\4.bmp","rb"))==NULL) /*打开位图2*/
{
printf("cannot open 2.bmp\n");
exit(1);
}
setbmp(fp2,image2);
sub(image1,image2,imagesub); save(imagesub);
smooth(imagesub);
highlow(imagesub);save(imagesub);
arrayline(imagesub);
save(imagesub);
p1=&openleft;
regionfind(imagesub,p1);/*区域划分,获得开度*/
openleft=*p1;
i=openle