Alogrithm.h的全部程序如下:
#ifndef ALGORITHM_H_INCLUDE
#define ALGORITHM_H_INCLUDE
#include <math.h>
#include "Bitmap.h"
//图像细化
int Thining(BYTE* image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount)
{
static int erasetable[256]={
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
1,1,0,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,
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,0,1,1,0,0,1,1,1,0,1,1,0,0,1,0,0,0
};
DWORD dwLineBytes=(dwWidth*wBitCount+31)/32*4;
BYTE *temp=NULL;
DWORD i=0;
DWORD j=0;
BYTE g=0;
BYTE zs=0,s=0,ys=0,z=0,y=0,zx=0,x=0,yx=0;
int num=0;
temp=(BYTE*)malloc(dwHeight*dwLineBytes*sizeof(BYTE));
if(!temp)
{
return -1;
}
if(wBitCount!=8)
{
free(temp);
temp=NULL;
return -1;
}
for(i=0;i<dwHeight;i++)
{
for(j=0;j<dwWidth;j++)
{
g=*(image+dwLineBytes*i+j);
if(g<=120)
{
*(image+dwLineBytes*i+j)=(BYTE)0;
}
else
{
*(image+dwLineBytes*i+j)=(BYTE)255;
}
}
}
memcpy(temp,image,dwLineBytes*dwHeight*sizeof(BYTE));
for(i=1;i<dwHeight-1;i++)
{
for(j=1;j<dwWidth-1;j++)
{
g=*(image+dwLineBytes*i+j);
if(g==0)
{
z=*(image+dwLineBytes*i+j-1);
y=*(image+dwLineBytes*i+j+1);
if(z==255 || y==255)
{
zs=*(image+dwLineBytes*(i+1)+j-1);
s=*(image+dwLineBytes*(i+1)+j);
ys=*(image+dwLineBytes*(i+1)+j+1);
zx=*(image+dwLineBytes*(i-1)+j-1);
x=*(image+dwLineBytes*(i-1)+j);
yx=*(image+dwLineBytes*(i-1)+j+1);
num=zs/255+s/255*2+ys/255*4+z/255*8+y/255*16+zx/255*32+x/255*64+yx/255*128;
if(erasetable[num]==1)
{
*(temp+dwLineBytes*i+j)=(BYTE)255;
*(image+dwLineBytes*i+j)=(BYTE)255;
}
}
}
}
}
for(i=1;i<dwHeight-1;i++)
{
for(j=1;j<dwWidth-1;j++)
{
g=*(image+dwLineBytes*i+j);
if(g==0)
{
s=*(image+dwLineBytes*(i+1)+j);
x=*(image+dwLineBytes*(i-1)+j);
if(s==255 || x==255)
{
zs=*(image+dwLineBytes*(i+1)+j-1);
z=*(image+dwLineBytes*i+j-1);
y=*(image+dwLineBytes*i+j+1);
ys=*(image+dwLineBytes*(i+1)+j+1);
zx=*(image+dwLineBytes*(i-1)+j-1);
yx=*(image+dwLineBytes*(i-1)+j+1);
num=zs/255+s/255*2+ys/255*4+z/255*8+y/255*16+zx/255*32+x/255*64+yx/255*128;
if(erasetable[num]==1)
{
*(temp+dwLineBytes*i+j)=(BYTE)255;
*(image+dwLineBytes*i+j)=(BYTE)255;
}
}
}
}
}
memcpy(image,temp,dwHeight*dwLineBytes*sizeof(BYTE));
free(temp);
temp=NULL;
return 0;
}
//图像膨胀
int Dilation(BYTE* image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,
int type,int num)
{
DWORD dwLineBytes=(dwWidth*wBitCount+31)/32*4;
DWORD i=0;
DWORD j=0;
DWORD n=0;
BYTE b=0;
BYTE g=0;
BYTE r=0;
double avg=0;
BYTE *temp=NULL;
int k=0;
if(image==NULL)
{
return -1;
}
if(num!=3 && num!=5 &&num!=7)
{
return -1;
}
temp=(BYTE*)malloc(dwHeight*dwLineBytes*sizeof(BYTE));
if(!temp)
{
return -1;
}
memset(temp,255,dwLineBytes*dwHeight*sizeof(BYTE));
if(wBitCount==8)
{
//如果为非二值图像,则转化为二值图像
for(i=0;i<dwHeight;i++)
{
for(j=0;j<dwWidth;j++)
{
g=*(image+dwLineBytes*i+j);
if(g>=120)
{
*(image+dwLineBytes*i+j)=255;
}
else
{
*(image+dwLineBytes*i+j)=0;
}
}
}
if(type==0)
{
//水平方向
for(i=0;i<dwHeight;i++)
{
for(j=(num-1)/2;j<dwWidth-(num-1)/2;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
g=*(image+dwLineBytes*i+j+k);
if(g==0)
{
*(temp+dwLineBytes*i+j)=0;
break;
}
}
}
}
}
else
{
//垂直方向
for(i=(num-1)/2;i<dwHeight-(num-1)/2;i++)
{
for(j=0;j<dwWidth;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
g=*(image+dwLineBytes*(i+k)+j);