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

📄 bmptestview.cpp

📁 车牌识别及定位
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					if ((unsigned int) *(m_Buffer1+(i+i1)*Width+(j+j1)*3)>50)
						a=1;
				}
			}
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=(a?255:0);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=(a?255:0);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=(a?255:0);
		}
	}
	if(m_Buffer1)
	{
		delete [] m_Buffer1;
		m_Buffer1=0;
	}
	pDoc->m_BM=true;
	Invalidate();	
}

void CBmpTestView::OnEdgeColorprewitt1() 
{
	// TODO: Add your command handler code here
# include <math.h>
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
	if(pDoc->m_Dib.BPP<=8)
	{
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式");
	   return;
	}
	int i,j,k,image[5][4],Height,Width;
	unsigned int a;
	BYTE * m_Buffer1;	
	double e1,e2;
	//filtering
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize];
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++)
	{
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j);
	}
 	for (i=0;i<Height-1;i++)
	{
	    for (j=0;j<(int)Width/3 -1;j++)
		{
			for (k=0;k<3;k++)
			{
				image[1][k+1]=(unsigned int) *(m_Buffer1+i*Width+j*3+k);
				image[2][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j+1)*3+k);
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+j*3+k);
				image[4][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k);
			}
            e1=sqrt(pow((image[1][1]-image[2][1]),2)+pow((image[1][2]-image[2][2]),2)+pow((image[1][3]-image[2][3]),2));
			e2=sqrt(pow((image[3][1]-image[4][1]),2)+pow((image[3][2]-image[4][2]),2)+pow((image[3][3]-image[4][3]),2));
			a=(unsigned int)(e1+e2);
			a=((a>255)?255:a);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a;
		}
	}
	if(m_Buffer1)
	{
		delete [] m_Buffer1;
		m_Buffer1=0;
	}
	pDoc->m_BM=true;
	Invalidate();		
}

void CBmpTestView::OnEdgeColorrobert1() 
{
	// TODO: Add your command handler code here
	
#include <math.h>
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
	if(pDoc->m_Dib.BPP<=8)
	{
       AfxMessageBox( "此图像为256色图像,目前该算子还不支持这种图像格式");
	   return;
	}
	int i,j,k,image[5][4],Height,Width;
	unsigned int a;
	BYTE * m_Buffer1;
	double e2,e1;
	//filtering
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize];
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++)
	{
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j);
	}
 	for (i=0;i<Height-1;i++)
	{
	    for (j=0;j<(int)Width/3 -1;j++)
		{
			for (k=0;k<3;k++)
			{
				image[1][k+1]=(unsigned int) *(m_Buffer1+i*Width+j*3+k);
				image[2][k+1]=(unsigned int) *(m_Buffer1+i*Width+(j+1)*3+k);
				image[3][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+j*3+k);
				image[4][k+1]=(unsigned int) *(m_Buffer1+(i+1)*Width+(j+1)*3+k);
			}
            e1=sqrt(pow((image[1][1]-image[4][1]),2)+pow((image[1][2]-image[4][2]),2)+pow((image[1][3]-image[4][3]),2));
			e2=sqrt(pow((image[2][1]-image[3][1]),2)+pow((image[2][2]-image[3][2]),2)+pow((image[2][3]-image[3][3]),2));
			a=(unsigned int)(e1+e2);
			a=((a>255)?255:a);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=a;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=a;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=a;
		}
	}
	if(m_Buffer1)
	{
		delete [] m_Buffer1;
		m_Buffer1=0;
	}
	pDoc->m_BM=true;
	Invalidate();	
}

void CBmpTestView::OnRowErosion() 
{
	// TODO: Add your command handler code here
	// 形态学:横向收缩,结构差,两边各收缩4个像素
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
	if(pDoc->m_Dib.BPP==8)
		return;
	int i,j,i1,j1,Height,Width,a;
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	BYTE * m_Buffer1;
	m_Buffer1=new BYTE[pDoc->m_Dib.BufferSize];
    for ( j=0;j<(int) pDoc->m_Dib.BufferSize;j++)
	{
	   	*(m_Buffer1+j)=*(pDoc->m_Dib.m_Buffer+j);
	}
 	for (i=2;i<Height-3;i++)
	{
	    for (j=4;j<(int)Width/3-5;j++)
		{
			a=0;
			for (i1=-2;i1<3;i1++)
			{
				for(j1=-4;j1<5;j1++)
				{
					if ((unsigned int) *(m_Buffer1+(i+i1)*Width+(j+j1)*3)<50)
						a=1;
				}
			}
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=(a?0:255);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=(a?0:255);
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=(a?0:255);
		}
	}
	if(m_Buffer1)
	{
		delete [] m_Buffer1;
		m_Buffer1=0;
	}
	pDoc->m_BM=true;
	Invalidate();		
}

void CBmpTestView::OnClose() 
{
	// TODO: Add your command handler code here
	OnRowDilation();
	OnRowDilation();
	OnRowErosion();
//	OnRowDilation();
}

void CBmpTestView::OnGet() 
{
	// TODO: Add your command handler code here
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
	if(pDoc->m_Dib.BPP==8)
		return;
	int j;
    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();			
}

void CBmpTestView::OnRelaScan() 
{
	// TODO: Add your command handler code here
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
# include <math.h>
	int i,j,i1,j1,Height,Width;
//    char s[15]="";
	if(pDoc->m_Dib.BPP==8)
		return;
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	int image[1024],count;
	double diff;
	CString strr,s;
	LPCTSTR fn;
	CFileDialog fd(FALSE,
				   NULL,
				   NULL,
				   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
				   "data files(*.dat)|*.dat|All files(*.*)|*.*||"
				   );
	if(fd.DoModal()==IDOK)
	{
		strr=fd.GetPathName();
		if(strr.Find(".dat")==-1)
		{
			strr=strr+".dat";
		}
	}
	fn=strr.GetBuffer(100);
	if(fn=="")
	{
		return;
	}
	FILE * fp;
	if((fp=fopen(fn,"wb"))==NULL)
	{
		return;
	}

//扫描从图像底部开始	
	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
			image[j]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3);
			if(image[j]<60)
			{
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			}
		} 
		j=0;
		count=0;
		while (j<(int)Width/3)
		{
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2));
			diff=abs(image[j]-image[j+1])*1.7;
			b[i][j]=(((int)diff>150)?1:0);
			count+=b[i][j];
			j++;
		}
//变化15次已是最高限了,再高就会使车牌出现空线
		if (count<15) 
		{
    		for(j=0;j<(int)Width/3;j++)
			{
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			}
		}
	} 
//将模板内计数并存入文件中以观查
	for (i=(int)Width/144;i<Height-(int)Width/144;i++)  //车牌纵横比约为3:1
	{
		for(j=(int)Width/48;j<(int)5*Width/16;j++)   //模板大小取车牌最小值,图像宽度的1/8
		{
//		  str(b[i][j],s);   我的数字——>字符串函数,优点是速度快
			count=0;
			for(i1=i-(int)Width/144;i1<i+(int)Width/144;i1++)
			{
				for(j1=j-(int)Width/48;j1<j+(int)Width/48;j1++)
						count+=b[i1][j1];
			}
			s.Format("%d",count);
		    fprintf(fp,"%s ",s); 	
		}
        fprintf(fp,"\n"); 	
	}
	pDoc->m_BM=true;
	Invalidate();
	fclose(fp);
}



void CBmpTestView::OnDensityScanMid() 
{
	// TODO: Add your command handler code here
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
# include <math.h>
	int i,j,Height,Width;
	if(pDoc->m_Dib.BPP==8)
		return;
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	int image[2048][4],b[2048],count;
	double diff;
//扫描从图像底部开始	
	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
			image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3);
			image[j][2]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+1);
			image[j][3]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3+2);
			if(image[j][1]<60)
			{
				image[j][1]=0;
				image[j][2]=0;
				image[j][3]=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			}
		} 
		j=0;
		count=0;
		while (j<(int)Width/3)
		{
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2));
			diff=abs(image[j][1]-image[j+1][1])*1.7;
			b[j]=(((int)diff>150)?1:0);
			count+=b[j];
			j++;
		}
//变化15次已是最高限了,再高就会使车牌出现空线
		if (count<15) 
		{
    		for(j=0;j<(int)Width/3;j++)
			{
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			}
		}
		else
		{
//分区域扫描法
			count=0;
//第一节
			for(j=0;j<(int)Width/45;j++)
			{
				count+=b[j];
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			}
//剩余部分的简化算法
			do
			{
			//变化6次是最佳经验值
				if(count<5)
				{
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3)=0;
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3+1)=0;
					*(pDoc->m_Dib.m_Buffer+i*Width+(j-(int)Width/90)*3+2)=0;
				}
				count=count+b[j]-b[j-(int)Width/45];
				j++;
			}while(j<(int)Width/3);
			for(j=j-(int)Width/45;j<(int)Width/3;j++)
			{
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
				*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
			} 
//进一步扫描,消除图像中的残留点
			for(j=0;j<(int)Width/3;j++)
			{
				image[j][1]=(unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3);
			} 
			j=0;
			count=0;
			while (j<(int)Width/3)
			{
//因每点三分量均相等,故可简化	diff=sqrt(pow((image[j][1]-image[j+3][1]),2)+pow((image[j][2]-image[j+3][2]),2)+pow((image[j][3]-image[j+3][3]),2));
				diff=abs(image[j][1]-image[j+1][1])*1.7;
				b[j]=(((int)diff>150)?1:0);
				count+=b[j];
				j++;
			}
//变化3次已是最高限了,再高就会使车牌出现空线
			if (count<6) 
			{
    			for(j=0;j<(int)Width/3;j++)
				{
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
					*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
				} 
			} 
		} 
	}
 	for (i=Height-5;i<Height;i++)
	{
	    for (j=0;j<(int)Width/3 ;j++)
		{
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3)=0;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+1)=0;
			*(pDoc->m_Dib.m_Buffer+i*Width+j*3+2)=0;
		}
	} 
	pDoc->m_BM=true;
	Invalidate();	
	
}


void rgbToHSV(float r,float g,float b,float *h,float *s,float *v)
{
	float max=MAX(r,MAX(g,b)),min=MIN(r,MIN(g,b));
	float delta=max-min;
	*v=max;
	if(max!=0.0) *s=delta/max;
	else *s=0.0;
	if(*s==0.0)	*h=NO_HUE
	else
	{		
		if(r==max) *h=(g-b)/delta;
		else if(g==max) *h=2+(b-r)/delta;
		else  *h=4+(r-g)/delta;
		*h=*h*60;
		if(*h<0) *h=*h+360;
	}
}


void CBmpTestView::OnAreaMark() 
{
	// TODO: Add your command handler code here
	CBmpTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);  
	HWND      hWnd;
	int left[100],right[100],top[100],bottom[100],*pIntensity,*pSaturation,*pHue,SaturationI,HueI,IntensityI,i,j,k,premark,min,mark,Blue,Red,Green,markmax,Height,Width,Wid,Hei,*pHeight,*pWidth,*Widmax,*Heimax,CountofBlue,CountofGreen,CountofRed,maxofIntensity,maxofHue,maxofSat,max;
	bool begin;
	float Saturation,Hue,Intensity,	ExpofIntensity,SigmaofIntensity,ExpofHue,SigmaofHue,ExpofSat,SigmaofSat;
	float T0ofInt,T0,u1,u2,T1,w1,w2,u,sig1,sig2,u1ofInt,sig1ofInt,u2ofInt,sig2ofInt,distance,simulate;
	CString s;
	POINT  CurP,NeighborP,SeedP;
	BYTE *lpTempPtr,*lpTempPtr1;
	MYSTACK SeedFillStack;
	if(pDoc->m_Dib.BPP<16)
		return;
	Height=(int) pDoc->m_Dib.Height;
	Width=(int) pDoc->m_Dib.Bpl;
	//建立堆栈,大小与图像相同
	SeedFillStack.InitStack(hWnd,Height*Width);
	mark=0;
//初始化与标号
	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
			b[i][j]=0;
		}
	}
	for (i=0;i<Height;i++)
	{
		for(j=0;j<(int)Width/3;j++)
		{
	//种子法和迷宫法并求出每个区域的最左,最右,最上,最下
			if((unsigned int) *(pDoc->m_Dib.m_Buffer+i*Width+j*3)!=0 && b[i][j]==0)
			{
				mark++;
				left[mark]=(int)Width/3-1;right[mark]=0;bottom[mark]=Height-1;top[mark]=0;
				SeedP.x=j;
				SeedP.y=i;
				SeedFillStack.MyPush(SeedP);
				while(!SeedFillStack.IsStackEmpty())
				{
					CurP=SeedFillStack.MyPop();
					b[CurP.y][CurP.x]=mark;
					if(CurP.y>top[mark])
						top[mark]=CurP.y;
					if(CurP.y<bottom[mark])
						bottom[mark]=CurP.y;
					if(CurP.x>right[mark])
						right[mark]=CurP.x;
					if(CurP.x<left[mark])
						left[mark]=CurP.x;
					lpTempPtr=pDoc->m_Dib.m_Buffer+CurP.y*Width+CurP.x*3;
					*lpTempPtr=255;
					*(lpTempPtr+1)=255;
					*(lpTempPtr+2)=255;
	//标记颜色
/*					switch(mark%4)
					{
					case 0:
						*lpTempPtr=255;
						*(lpTempPtr+1)=0;
						*(lpTempPtr+2)=0;
						break;
					case 1:
						*lpTempPtr=0;
						*(lpTempPtr+1)=255;
						*(lpTempPtr+2)=0;
						break;

⌨️ 快捷键说明

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