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

📄 bmptestview.cpp

📁 车牌识别及定位
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					case 2:
						*lpTempPtr=0;
						*(lpTempPtr+1)=0;
						*(lpTempPtr+2)=255;
						break;
					case 3:
						*lpTempPtr=127;
						*(lpTempPtr+1)=127;
						*(lpTempPtr+2)=127;
						break;
					} */
			//left neighbour
					if(CurP.x>0 )
					{
						NeighborP.x=CurP.x-1;
						NeighborP.y=CurP.y;
						lpTempPtr1=lpTempPtr-3;
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0)
							SeedFillStack.MyPush(NeighborP);
					}
		//down neighbour
					if(CurP.y>0)
					{
						NeighborP.x=CurP.x;
						NeighborP.y=CurP.y-1;
						lpTempPtr1=lpTempPtr-Width;
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0)
							SeedFillStack.MyPush(NeighborP);
					}
		//right neighbour
					if(CurP.x<(int)Width/3-1)
					{
						NeighborP.x=CurP.x+1;
						NeighborP.y=CurP.y;
						lpTempPtr1=lpTempPtr+3;
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0)
							SeedFillStack.MyPush(NeighborP);
					}
		//up neighbour
					if(CurP.y<Height-1)
					{
						NeighborP.x=CurP.x;
						NeighborP.y=CurP.y+1;
						lpTempPtr1=lpTempPtr+Width;
						if(*lpTempPtr1!=0 && b[NeighborP.y][NeighborP.x]==0)
							SeedFillStack.MyPush(NeighborP);
					}
				}
			}
		}  //列结束
	} //行结束
	SeedFillStack.DeInitStack();
//计算每一区域的平均高度和宽度及区域面积
	markmax=mark+1;
	pWidth=new int[markmax];
	pHeight=new int[markmax];
	Heimax=new int[markmax];
	Widmax=new int[markmax];
	pIntensity=new int[256*markmax];
	pSaturation=new int[256*markmax];
	pHue=new int[256*markmax];
//先计算各个区域的平均宽度
	for(i=0;i<markmax;i++)
	{
		pWidth[i]=0;
		pHeight[i]=0;
		Heimax[i]=0;
		Widmax[i]=0;
		for (j=0;j<256;j++)
		{
			pIntensity[j+i*256]=0;
			pSaturation[j+i*256]=0;
			pHue[j+i*256]=0;
		}
	}
	for (i=0;i<Height;i++)
	{
		Wid=0;
		premark=0;
		for(j=0;j<(int)Width/3;j++)
		{
			mark=b[i][j];
			if(mark!=0)
			{
				if(mark!=premark)
				{
					pWidth[mark]++;
					Wid++;
					premark=mark;
				}
				else
				{
					pWidth[mark]++;
					Wid++;
				}
			}
			else
			{
				if((Wid>Widmax[premark])&&(premark!=0))
					Widmax[premark]=Wid;
				Wid=0;
				premark=0;
			} 
		}
	} 
//计算各区域的平均高度
	for(j=0;j<(int)Width/3;j++)
	{
		Hei=0;
		premark=0;
		for (i=0;i<Height;i++)
		{
			mark=b[i][j];
			if(mark!=0)
			{
				if(mark!=premark)
				{
					pHeight[mark]++;
					Hei++;
					premark=mark;
				}
				else
				{
					pHeight[mark]++;
					Hei++;
				}
			}
			else
			{
				if((Hei>Heimax[premark])&&(premark!=0))
					Heimax[premark]=Hei;
				Hei=0;
				premark=0;
			} 
		}
	} 
//删除无用的区域
	for(i=1;i<markmax;i++)
	{
//求各区域平均宽度及平均高度
		pWidth[i]/=Heimax[i];
		pHeight[i]/=Widmax[i];
		if(pWidth[i]>Width/9||pWidth[i]<Width/30)
			Widmax[i]=0;
		else
		{
			if(abs(pWidth[i]/pHeight[i]-2.5)>2)
				Widmax[i]=0;
		}
	}
	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
			mark=b[i][j];
			if(mark!=0)
			{
				if(Widmax[mark]==0)
				{
					lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3;
					*lpTempPtr=0;
					*(lpTempPtr+1)=0;
					*(lpTempPtr+2)=0;
					b[i][j]=0;
				}
				else
				{
					lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3;
					*lpTempPtr=255;
					*(lpTempPtr+1)=255;
					*(lpTempPtr+2)=255;
				}
			}
		}
	}
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++)
	{
	   	*(pDoc->m_Dib.m_Buffer+j)=(*(pDoc->m_Dib.m_Buffer+j))&(*(pDoc->m_Dib.m_Buffer1+j));
	}
	pDoc->m_BM=true;
	Invalidate();	
	AfxMessageBox("下面是光强、色度、色饱和度直方图显示!");
	m_Gradmax=0;
 	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
			mark=b[i][j];
			if(mark!=0)
			{
				lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+j*3;
			    Blue=(int) *lpTempPtr;
				Green=(int) *(lpTempPtr+1);
				Red=(int) *(lpTempPtr+2);
				rgbToHSV(Red,Green,Blue,&Hue,&Saturation,&Intensity);
//如下是三个量的量化过程
				HueI=(int) (Hue*255/360);
				SaturationI=(int) (Saturation*255);
				IntensityI= (int) Intensity;
// 直方图统计过程
				pIntensity[IntensityI+mark*256]++;
				pHue[HueI+mark*256]++;
				pSaturation[SaturationI+mark*256]++; 
			}
		}
	}
	for(j=0;j<markmax;j++)
	{
		m_Gradmax=0;
		CountofBlue=0;
		CountofGreen=0;
		CountofRed=0;
	    for ( i=0;i<256;i++)
		{
			m_Grade_Blue[i]=pIntensity[i+j*256];
			m_Grade_Green[i]=pHue[i+j*256];
			m_Grade_Red[i]=pSaturation[i+j*256];
			if (m_Grade_Blue[i]>m_Gradmax)
				m_Gradmax=m_Grade_Blue[i];
			if (m_Grade_Green[i]>m_Gradmax)
				m_Gradmax=m_Grade_Green[i];
			if (m_Grade_Red[i]>m_Gradmax)
				m_Gradmax=m_Grade_Red[i];
			CountofBlue=CountofBlue+m_Grade_Blue[i];
			CountofGreen=CountofGreen+m_Grade_Green[i];
			CountofRed=CountofRed+m_Grade_Red[i];		
		}

		if (m_Gradmax>0)
		{
			pDoc->m_BM=false;
			Invalidate();
			ExpofIntensity=0;
			ExpofHue=0;
			SigmaofIntensity=0;
			SigmaofHue=0;
			ExpofSat=0;SigmaofSat=0;
// Calculate estimate and maxpoint of Intensity and Hue
			max=0;min=0;
		    for ( i=0;i<256;i++)
			{
				if(max<m_Grade_Blue[i])
				{
					max=m_Grade_Blue[i];
					maxofIntensity=i;
				}
				if(min<m_Grade_Red[i])
				{
					min=m_Grade_Red[i];
					maxofSat=i;
				}
				ExpofIntensity=ExpofIntensity+(float) i*m_Grade_Blue[i]/CountofBlue;
				ExpofHue=ExpofHue+(float) i*m_Grade_Green[i]/CountofGreen;
				ExpofSat=ExpofSat+(float) i*m_Grade_Red[i]/CountofRed;
			}
			max=0;
			for ( i=0;i<256;i++)
			{
				if(max<m_Grade_Green[i])
				{
					max=m_Grade_Green[i];
					maxofHue=i;
				}
				SigmaofIntensity=SigmaofIntensity+(float) (i-ExpofIntensity)*(i-ExpofIntensity)*m_Grade_Blue[i]/CountofBlue;
				SigmaofHue=SigmaofHue+(float) (i-ExpofHue)*(i-ExpofHue)*m_Grade_Green[i]/CountofGreen;
				SigmaofSat=SigmaofSat+(float) (i-ExpofSat)*(i-ExpofSat)*m_Grade_Red[i]/CountofRed;
			}
			SigmaofIntensity=sqrt(SigmaofIntensity);
			SigmaofHue=sqrt(SigmaofHue);
			SigmaofSat=sqrt(SigmaofSat);
//estimate if there is dual peak of Intensity
			T0=ExpofIntensity;T1=0;
			while (fabs(T0-T1)>0.01)
			{
				w1=0;w2=0;u1=0;u2=0;sig1=0;sig2=0;
				for(i=0;i<=int(T0);i++)
				{
					w1=w1+(float)m_Grade_Blue[i]/CountofBlue;
					u1=u1+(float)i*m_Grade_Blue[i]/CountofBlue;
				}
				u1=u1/w1;
				for(i=0;i<=int(T0);i++)
				{
					sig1=sig1+(float)(i-u1)*(i-u1)*m_Grade_Blue[i]/CountofBlue;
				}
				for(i=int(T0)+1;i<256;i++)
				{
					w2=w2+(float)m_Grade_Blue[i]/CountofBlue;
					u2=u2+(float)i*m_Grade_Blue[i]/CountofBlue;
				}
				u2=u2/w2;
				for(i=int(T0)+1;i<256;i++)
				{
					sig2=sig2+(float)(i-u2)*(i-u2)*m_Grade_Blue[i]/CountofBlue;
				}
				T1=T0;T0=0.5*(u1+u2);sig1=sqrt(sig1);sig2=sqrt(sig2);
			}
			T0ofInt=T0;u1ofInt=u1;sig1ofInt=sig1;u2ofInt=u2;sig2ofInt=sig2;
//estimate if there is dual peak of Hue
			T0=ExpofHue;T1=0;
			while (fabs(T0-T1)>0.01)
			{
				w1=0;w2=0;u1=0;u2=0;sig1=0;sig2=0;
				for(i=0;i<=int(T0);i++)
				{
					w1=w1+(float)m_Grade_Green[i]/CountofGreen;
					u1=u1+(float)i*m_Grade_Green[i]/CountofGreen;
				}
				u1=u1/w1;
				for(i=0;i<=int(T0);i++)
				{
					sig1=sig1+(float)(i-u1)*(i-u1)*m_Grade_Green[i]/CountofGreen;
				}
				for(i=int(T0)+1;i<256;i++)
				{
					w2=w2+(float)m_Grade_Green[i]/CountofGreen;
					u2=u2+(float)i*m_Grade_Green[i]/CountofGreen;
				}
				u2=u2/w2;
				for(i=int(T0)+1;i<256;i++)
				{
					sig2=sig2+(float)(i-u2)*(i-u2)*m_Grade_Green[i]/CountofGreen;
				}
				T1=T0;T0=0.5*(u1+u2);sig1=sqrt(sig1);sig2=sqrt(sig2);
			}
			s.Format("ExpofIntensity=%f,SigmaofIntensity=%f,T0ofIntensity=%f,u1ofInt=%f,sig1ofInt=%f,u2ofInt=%f,sig2ofInt=%f,!!!!!!,ExpofHue=%f,SigmaofHue=%f,T0=%f,u1=%f,sig1=%f,u2=%f,sig2=%f,%d,%d",ExpofIntensity,SigmaofIntensity,T0ofInt,u1ofInt,sig1ofInt,u2ofInt,sig2ofInt,ExpofHue,SigmaofHue,T0,u1,sig1,u2,sig2,maxofIntensity,maxofHue);
			AfxMessageBox(s);
// judge if Intensity is dual peak or single peak
			if (abs(ExpofIntensity-maxofIntensity)>15)
// dual peak
            {
				if (abs(u1ofInt-maxofIntensity)<15)
				{
					ExpofIntensity=u1ofInt;
					SigmaofIntensity=sig1ofInt;
				}
				else
				{
					if (abs(u2ofInt-maxofIntensity)<15)
					{
						ExpofIntensity=u2ofInt;
						SigmaofIntensity=sig2ofInt;
					}
//					else
//						ExpofIntensity=-1;
				}
			}
// judge if Hue is dual peak or single peak
			if (abs(ExpofHue-maxofHue)>15)
// dual peak
            {
				if (abs(u1-maxofHue)<15)
				{
					ExpofHue=u1;
					SigmaofHue=sig1;
				}
				else
				{
					if (abs(u2-maxofHue)<15)
					{
						ExpofHue=u2;
						SigmaofHue=sig2;
					}
					else
						ExpofHue=-1;
				}
			}
			s.Format("ExpofIntensity=%f,SigmaofIntensity=%f,ExpofHue=%f,SigmaofHue=%f,ExpofSat=%f,SigmaofSat=%f",ExpofIntensity,SigmaofIntensity,ExpofHue,SigmaofHue,ExpofSat,SigmaofSat);
			AfxMessageBox(s);
// estimate the color of vehicle plate
			//deal with blue
			begin=false;
			if(abs(ExpofHue-150)<19||(ExpofHue==-1&&abs(maxofHue-140)<9))
			{
				if((ExpofIntensity>60)&&(ExpofIntensity<242))
				{
					s.Format("车牌为兰色");
					begin=true;
				}
				else
				{
					if(ExpofIntensity<=60)
					{
						s.Format("车牌为黑色");
						begin=true;
					}
					else
					{
						s.Format("无用区域,删除");
						for (i=0;i<Height;i++)
						{
							for(k=0;k<(int)Width/3;k++)
							{
								mark=b[i][k];
								if(mark==j)
								{
									lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3;
									*lpTempPtr=0;
									*(lpTempPtr+1)=0;
									*(lpTempPtr+2)=0;
									b[i][k]=0;
								}
							}
						}
					}
				}
			}
			else
			{ //deal with yellow
				if(abs(ExpofHue-33)<5||(ExpofHue==-1&&abs(maxofHue-33)<5))
				{
					if ((ExpofIntensity>60)&&(ExpofIntensity<242))
					{
						s.Format("车牌为黄色");
						begin=true;
					}
				}
				else
				{
					if(ExpofIntensity<=60&&ExpofIntensity>=0)
					{
						s.Format("车牌为黑色");
						begin=true;
					}
					else
					{
						if(ExpofIntensity>242||(ExpofIntensity==-1&&maxofIntensity>242))
						{
							s.Format("车牌为白色");
							begin=true;
						}
						else
						{
							s.Format("无用区域,删除");
							for (i=0;i<Height;i++)
							{
								for(k=0;k<(int)Width/3;k++)
								{
									mark=b[i][k];
									if(mark==j)
									{
										lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3;
										*lpTempPtr=0;
										*(lpTempPtr+1)=0;
										*(lpTempPtr+2)=0;
										b[i][k]=0;
									}
								}
							}
						}
					}
				}
			}
			AfxMessageBox(s);
			if(begin)
			{
			 	for (i=bottom[j]-3;i<=top[j]+3;i++)
				{
					for(k=left[j];k<=right[j];k++)
					{
						lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3;
						*lpTempPtr=255;
						*(lpTempPtr+1)=255;
						*(lpTempPtr+2)=255;
					}
				}
//			s.Format("left=%d,right=%d,bottom=%d,top=%d",left[j],right[j],bottom[j],top[j]);
//			AfxMessageBox(s);
			 	for (i=bottom[j]-3;i<=top[j]+3;i++)
				{
					for(k=left[j];k<=right[j];k++)
					{
						lpTempPtr=pDoc->m_Dib.m_Buffer1+i*Width+k*3;
					    Blue=(int) *lpTempPtr;
						Green=(int) *(lpTempPtr+1);
						Red=(int) *(lpTempPtr+2);
						rgbToHSV(Red,Green,Blue,&Hue,&Saturation,&Intensity);
//如下是三个量的量化过程
						HueI=(int) (Hue*255/360);
						SaturationI=(int) (Saturation*255);
						IntensityI= (int) Intensity;
						lpTempPtr=pDoc->m_Dib.m_Buffer+i*Width+k*3;
						distance=sqrt(pow(SaturationI*cos(HueI*2*pi/255)/255-ExpofSat*cos(ExpofHue*2*pi/255)/255,2)+pow(SaturationI*sin(HueI*2*pi/255)/255-ExpofSat*sin(ExpofHue*2*pi/255)/255,2)+pow((IntensityI-ExpofIntensity)/255,2));
						simulate=1-distance/sqrt(5);
//						s.Format("simulate=%f",simulate);
//						AfxMessageBox(s);
						if(simulate>0.85)
						{
							*lpTempPtr=255;
							*(lpTempPtr+1)=255;
							*(lpTempPtr+2)=255;
						}
						else
						{
							*lpTempPtr=0;
							*(lpTempPtr+1)=0;
							*(lpTempPtr+2)=0;
						}
					}
				}
			}
		}
	}
    for 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -