⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiduruihuadib.cpp

📁 本程序是完成人耳识别
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	Template[4]=9.0;
	Template[5]=-1.0;
	Template[6]=-1.0;
	Template[7]=-1.0;
	Template[8]=-1.0;
	//调用Templat()函数
	Templat( temp1,wide,height,tempH,tempW,tempMX,tempMY,Template,tempC);
	memcpy(p_data, temp1,wide*height);   //复制处理后的图像
	delete  temp1;
}

//根据人耳特征对图像进行边界跟踪
void TiDuRuiHuaDib::Lunkuogenzong()
{
   this->Lunkuotiqu();
   	int i,j;  //循环变量
	int x,y;   //标记搜索点的位置
	int height,wide;  //原图像高,宽
	LPBYTE p_data;    //原图像指针
	LPBYTE p_temp;     //缓存区域
	CPoint upstartpoint;    //人耳最上边缘搜索起始点
	CPoint leftstartpoint;  //人耳最左端边缘的搜索起始点
	bool FindStartPoint;   // 是否找到起始点的布尔变量
	bool FindPoint;     //是否找到点的布尔变量
	p_data=GetData();   //获得原图像数据
	height=GetHeight();
	wide=GetWidth();
	p_temp= new BYTE[height*wide];   //为缓存数据开辟空间
	memset(p_temp,255,height*wide);    //初始化缓存数据
	//寻找起始点
	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 ;     //原图数据区指针
	int wide,height;    //原图长、宽
	// 指向源图像的指针
	LPBYTE	lpSrc;
	// 指向缓存图像的指针
	LPBYTE	lpDst;	
	// 指向缓存DIB图像的指针
	LPBYTE	temp;
	//循环变量
	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++)
		{
			// 指向源图像倒数第j行,第i个象素的指针			
			lpSrc = (LPBYTE)p_data + wide * j + i;
			// 指向目标图像倒数第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;   //循环变量
	int height,wide;  //原图像高、宽
	LPBYTE p_data;    //原图像数据指针
	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]++;    //统计轮廓像素点个数,即周长
			}
		}
	}
	//求出偏转角度
    float bizhi = (float)tezheng[2]/(float)tezheng[0];
	jiaodu=atan(bizhi);
	
	//从对话框输出特征参数
	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;   //循环变量
  int height,wide;  //原图像高、宽
  LPBYTE p_data;    //原图像数据指针
  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;   //循环变量
	int height,wide;  //原图像高、宽
	LPBYTE p_data;    //原图像数据指针
	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;   //循环变量
	int height,wide;  //原图像高、宽
	LPBYTE p_data;    //原图像数据指针
	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 + -