#include "iostream.h"
#include "math.h"
#include "memory.h"
#include "windows.h"
#include "winnt.h"
#include "string.h"
#include "resource.h"
//using namespace std;
class myImage
{
public:
int Longsize;
int Heightsize;
int Centerx[10];
int Centery[10];
int flag[10];
int *Image;
int Centersize;
public:
myImage(int Long,int Height,int source[]);
void SearchCenter(unsigned char *Image0);
unsigned char * SearchEdge();
//void SearchCenter(double Image2[][]);
~myImage();
};
//象素矩阵初始化
myImage::myImage(int Long,int Height,int source[])
{
int i,j;
Image=new int [Long*Height];
for(i=0;i<Height;i++)
for(j=0;j<Long;j++)
Image[i*Long+j]=source[i*Long+j];
Longsize=Long;
Heightsize=Height;
}
//寻找特征图形的中心
void myImage::SearchCenter(unsigned char *Image0)
{
static int k=0;
int i;
int j,m=0;
int a,b;
int rr,r;
int aa[4]={0},bb[4]={0},r1[4]={0};
//int Max=0;
rr=(int)sqrt(pow(Heightsize,2)+pow(Longsize,2));
unsigned char *pArry;
pArry=new unsigned char [rr*Heightsize*Longsize];
memset(pArry,0,rr*Heightsize*Longsize);
for(i=0;i<Heightsize;i++)
{
for(j=0;j<Longsize;j++)
if(Image0[i*Longsize+j]==0)
{
for(b=0;b<Heightsize;b++)
for(a=0;a<Longsize;a++)
{
r=(int)sqrt(pow(i-a,2)+pow(j-b,2));
pArry[r*Longsize*Heightsize+b*Longsize+a]++;
}
}
}
for(r=0;r<rr;r++)
{
for(b=0;b<Heightsize;b++)
{
for(a=0;a<Longsize;a++)
{
if(pArry[r*Heightsize*Longsize+b*Longsize+a]>4)
{
Centerx[k]=a;
Centery[k]=b;
flag[k]=0;
k++;//0表示园,1表示矩形,2表示三角形
}
else if(pArry[r*Heightsize*Longsize+b*Longsize+a]==4)
{
for(i=0;i<Heightsize;i++)
{
for(j=0;j<Longsize;j++)
if(Image0[i*Longsize+j]==0)
if((int)sqrt(pow(i-a,2)+pow(j-b,2))==r)
{
aa[m]=i,bb[m]=j;
m++;
}
}
for(m=0;m<4;m++)
{
r1[m]=(int)sqrt(pow(aa[m+1]-aa[m],2)+pow(bb[m+1]-bb[m],2));
if(m=3)
r1[m]=(int)sqrt(pow(aa[0]-aa[m],2)+pow(bb[0]-bb[m],2));
}
if((r1[0]==r1[1])&&(r1[1]==r1[2])&&(r1[2]==r1[3]))
{
Centerx[k]=a;
Centery[k]=b;
flag[k]=1;
k++;
}
}
else if(pArry[r*Heightsize*Longsize+b*Longsize+a]==3)
{
for(i=0;i<Heightsize;i++)
{
for(j=0;j<Longsize;j++)
if(Image0[i*Longsize+j]==0)
if((int)sqrt(pow(i-a,2)+pow(j-b,2))==r)
{
aa[m]=i,bb[m]=j;
m++;
}
}
for(m=0;m<3;m++)
{
r1[m]=(int)sqrt(pow(aa[m+1]-aa[m],2)+pow(bb[m+1]-bb[m],2));
if(m=2)
r1[m]=(int)sqrt(pow(aa[0]-aa[m],2)+pow(bb[0]-bb[m],2));
}
if((r1[0]==r1[1])&&(r1[1]==r1[2]))
{
Centerx[k]=a;
Centery[k]=b;
flag[k]=2;
k++;
}
}
}
}
}
Centersize=k;
return;
}
//寻找边界
unsigned char * myImage::SearchEdge()
{
int i,j;
int fx,fy,G;
unsigned char *Image1;
Image1=new unsigned char [Heightsize*Longsize];//分配新内存存放边界图像
memset(Image1,255,Heightsize*Longsize);//初始化新分配内存为255
//使用梯度法寻找,注意不能越界
for(j=1;j<Longsize-1;j++)
{
for(i=1;i<Heightsize-1;i++)
{
fx=Image[(i+1)*Longsize+j]-Image[i*Longsize+j];
fy=Image[i*Longsize+j]-Image[i*Longsize+j+1];
G=(int)sqrt(pow(fx,2)+pow(fy,2));//计算每个象素的梯度
if(G>200)//如果梯度大于阈值,将该象素图黑
Image1[i*Longsize+j]=0;
}
}
return Image1;
}
void main()
{
LPSTR *lbip;
LoadImage();
}