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

📄 imageprocessing.cpp

📁 车牌识别及定位
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    u12=u20-u02;
	l=atan2( u11, u12 );
	*protate=l/2;
	*pAnew_mon=(double)n;                                                  
//to store the moment 
	A1=a1;
	*(++pAnew_mon)=A1;
	A2=a2;       
	*(++pAnew_mon)=A2;
	A3=a3; 
	*(++pAnew_mon)=A3;
	A4=a4;           
	*(++pAnew_mon)=A4;
	A5=a5;
	*(++pAnew_mon)=A5;
	A6=a6;           
	*(++pAnew_mon)=A6;
	A7=a7;           
	*(++pAnew_mon)=A7;
    if(bmoment)
    {
        wsprintf(filename1,"%s",(LPSTR)ext1); 
		if((ffp=fopen(filename1,"ab"))==NULL)
		{
			return false;
		}
		sprintf(Outtext,"\r\n %-20s%-7ld%-25le%-25le%-25le%-25le%-25le%-25le%-25le%-25le",
			  pDoc->m_Dib.arcFileName,white,n,A1,A2,A3,A4,A5,A6,A7);                 
		fprintf (ffp,"%s",Outtext);	
       	fclose(ffp);                     
     } 
     return true;
}                          

void HorzMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int x,y;
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	for(y=0;y<nHeight;y++)
	{
		for(x=0;x<nWidth;x++)
		{
			lpOutput[x*3+y*nByteWidth]=lpInput[(nWidth-1-x)*3+y*nByteWidth];
			lpOutput[x*3+1+y*nByteWidth]=lpInput[(nWidth-1-x)*3+1+y*nByteWidth];
			lpOutput[x*3+2+y*nByteWidth]=lpInput[(nWidth-1-x)*3+2+y*nByteWidth];
		}
		Progress(y);
	}
}

void VertMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int x,y;
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	for(y=0;y<nHeight;y++)
	{
		for(x=0;x<nByteWidth;x++)
		{
			lpOutput[x+y*nByteWidth]=lpInput[x+(nHeight-y-1)*nByteWidth];
		}
		Progress(y);
	}
}

void CornerMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int x,y;
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	for(y=0;y<nHeight;y++)
	{
		for(x=0;x<nWidth;x++)
		{
			lpOutput[x*3+y*nByteWidth]=lpInput[(nWidth-1-x)*3+(nHeight-y-1)*nByteWidth];
			lpOutput[x*3+1+y*nByteWidth]=lpInput[(nWidth-1-x)*3+1+(nHeight-y-1)*nByteWidth];
			lpOutput[x*3+2+y*nByteWidth]=lpInput[(nWidth-1-x)*3+2+(nHeight-y-1)*nByteWidth];
		}
		Progress(y);
	}
}

#define Point(x,y) lpPoints[(x)+(y)*nWidth]
#define Point1(x,y) lpPoints1[(x)+(y)*nWidth]

void GetPoints(int nWidth,int nHeight,BYTE *lpBits,BYTE *lpPoints)
{
	int x,y,p;
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	for(y=0;y<nHeight;y++)
	{
		for(x=0;x<nWidth;x++)
		{
			p=x*3+y*nByteWidth;
			lpPoints[x+y*nWidth]=(BYTE)(0.299*(float)lpBits[p+2]+0.587*(float)lpBits[p+1]+0.114*(float)lpBits[p]+0.1);
		}
	}
}

void PutPoints(int nWidth,int nHeight,BYTE *lpBits,BYTE *lpPoints)
{
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	int x,y,p,p1;
	for(y=0;y<nHeight;y++)
	{
		for(x=0;x<nWidth;x++)
		{
			p=x*3+y*nByteWidth;
			p1=x+y*nWidth;
			lpBits[p]=lpPoints[p1];
			lpBits[p+1]=lpPoints[p1];
			lpBits[p+2]=lpPoints[p1];
		}
	}
}

void HistogramEq1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput)
{
	int x,y;
	BYTE *lpPoints=new BYTE[nWidth*nHeight];
	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	
	int r[256],s[256];
	ZeroMemory(r,1024);
	ZeroMemory(s,1024);
	for(y=0;y<nHeight;y++){
		for(x=0;x<nWidth;x++){
			r[Point(x,y)]++;
		}
	}
	s[0]=r[0];
	for(y=1;y<256;y++)
	{
		s[y]=s[y-1];
		s[y]+=r[y];
	}
	for(y=0;y<nHeight;y++){
		for(x=0;x<nWidth;x++){
			Point(x,y)=s[Point(x,y)]*255/nWidth/nHeight;
		}
	}

	PutPoints(nWidth,nHeight,lpOutput,lpPoints);
	delete lpPoints;
}

//3x3中值滤波
void Med1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int x,y;

	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	BYTE p[9],s;
	int i,j;
	for(y=1;y<nHeight-1;y++)
	{
		for(x=3;x<nWidth*3-3;x++)
		{
			p[0]=lpInput[x-3+(y-1)*nByteWidth];
			p[1]=lpInput[x+(y-1)*nByteWidth];
			p[2]=lpInput[x+3+(y-1)*nByteWidth];
			p[3]=lpInput[x-3+y*nByteWidth];
			p[4]=lpInput[x+y*nByteWidth];
			p[5]=lpInput[x+3+y*nByteWidth];
			p[6]=lpInput[x-3+(y+1)*nByteWidth];
			p[7]=lpInput[x+(y+1)*nByteWidth];
			p[8]=lpInput[x+3+(y+1)*nByteWidth];
			for(j=0;j<5;j++)
			{
				for(i=j+1;i<9;i++)
				{
					if (p[j]>p[i])
					{
						s=p[j];
						p[j]=p[i];
						p[i]=s;
					}
				}
			}
			lpOutput[x+y*nByteWidth]=p[4];
		}
		Progress(y);
	}
}

void Mean1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int x,y,x1,y1,p;
//	BYTE *lpPoints=new BYTE[nWidth*nHeight];
//	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	int sr,sg,sb;
	int nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	for(y=1;y<nHeight-1;y++)
	{
		for(x=1;x<nWidth-1;x++)
		{
			p=x*3+y*nByteWidth;
			sb=0;
			sg=0;
			sr=0;
			for(y1=-1;y1<=1;y1++)
			for(x1=-1;x1<=1;x1++)
			{
				sb+=lpInput[(y+y1)*nByteWidth+(x+x1)*3];
				sg+=lpInput[(y+y1)*nByteWidth+(x+x1)*3+1];
				sr+=lpInput[(y+y1)*nByteWidth+(x+x1)*3+2];
			}
			lpOutput[p+2]=sr/9;
			lpOutput[p+1]=sg/9;
			lpOutput[p]=sb/9;
		}
		Progress(y);
	}
//	PutPoints(nWidth,nHeight,lpOutput,lpPoints);
//	delete lpPoints;
}


#define pi (double)3.14159265359

/*复数定义*/
typedef struct
{
	double re;
	double im;
}COMPLEX;

/*复数加运算*/
COMPLEX Add(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re=c1.re+c2.re;
	c.im=c1.im+c2.im;
	return c;
}

/*复数减运算*/
COMPLEX Sub(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re=c1.re-c2.re;
	c.im=c1.im-c2.im;
	return c;
}

/*复数乘运算*/
COMPLEX Mul(COMPLEX c1, COMPLEX c2)
{
	COMPLEX c;
	c.re=c1.re*c2.re-c1.im*c2.im;
	c.im=c1.re*c2.im+c2.re*c1.im;
	return c;
}

/*快速付里哀变换
TD为时域值,FD为频域值,power为2的幂数*/
void FFT(COMPLEX * TD, COMPLEX * FD, int power)
{
	int count;
	int i,j,k,bfsize,p;
	double angle;
	COMPLEX *W,*X1,*X2,*X;

	/*计算付里哀变换点数*/
	count=1<<power;
	/*分配运算所需存储器*/
	W=(COMPLEX *)malloc(sizeof(COMPLEX)*count/2);
	X1=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
	X2=(COMPLEX *)malloc(sizeof(COMPLEX)*count);
	/*计算加权系数*/
	for(i=0;i<count/2;i++)
	{
		angle=-i*pi*2/count;
		W[i].re=cos(angle);
		W[i].im=sin(angle);
	}
	/*将时域点写入存储器*/
	memcpy(X1,TD,sizeof(COMPLEX)*count);
	/*蝶形运算*/
	for(k=0;k<power;k++)
	{
		for(j=0;j<1<<k;j++)
		{
			bfsize=1<<(power-k);
			for(i=0;i<bfsize/2;i++)
			{
				p=j*bfsize;
				X2[i+p]=Add(X1[i+p],X1[i+p+bfsize/2]);
				X2[i+p+bfsize/2]=Mul(Sub(X1[i+p],X1[i+p+bfsize/2]),W[i*(1<<k)]);
			}
		}
		X=X1;
		X1=X2;
		X2=X;
	}
	/*重新排序*/
	for(j=0;j<count;j++)
	{
		p=0;
		for(i=0;i<power;i++)
		{
			if (j&(1<<i)) p+=1<<(power-i-1);
		}
		FD[j]=X1[p];
	}
	/*释放存储器*/
	free(W);
	free(X1);
	free(X2);
}
extern void str(int quotient,char addr[15])
{
	int remainder,i,j,k;
	char  s1[15];
	i=0;
	do 
	{
		remainder=quotient%10;
		quotient=quotient/10;
		remainder=remainder+48;
        s1[i]=(char) remainder;
		i++;
	} while(quotient!=0);
    j=0;
	for(k=i-1;k>=0;k--)
	{
		addr[j]=s1[k];
		j++;
	}
	addr[j]=(char) '\0';
    return;
}

/*快速离散余弦变换,利用快速付里哀变换
f为时域值,F为变换域值,power为2的幂数*/
void DCT(double *f, double *F, int power)
{
	int i,count;
	COMPLEX *X;
	double s;

	/*计算离散余弦变换点数*/
	count=1<<power;
	/*分配运算所需存储器*/
	X=(COMPLEX *)malloc(sizeof(COMPLEX)*count*2);
	/*延拓*/
	memset(X,0,sizeof(COMPLEX)*count*2);
	/*将时域点写入存储器*/
	for(i=0;i<count;i++)
	{
		X[i].re=f[i];
	}
	/*调用快速付里哀变换*/
	FFT(X,X,power+1);
	/*调整系数*/
	s=1/sqrt(count);
	F[0]=X[0].re*s;
	s*=sqrt(2);
	for(i=1;i<count;i++)
	{
		F[i]=(X[i].re*cos(i*pi/(count*2))+X[i].im*sin(i*pi/(count*2)))*s;
	}
	/*释放存储器*/
	free(X);
}

/*快速沃尔什-哈达玛变换
f为时域值,F为变换域值,power为2的幂数*/
void WALh(double *f, double *W, int power)
{
	int count;
	int i,j,k,bfsize,p;
	double *X1,*X2,*X;

	/*计算快速沃尔什变换点数*/
	count=1<<power;
	/*分配运算所需存储器*/
	X1=(double *)malloc(sizeof(double)*count);
	X2=(double *)malloc(sizeof(double)*count);
	/*将时域点写入存储器*/
	memcpy(X1,f,sizeof(double)*count);
	/*蝶形运算*/
	for(k=0;k<power;k++)
	{
		for(j=0;j<1<<k;j++)
		{
			bfsize=1<<(power-k);
			for(i=0;i<bfsize/2;i++)
			{
				p=j*bfsize;
				X2[i+p]=X1[i+p]+X1[i+p+bfsize/2];
				X2[i+p+bfsize/2]=X1[i+p]-X1[i+p+bfsize/2];
			}
		}
		X=X1;
		X1=X2;
		X2=X;
	}
	/*调整系数*/
//	for(i=0;i<count;i++)
//	{
//		W[i]=X1[i]/count;
//	}
	for(j=0;j<count;j++)
	{
		p=0;
		for(i=0;i<power;i++)
		{
			if (j&(1<<i)) p+=1<<(power-i-1);
		}
		W[j]=X1[p]/count;
	}
	/*释放存储器*/
	free(X1);
	free(X2);
}


void Fourier1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int w=1,h=1,wp=0,hp=0;
	while(w*2<=nWidth)
	{
		w*=2;
		wp++;
	}
	while(h*2<=nHeight)
	{
		h*=2;
		hp++;
	}
	int x,y;
	BYTE *lpPoints=new BYTE[nWidth*nHeight];
	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	COMPLEX *TD=new COMPLEX[w*h];
	COMPLEX *FD=new COMPLEX[w*h];

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			TD[x+w*y].re=Point(x,y);
			TD[x+w*y].im=0;
		}
	}

	for(y=0;y<h;y++)
	{
		FFT(&TD[w*y],&FD[w*y],wp);
//		Progress(y*nHeight/2/h);
	}
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			TD[y+h*x]=FD[x+w*y];
//			TD[x+w*y]=FD[x*h+y];
		}
	}
	for(x=0;x<w;x++)
	{
		FFT(&TD[x*h],&FD[x*h],hp);
		Progress(x*nHeight/2/w+nHeight/2);
	}

	memset(lpPoints,0,nWidth*nHeight);
	double m;
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			m=sqrt(FD[x*h+y].re*FD[x*h+y].re+FD[x*h+y].im*FD[x*h+y].im)/100;
			if (m>255) m=255;
			Point((x<w/2?x+w/2:x-w/2),nHeight-1-(y<h/2?y+h/2:y-h/2))=(BYTE)(m);
		}
	}
	delete TD;
	delete FD;
	PutPoints(nWidth,nHeight,lpOutput,lpPoints);
	delete lpPoints;
}

void Walsh1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int w=1,h=1,wp=0,hp=0;
	while(w*2<=nWidth)
	{
		w*=2;
		wp++;
	}
	while(h*2<=nHeight)
	{
		h*=2;
		hp++;
	}
	int x,y;
	BYTE *lpPoints=new BYTE[nWidth*nHeight];
	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	double *f=new double[w*h];
	double *W=new double[w*h];

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x+y*w]=Point(x,y);
		}
	}

	for(y=0;y<h;y++)
	{
		WALh(f+w*y,W+w*y,wp);
		Progress(y*nHeight/2/h);
	}
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x*h+y]=W[x+w*y];
		}
	}
	for(x=0;x<w;x++)
	{
		WALh(f+x*h,W+x*h,hp);
//		Progress(x*nHeight/2/w+nHeight/2);
	}
	double a;
	memset(lpPoints,0,nWidth*nHeight);

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			a=fabs(W[x*h+y]*1000);
			if (a>255) a=255;
			Point(x,nHeight-y-1)=(BYTE)a;
		}
	}
	delete f;
	delete W;
	PutPoints(nWidth,nHeight,lpOutput,lpPoints);
	delete lpPoints;
}

void Dct1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos))
{
	int w=1,h=1,wp=0,hp=0;
	while(w*2<=nWidth)
	{
		w*=2;
		wp++;
	}
	while(h*2<=nHeight)
	{
		h*=2;
		hp++;
	}
	int x,y;
	BYTE *lpPoints=new BYTE[nWidth*nHeight];
	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	double *f=new double[w*h];
	double *W=new double[w*h];

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x+y*w]=Point(x,y);
		}
	}

	for(y=0;y<h;y++)
	{
		DCT(&f[w*y],&W[w*y],wp);
//		Progress(y*nHeight/2/h);
	}
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x*h+y]=W[x+w*y];
		}
	}
	for(x=0;x<w;x++)
	{
		DCT(&f[x*h],&W[x*h],hp);
		Progress(x*nHeight/2/w+nHeight/2);
	}
	double a;
	memset(lpPoints,0,nWidth*nHeight);

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			a=fabs(W[x*h+y]);

⌨️ 快捷键说明

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