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);
if(g==0)
{
*(temp+dwLineBytes*i+j)=0;
break;
}
}
}
}
}
}
else if(wBitCount==24)
{
//如果不是二值图像,需要先转换为二值图像
for(i=0;i<dwHeight;i++)
{
for(j=0,n=0;j<dwWidth*3;j++,n++)
{
b=*(image+dwLineBytes*i+j);
j++;
g=*(image+dwLineBytes*i+j);
j++;
r=*(image+dwLineBytes*i+j);
avg=(b+g+r)/3.0;
if(avg>=120)
{
avg=255;
}
else
{
avg=0;
}
*(image+dwLineBytes*i+n)=(BYTE)avg;
n++;
*(image+dwLineBytes*i+n)=(BYTE)avg;
n++;
*(image+dwLineBytes*i+n)=(BYTE)avg;
}
}
if(type==0)
{
//水平方向
for(i=0;i<dwHeight;i++)
{
for(j=(num-1)/2;j<(dwWidth-(num-1)/2)*3;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
b=*(image+dwLineBytes*i+j+k*3);
g=*(image+dwLineBytes*i+j+1+k*3);
r=*(image+dwLineBytes*i+j+2+k*3);
if(b==0 && g==0 && r==0)
{
*(temp+dwLineBytes*i+j)=0;
j++;
*(temp+dwLineBytes*i+j)=0;
j++;
*(temp+dwLineBytes*i+j)=0;
break;
}
}
}
}
}
else
{
//垂直方向
for(i=(num-1)/2;i<dwHeight-(num-1)/2;i++)
{
for(j=0;j<dwWidth*3;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
b=*(image+dwLineBytes*(i+k)+j);
g=*(image+dwLineBytes*(i+k)+j+1);
r=*(image+dwLineBytes*(i+k)+j+2);
if(b==0 && g==0 && r==0)
{
*(temp+dwLineBytes*i+j)=0;
j++;
*(temp+dwLineBytes*i+j)=0;
j++;
*(temp+dwLineBytes*i+j)=0;
break;
}
}
}
}
}
}
else
{
free(temp);
temp=NULL;
return -1;
}
memcpy(image,temp,dwLineBytes*dwHeight*sizeof(BYTE));
free(temp);
temp=NULL;
return 0;
}
//图像腐蚀
int Erosion(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,0,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==255)
{
*(temp+dwLineBytes*i+j)=255;
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);
if(g==255)
{
*(temp+dwLineBytes*i+j)=255;
break;
}
}
}
}
}
}
else if(wBitCount==24)
{
//如果不是二值图像,需要先转换为二值图像
for(i=0;i<dwHeight;i++)
{
for(j=0,n=0;j<dwWidth*3;j++,n++)
{
b=*(image+dwLineBytes*i+j);
j++;
g=*(image+dwLineBytes*i+j);
j++;
r=*(image+dwLineBytes*i+j);
avg=(b+g+r)/3.0;
if(avg>=120)
{
avg=255;
}
else
{
avg=0;
}
*(image+dwLineBytes*i+n)=(BYTE)avg;
n++;
*(image+dwLineBytes*i+n)=(BYTE)avg;
n++;
*(image+dwLineBytes*i+n)=(BYTE)avg;
}
}
if(type==0)
{
//水平方向
for(i=0;i<dwHeight;i++)
{
for(j=(num-1)/2;j<(dwWidth-(num-1)/2)*3;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
b=*(image+dwLineBytes*i+j+k*3);
g=*(image+dwLineBytes*i+j+1+k*3);
r=*(image+dwLineBytes*i+j+2+k*3);
if(b==255 && g==255 && r==255)
{
*(temp+dwLineBytes*i+j)=255;
j++;
*(temp+dwLineBytes*i+j)=255;
j++;
*(temp+dwLineBytes*i+j)=255;
break;
}
}
}
}
}
else
{
//垂直方向
for(i=(num-1)/2;i<dwHeight-(num-1)/2;i++)
{
for(j=0;j<dwWidth*3;j++)
{
for(k=-(num-1)/2;k<=(num-1)/2;k++)
{
b=*(image+dwLineBytes*(i+k)+j);
g=*(image+dwLineBytes*(i+k)+j+1);
r=*(image+dwLineBytes*(i+k)+j+2);
if(b==255 && g==255 && r==255)
{
*(temp+dwLineBytes*i+j)=255;
j++;
*(temp+dwLineBytes*i+j)=255;
j++;
*(temp+dwLineBytes*i+j)=255;
break;
}
}
}
}
}
}
else
{
free(temp);
temp=NULL;
return -1;
}
memcpy(image,temp,dwLineBytes*dwHeight*sizeof(BYTE));
free(temp);
temp=NULL;
return 0;
}
//图像开闭运算
int OpenClose(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCout,
int type,int direction,int num)
{
if(num!=3 && num!=5 && num!=7)
{
return -1;
}
if(type==0)
{//开运算
Erosion(image,dwWidth,dwHeight,wBitCout,direction,num);
Dilation(image,dwWidth,dwHeight,wBitCout,direction,num);
return 0;
}
else
{//闭运算
Dilation(image,dwWidth,dwHeight,wBitCout,direction,num);
Erosion(image,dwWidth,dwHeight,wBitCout,direction,num);
return 0;
}
}
//图像窗口变换
int WindowTranslation(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,
int down,int up)
{
DWORD height=dwHeight;
DWORD width=dwWidth;
WORD bitCount=wBitCount;
DWORD lineBytes=(width*bitCount+31)/32*4;
BYTE *temp=image;
DWORD i=0;
DWORD j=0;
DWORD n=0;
BYTE g=0;
BYTE b=0;
BYTE r=0;
double avg=0;
if(bitCount==8)
{
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
g=*(temp+lineBytes*(height-1-i)+j);
if(g<down)
{
*(temp+lineBytes*(height-1-i)+j)=0;
}
if(g>up)
{
*(temp+lineBytes*(height-1-i)+j)=255;
}
}
}
}
else if(bitCount==24)
{
for(i=0;i<height;i++)
{
for(j=0,n=0;j<width*3;j++,n++)
{
b=*(temp+lineBytes*i+j);
j++;
g=*(temp+lineBytes*i+j);
j++;
r=*(temp+lineBytes*i+j);
avg=(b+g+r)/3.0;
if(avg<down) avg=0;
if(avg>up) avg=255;
*(temp+lineBytes*i+n)=(BYTE)avg;
n++;
*(temp+lineBytes*i+n)=(BYTE)avg;
n++;
*(temp+lineBytes*i+n)=(BYTE)avg;
}
}
}
else
{
return -1;
}
return 0;
}
//图像对数变换
int LogTranslation(BYTE *image,DWORD dwWidth,DWORD dwHeight,WORD wBitCount,
int k)
{
DWORD height=dwHeight;
DWORD width=dwWidth;
WORD bitCount=wBitCount;
DWORD lineBytes=(width*bitCount+31)/32*4;
BYTE *temp=image;
DWORD i=0;
DWORD j=0;
DWORD n=0;
BYTE b=0;
BYTE g=0;
BYTE r=0;
if(temp==NULL)
{
return -1;
}
if(bitCount==8)
{
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
g=*(temp+lineBytes*i+j);
*(temp+lineBytes*i+j)=(BYTE)(k*log(g+1));
}
}
}
else if(bitCount==24)
{
for(i=0;i<height;i++)
{
for(j=0,n=0;j<width*3;j++,n++)
{
b=*(temp+lineBytes*i+j);
j++;
g=*(temp+lineBytes*i+j);
j++;
r=*(temp+lineBytes*i+j);
*(temp+lineBytes*i+n)=(BYTE)(k*log(b+1));
n++;
*(temp+lineBytes*i+n)=(BYTE)(k*log(g+1));
n++;
*(temp+lineBytes*i+n)=(BYTE)(k*log(r+1));
}
}
}
else
{
return -1;
}
return 0;
}