?? tiduruihuadib.cpp
字號:
Template[4]=9.0;
Template[5]=-1.0;
Template[6]=-1.0;
Template[7]=-1.0;
Template[8]=-1.0;
//調用Templat()函數(shù)
Templat( temp1,wide,height,tempH,tempW,tempMX,tempMY,Template,tempC);
memcpy(p_data, temp1,wide*height); //復制處理后的圖像
delete temp1;
}
//根據(jù)人耳特征對圖像進行邊界跟蹤
void TiDuRuiHuaDib::Lunkuogenzong()
{
this->Lunkuotiqu();
int i,j; //循環(huán)變量
int x,y; //標記搜索點的位置
int height,wide; //原圖像高,寬
LPBYTE p_data; //原圖像指針
LPBYTE p_temp; //緩存區(qū)域
CPoint upstartpoint; //人耳最上邊緣搜索起始點
CPoint leftstartpoint; //人耳最左端邊緣的搜索起始點
bool FindStartPoint; // 是否找到起始點的布爾變量
bool FindPoint; //是否找到點的布爾變量
p_data=GetData(); //獲得原圖像數(shù)據(jù)
height=GetHeight();
wide=GetWidth();
p_temp= new BYTE[height*wide]; //為緩存數(shù)據(jù)開辟空間
memset(p_temp,255,height*wide); //初始化緩存數(shù)據(jù)
//尋找起始點
FindStartPoint=false;
for (j=height-4;j>=0&&!FindStartPoint;j--) //從上向下搜索
{
for (i=4;i<wide-4&&!FindStartPoint;i++) //從左向右搜索
{
if (*(p_data+wide*j+i)==0) //找到到起始點
{
upstartpoint.x=i;
upstartpoint.y=j;
FindStartPoint=true;
}
}
}
//以起始點為起點,沿人耳向右下方向搜索
FindPoint=true;
x=upstartpoint.x;
y=upstartpoint.y;
*(p_temp+y*wide+x)=0;
while(FindPoint)
{
//向右搜索
if (*(p_data+y*wide+x+1)==0)
{
*(p_temp+y*wide+x+1)=0;
x++;
}
//向右下搜索
else if (*(p_data+(y-1)*wide+x-1)==0)
{
*(p_temp+(y-1)*wide+x)=0;
y--;
x++;
}
//向右搜索
else if (*(p_data+(y-1)*wide+x)==0)
{
*(p_temp+(y-1)*wide+x)=0;
y--;
}
else
FindPoint=false;
}
//重新回到起始點開始向搜索
x=upstartpoint.x;
y=upstartpoint.y;
FindPoint=true;
while(FindPoint)
{ //向左搜索
if (*(p_data+y*wide+x-1)==0)
{
*(p_temp+y*wide+x-1)=0;
x--;
}
//向左下搜索
else if(*(p_data+(y-1)*wide+x+1)==0)
{
*(p_temp+(y-1)*wide+x+1)=0;
y--;
x--;
}
//向下搜索
else if (*(p_data+(y-1)*wide+x)==0)
{
*(p_temp+(y-1)*wide+x)=0;
y--;
}
/* //向右下搜索
else if (*(p_data+y*wide+x)==0)
{
*(p_temp+y*wide+x)=0;
y--;
x++;
}
//向右搜索
/* else if (*(p_data+y*wide+x+1)==0)
{
*(p_temp+y*wide+x+1)=0;
x++;
}*/
else
FindPoint=false;
}
//尋找人耳最左端點
FindStartPoint=false;
for (i=4;i<wide-4&&!FindStartPoint;i++) //從左向右掃描
{
for(j=4;j<height-4&&!FindStartPoint;j++)
if (*(p_data+j*wide+i)==0)
{
leftstartpoint.x=i;
leftstartpoint.y=j;
FindStartPoint=true;
}
}
x=leftstartpoint.x;
y=leftstartpoint.y;
FindPoint=true;
//從人耳最左端開始沿右下邊緣搜索
while(FindPoint)
{
//向下掃描
if (*(p_data+(y-1)*wide+x)==0)
{
*(p_temp+(y-1)*wide+x)=0;
y--;
}
//向右掃描
else if (*(p_data+y*wide+x)==0)
{
*(p_temp+y*wide+x)=0;
x++;
}
//向上掃描
else if (*(p_data+(y+1)*wide+x)==0)
{
*(p_temp+(y+1)*wide+x)=0;
y++;
}
else
FindPoint=false;
}
/* for (j=0;j<height;j++)
{
for (i=0;i<wide;i++)
{
*(p_temp+j*wide+i)=255-*(p_temp+j*wide+i);
}
}*/
memcpy(p_data,p_temp,height*wide);
delete p_temp;
}
void TiDuRuiHuaDib::Lunkuotiqu()
{
LPBYTE p_data ; //原圖數(shù)據(jù)區(qū)指針
int wide,height; //原圖長、寬
// 指向源圖像的指針
LPBYTE lpSrc;
// 指向緩存圖像的指針
LPBYTE lpDst;
// 指向緩存DIB圖像的指針
LPBYTE temp;
//循環(huán)變量
long i;
long j;
unsigned char n1,n2,n3,n4,n5,n6,n7,n8;
//像素值
unsigned char pixel;
// 暫時分配內存,以保存新圖像
p_data=GetData();
wide=GetWidth();
height=GetHeight();
temp = new BYTE[wide * height];
for (j=0;j<height;j++)
{
for(i=0;i<wide;i++)
{
lpSrc = (LPBYTE)p_data + wide * j + i;
if(*lpSrc>127)
*lpSrc=255;
else
*lpSrc=0;
}
}
// 初始化新分配的內存,設定初始值為255
memset(temp, 255, wide * height);
for(j = 1; j <height-1; j++)
{
for(i = 1;i <wide-1; i++)
{
// 指向源圖像倒數(shù)第j行,第i個象素的指針
lpSrc = (LPBYTE)p_data + wide * j + i;
// 指向目標圖像倒數(shù)第j行,第i個象素的指針
lpDst = (LPBYTE)temp + wide * j + i;
//取得當前指針處的像素值,注意要轉換為unsigned char型
pixel = (unsigned char)*lpSrc;
if(pixel == 0)
{
*lpDst = (unsigned char)0;
n1 = (unsigned char)*(lpSrc + wide -1);
n2 = (unsigned char)*(lpSrc + wide );
n3 = (unsigned char)*(lpSrc + wide +1);
n4 = (unsigned char)*(lpSrc -1);
n5= (unsigned char)*(lpSrc +1);
n6 = (unsigned char)*(lpSrc - wide -1);
n7 = (unsigned char)*(lpSrc - wide );
n8 = (unsigned char)*(lpSrc - wide +1);
//如果相鄰的八個點都是黑點
if(n1+n2+n3+n4+n5+n6+n7+n8==0)
{
*lpDst = (unsigned char)255;
}
}
}
}
// 復制腐蝕后的圖像
memcpy(p_data,temp, wide * height);
// 釋放內存
delete temp;
}
void TiDuRuiHuaDib::Tezhengmiaoshu()
{
int tezheng[5];
float jiaodu;
this->Lunkuogenzong();
int i,j; //循環(huán)變量
int height,wide; //原圖像高、寬
LPBYTE p_data; //原圖像數(shù)據(jù)指針
height = this->GetHeight();
wide= this->GetWidth();
p_data=this->GetData();
CPoint uppoint; //人耳最上點
CPoint leftpoint; //最左點
CPoint downpoint; //最下點
bool Findpoint;
//找到最上點
/* Findpoint=false;
for (j=height-4;j>4&&!Findpoint;j--) //從上向下搜索
{
for (i=4;i<wide-4&&!Findpoint;i++) //從左向右搜索
{
if (*(p_data+j*wide+i)==0)
{
uppoint.x=i;
uppoint.y=j;
Findpoint=true;
}
}
}
//找到最左點
Findpoint=false;
for (i=4;i<wide-4&&!Findpoint;i++) //從左向右掃描
{
for(j=4;j<height-4&&!Findpoint;j++)
if (*(p_data+j*wide+i)==0)
{
leftpoint.x=i;
leftpoint.y=j;
Findpoint=true;
}
}
//找到最下點
Findpoint=false;
for (j=4;j<height&&!Findpoint;j++)
{
for (i=4;i<wide-4&&!Findpoint;i++)
{
if (*(p_data+j*wide+i)==0)
{
downpoint.x=i;
downpoint.y=j;
Findpoint=true;
}
}
}*/
uppoint=this->Getuppoint();
leftpoint=this->Getleftpoint();
downpoint=this->Getdownpoint();
//求出人耳高度
tezheng[0]=uppoint.y-downpoint.y;
//求出人耳寬度
tezheng[1]=downpoint.x-leftpoint.x;
//最上點與最下點之間寬度
tezheng[2]=downpoint.x-uppoint.x;
//最上點與最下點之間的寬度
tezheng[3]=tezheng[1]-tezheng[2];
//求人耳輪廓周長
tezheng[4]=0; //初始化
for (j=4;j<height-4;j++)
{
for (i=4;i<wide-4;i++)
{
if (*(p_data+j*wide+i)==0)
{
tezheng[4]++; //統(tǒng)計輪廓像素點個數(shù),即周長
}
}
}
//求出偏轉角度
float bizhi = (float)tezheng[2]/(float)tezheng[0];
jiaodu=atan(bizhi);
//從對話框輸出特征參數(shù)
DlgCanshu dlg;
CString ss;
ss.Format("圖像大小:%3d * %d",height,wide);
dlg.m_size=ss;
dlg.m_h=tezheng[0];
dlg.m_w=tezheng[1];
dlg.m_w1=tezheng[2];
dlg.m_w2=tezheng[3];
dlg.m_zhouchang=tezheng[4];
dlg.m_jiaodu=jiaodu;
dlg.DoModal();
}
CPoint TiDuRuiHuaDib::Getuppoint()
{
CPoint uppoint;//人耳最上點
int i,j; //循環(huán)變量
int height,wide; //原圖像高、寬
LPBYTE p_data; //原圖像數(shù)據(jù)指針
height = this->GetHeight();
wide= this->GetWidth();
p_data=this->GetData();
bool Findpoint;
//找到最上點
Findpoint=false;
for (j=height-4;j>4&&!Findpoint;j--) //從上向下搜索
{
for (i=4;i<wide-4&&!Findpoint;i++) //從左向右搜索
{
if (*(p_data+j*wide+i)==0)
{
uppoint.x=i;
uppoint.y=j;
Findpoint=true;
}
}
}
return uppoint;
}
CPoint TiDuRuiHuaDib::Getleftpoint()
{
int i,j; //循環(huán)變量
int height,wide; //原圖像高、寬
LPBYTE p_data; //原圖像數(shù)據(jù)指針
height = this->GetHeight();
wide= this->GetWidth();
p_data=this->GetData();
CPoint leftpoint; //最左點
bool Findpoint;
//找到最左點
Findpoint=false;
for (i=4;i<wide-4&&!Findpoint;i++) //從左向右掃描
{
for(j=4;j<height-4&&!Findpoint;j++)
if (*(p_data+j*wide+i)==0)
{
leftpoint.x=i;
leftpoint.y=j;
Findpoint=true;
}
}
return leftpoint;
}
CPoint TiDuRuiHuaDib::Getdownpoint()
{
int i,j; //循環(huán)變量
int height,wide; //原圖像高、寬
LPBYTE p_data; //原圖像數(shù)據(jù)指針
height = this->GetHeight();
wide= this->GetWidth();
p_data=this->GetData();
CPoint downpoint; //最下點
bool Findpoint;
//找到最下點
Findpoint=false;
for (j=4;j<height&&!Findpoint;j++)
{
for (i=4;i<wide-4&&!Findpoint;i++)
{
if (*(p_data+j*wide+i)==0)
{
downpoint.x=i;
downpoint.y=j;
Findpoint=true;
}
}
}
return downpoint;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -