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

📄 dipview.cpp

📁 使用OpenCV和C++实现的车牌识别系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
        maxChanges[c]=0;
	    Rows[c]=0; 
		H[c]=0;
		Top[c]=1;
	}

    for(row=0;row<height;row++)
	{
		for(int col=0;col<width-1;col++)
		{	
			if(pDoc->ImgData[row*width+col]!=pDoc->ImgData[row*width+col+1])
				Count[row]++;		
		}
		TempCount[row]=Count[row];
	}

	/////////////////////////////////////Top 10 chage row
	for(int top=0;top<10;top++)
	{
		int Max=0;
		for(row=0;row<height;)
		{
			if(Max<TempCount[row])
			{
				Max=TempCount[row];
			}	
			row=row+10	;
		}
		maxChanges[top]=Max;

		int R=0;
    	for(row=0;row<height;)
		{
	    	if(Max!=TempCount[row])
			{
	    		row=row+10;				
			}	
			else
			{
				R=row;
				break;	
			}
		}
		Rows[top]=R;
		TempCount[R]=0;	
	}

	for(top=0;top<10;top++)
	{
		for(int range=-10;range<10;range++)
		{
			if((0.7*Count[Rows[top]]<=Count[Rows[top]+range])&&(Count[Rows[top]+range]<=1.3*Count[Rows[top]]))		
			{
				 for(int col=0;col<width;col++)
				 {	
		        	g[(Rows[top]+range)*width+col]=255;						
				 }
			}
		}
	}

	 for(row=0;row<height;row++)
	 {
		for(int col=0;col<pDoc->ImgWidth;col++)
		{	
		    if(g[row*width+col]<255)
			{
               g[row*width+col]=0;
			}
		}
	 }	

	int Area=0;                   //标识区域的地点
	if(g[0*width+10]==255)
	{
		Area++;		
		H[Area]++;
	    Top[Area]=0;	
	}
	for(row=1;row<height;row++)
	{	
		if((g[row*width+10]==255) && (g[(row-1)*width+10]==0))
		{
			Area++;		
	        H[Area]++;
	    	Top[Area]=row;				
		}
		else 
		{
			if(g[row*width+10]==255&&g[(row-1)*width+10]==255)
		    {
		    	H[Area]++;
		    }		    
		}
	 }	 

	for(int x=0;x<20;x++)
	{
		int h=H[x];
		if(h>20)                                //特定应用条件,拍摄所得车牌的区域高度、宽度均有一定的范围
		{                                       //分析图象可得
		    int r=0;
			int r0=0;
			r=Top[x];
			r0=Top[x]+h;

			if(r!=0&&r0!=height)
			{
				for(int col=0;col<width;col++)
			    {
					pDoc->ImgData[r*width+col]=255;			
					pDoc->ImgData[r0*width+col]=255;	
				}				
			}
			else
			{
				if(r==0)
				{
					r=5;				
					for(int col=0;col<width;col++)
			        {
						pDoc->ImgData[r*width+col]=255;			
						pDoc->ImgData[r0*width+col]=255;	
					}				
				}
				else
				{
					if(r0==height)
					{
						r0=height-10;
						for(int col=0;col<width;col++)
						{
							pDoc->ImgData[r*width+col]=255;			
							pDoc->ImgData[r0*width+col]=255;	
						}						
					}
				}
			}
		}
	}	

	free(g);
	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);
}

void CDipView::OnBanlance() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	 int width,height;
	 width=pDoc->ImgWidth;
	 height=pDoc->ImgHeight;
	 float num=0;
	
	 int row,col;
	 int count[256];
	 float s0[256];
	 int s[256];                        //256个灰度等级
	 int x=0;
	 int temp=0;

	 for( x=0;x<256;x++)
	 {
		 count[x]=0;
		 s0[x]=0.0;
		 s[x]=0;
	 }

	 for(row=0;row<pDoc->ImgHeight;row++)                          //每个灰度等级象素个数
	 {
		for(col=0;col<pDoc->ImgWidth;col++)
		{	
			temp=pDoc->ImgData[row*width+col];
			count[temp]++;			
		}
	 }	

	 for(x=0;x<256;x++)                                       //对应灰度等级概率
	 {
		 num=(float)width*height;
		 s0[x]=(float)(count[x]/num);
	 }

	 for( x=1;x<256;x++)                                      //概率累计,合并灰度等级    
	 {	
		 s0[x]+=s0[x-1];	 
	 }	

     for( x=0;x<256;x++)                                      //映射
	 {	
		  s[x]=(int)(s0[x]*255+0.5);
	 }

	  for(row=0;row<pDoc->ImgHeight;row++)                          //每个灰度等级象素个数
	 {
		for(col=0;col<pDoc->ImgWidth;col++)
		{	
			int reflect=0;
			temp=pDoc->ImgData[row*width+col];
			reflect=s[temp];
			pDoc->ImgData[row*width+col]=reflect;
		}
	 }

	 IsNewBitmap=true;
	 IsNewFile=true;
	 pDoc->UpdateAllViews(NULL); 	
}

void CDipView::OnSubVertical() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	int width,height;
	width=pDoc->ImgWidth;
	height=pDoc->ImgHeight;

    BYTE *g=new BYTE[width*height];
	
	int row,col,sub;

	for(row=0;row<height-1;row++)
	{
		for(col=0;col<width;col++)
		{			
			sub=abs((pDoc->ImgData[row*width+col])-(pDoc->ImgData[(row+1)*width+col]));
			g[row*width+col]=sub;
		}
	}

	for(row=0;row<height;row++)
	{
		for(col=0;col<width-1;col++)
		{			
			pDoc->ImgData[row*width+col]=g[row*width+col];
		}
	}
	 
	free(g);
	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);
	
}

void CDipView::OnSubHorizontal() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	int width,height;
	width=pDoc->ImgWidth;
	height=pDoc->ImgHeight;

    BYTE *g=new BYTE[width*height];
	
	int row,col,sub;

	for(row=0;row<height;row++)
	{
		for(col=0;col<width-1;col++)
		{			
			sub=abs((pDoc->ImgData[row*width+col])-(pDoc->ImgData[row*width+col+1]));
			if(sub<15)
			{
				g[row*width+col]=0;
			}
			else
			{
				if(sub>15)
				{
					g[row*width+col]=sub+50;
				}
			//	else
			//	g[row*width+col]=sub;
			}
		}
	}

	for(row=0;row<height;row++)
	{
		for(col=0;col<width-1;col++)
		{			
			pDoc->ImgData[row*width+col]=g[row*width+col];
			if(col==0)
			{
				pDoc->ImgData[row*width+col]=0;
			}
		}
	}
	 
	free(g);
	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);
}	


void CDipView::OnMidValue() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	int width,height;
	width=pDoc->ImgWidth;
	height=pDoc->ImgHeight;

    BYTE *g=new BYTE[width*height];
	BYTE temp[25];
	
	int row,col;
	int u,v,a;

	for(row=2;row<height-2;row++)
	{
		for(col=2;col<width-2;col++)
		{	
			for(u=0;u<5;u++)
			{
				for(v=0;v<5;v++)
				{
					temp[u*5+v]=pDoc->ImgData[(row-2+u)*width+(col-2)+v];
				}
			}

			for(u=0;u<25;u++)
			{
				for(v=u+1;v<25;v++)
				{
					if(temp[u]>temp[v])
					{
						a=temp[u];
						temp[u]=temp[v];
						temp[v]=a;
					}
				}
			}

			g[row*width+col]=temp[12];
		}
	}

	for(row=0;row<height;row++)
	{
		for(col=0;col<width-1;col++)
		{			
			pDoc->ImgData[row*width+col]=g[row*width+col];
		}
	}

	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);
}

void CDipView::OnAverage() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	 int width,height;
	 width=pDoc->ImgWidth;
	 height=pDoc->ImgHeight;

	 BYTE *g=new BYTE[width*height];
	
	 int row,col;
	 BYTE s[3][3]={{1,1,1},{1,1,1},{1,1,1}};

	 for(row=1;row<pDoc->ImgHeight-1;row++)
	 {
		for(col=1;col<pDoc->ImgWidth-1;col++)
		{	
			long sum=0;
			for(int i=0;i<3;i++)
			{
				for(int j=0;j<3;j++)
				{
					sum+=s[i][j]*pDoc->ImgData[((row-1+i)*pDoc->ImgWidth)+(col-1+j)];
				}
			}
			g[(row*pDoc->ImgWidth)+col]=(BYTE)(sum/9);
		}
	 }

	 for(row=0;row<height;row++)
	 {
		for(col=0;col<width;col++)
		{			
			pDoc->ImgData[row*width+col]=g[row*width+col];
		}
     }

	 IsNewBitmap=true;
	 IsNewFile=true;
	 pDoc->UpdateAllViews(NULL); 
}

void CDipView::OnBool() 
{
	// TODO: Add your command handler code here
	 CDipDoc *pDoc=GetDocument();

	 int width,height;
	 width=pDoc->ImgWidth;
	 height=pDoc->ImgHeight;
	
	 int row,col;
	
	 for(row=0;row<pDoc->ImgHeight;row++)
	 {
		for(col=0;col<pDoc->ImgWidth;col++)
		{	
		    if(pDoc->ImgData[row*width+col]<255)
			{
               pDoc->ImgData[row*width+col]=0;
			}
		}
	 }
	 
	 IsNewBitmap=true;
	 IsNewFile=true;
	 pDoc->UpdateAllViews(NULL); 	
}

void CDipView::OnVSearch()
{
	// TODO: 在此添加命令处理程序代码

	CDipDoc *pDoc=GetDocument();

	int width,height;
	width=pDoc->ImgWidth;
	height=pDoc->ImgHeight;

	BYTE *g=new BYTE[width*height];

    int *Count= new int[width];
	int *TempCount=new int[width];
	int *maxChanges=new int[30];
	int *Cols=new int[30];                  //maxChanges' col                       	
	
	int *W=new int[30];                     //Top' width
	int *left=new int[30];                   //Top' top col
	 
	for(int row=0;row<pDoc->ImgHeight;row++)
	 {
		for(int col=0;col<pDoc->ImgWidth;col++)
		{	
		   g[row*width+col]=pDoc->ImgData[row*width+col];			
		}
	 }
	 
	for(int c=0;c<width;c++)
	{
		Count[c]=0;
		TempCount[c]=0;
	}

	for(c=0;c<30;c++)
	{
        maxChanges[c]=0;
	    Cols[c]=0; 
		W[c]=0;
		left[c]=-1;
	}

    for(int col=0;col<width;col++)
	{
		for(int row=0;row<height-1;row++)
		{	
			if(pDoc->ImgData[row*width+col]!=pDoc->ImgData[(row+1)*width+col])
				Count[col]++;		
		}
		TempCount[col]=Count[col];
	}

	/////////////////////////////////////Top 30 change col
	for(int top=0;top<30;top++)
	{
		int Max=0;
		for(int col=0;col<width;)
		{
			if(Max<TempCount[col])
			{
				Max=TempCount[col];
			}	
			col=col+10	;
		}
		maxChanges[top]=Max;

		int C=0;
    	for(col=0;col<width;)
		{
	    	if(Max!=TempCount[col])
			{
	    		col=col+10;				
			}	
			else
			{
				C=col;
				break;	
			}
		}
		Cols[top]=C;
		TempCount[C]=0;	
	}	
	
	for(top=0;top<30;top++)
	{
		for(int range=-20;range<20;range++)
		{
			if((Cols[top]+range)>=0&&(Cols[top]+range)<width)
			{
				if((0.8*Count[Cols[top]]<=Count[Cols[top]+range])&&(Count[Cols[top]+range]<=1.2*Count[Cols[top]]))		
				{
					for(int row=0;row<height;row++)
					{	
		        		g[row*width+(Cols[top]+range)]=255;						
					}
				}
			}
			else
			{		
					continue;				
			}

		}
	}

	 for(row=0;row<height;row++)
	 {
		for(int col=0;col<width;col++)
		{	
		    if(g[row*width+col]<255)
			{
               g[row*width+col]=0;
			}
		}
	 }	
	
	int Area=-1;                   //标识区域的地点
	if(g[10*width+0]==255)
	{
		Area++;		
		W[Area]++;
	    left[Area]=0;	
	}
	for(col=1;col<width;col++)
	{	
		if(g[10*width+col]==255 && g[10*width+(col-1)]==0)
		{
			Area++;		
			W[Area]++;
	    	left[Area]=col;				
		}
		else 
		{
			if((g[10*width+col]==255)&&(g[10*width+col-1]==255))
			{
		    	W[Area]++;
			}		    
		}		
	 }
	
	for(int x=0;x<30;x++)
	{
		int w=W[x];
		if(w>10)
		{
		    int c=0;
			int c0=0;
			c=left[x];
			c0=left[x]+w;

			if(c!=0&&c0!=width)
			{
				for(int row=0;row<height;row++)
				{
					pDoc->ImgData[row*width+c]=255;			
					pDoc->ImgData[row*width+c0]=255;	
				}	
			}
			else
			{
				if(c==0)
				{
					c=10;				
					for(int row=0;row<height;row++)
					{
						pDoc->ImgData[row*width+c]=255;			
						pDoc->ImgData[row*width+c0]=255;	
					}	
				}
				else
				{
					if(c0==width)
					{
						c0=width-10;
						for(int row=0;row<height;row++)
					    {
							pDoc->ImgData[row*width+c]=255;			
							pDoc->ImgData[row*width+c0]=255;	
						}	
					}
				}
			}
		}
	}	
	
	free(g);
	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);	
}

/// <summary>

⌨️ 快捷键说明

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