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

📄 carlpdlg.cpp

📁 车牌定位c++源码,本人编辑的地方车辆号牌识别.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			if (x0>=buttonx[i] && y0>=buttony[i] && x0<=buttonx[i]+120 && y0<=buttony[i]+24) break;
		}

	}
	if (i<buttonptr) return i;
	return -1;
}

void CCarLPDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int focused=FALSE;
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus!=pos)
		{
			MenuButton(pos,1);
			if (menufocus!=-1) MenuButton(menufocus,0);
			menufocus=pos;
		}
		focused=TRUE;
	}
	if (!focused && menufocus!=-1)
	{
		MenuButton(menufocus,0);
		menufocus=-1;
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CCarLPDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus!=pos)
		{
			if (menufocus!=-1) MenuButton(menufocus,0);
			menufocus=pos;
		}
		MenuButton(pos,2);
		menuclick=pos;
	}
	CDialog::OnLButtonDown(nFlags, point);
}


void CCarLPDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus==pos && menuclick==pos)
		{
			MenuButton(pos,1);
			PostMessage(WM_BUTTONCLICK,pos,0);//产生用户自定义消息WM_BUTTONCLICK
		}
	}
	CDialog::OnLButtonUp(nFlags, point);
}


//add the button
void CCarLPDlg::AddButton(int x,int y,int ID, const char *caption)
{
	buttonx[buttonptr]=x;
	buttony[buttonptr]=y;
	strcpy(buttoncaption[buttonptr],caption);
	buttonid[buttonptr]=ID;
	MenuButton(buttonptr,0);
	buttonptr++;
}
 
//call this function to draw the button in the position pos
void CCarLPDlg::MenuButton(int pos,int status)
{
	CClientDC dc(this);
	DrawButton(&dc,buttonx[pos],buttony[pos],status,buttoncaption[pos],pos);
}

void CCarLPDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CCarLPDlg::OnLButtonDown(nFlags,point);
	CDialog::OnLButtonDblClk(nFlags, point);
}


 
//load the bitmap to the car license recogniton dialog
void CCarLPDlg::LoadBitmap()
{
	BITMAPINFOHEADER *pInfo;
	//lpBitmap points to the beginning of the structure BITMAPFILEHEADER of the bitmap
	pInfo=(BITMAPINFOHEADER *)(lpBitmap+sizeof(BITMAPFILEHEADER)); 
	nWidth=pInfo->biWidth;
	nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	nHeight=pInfo->biHeight;
	if (pInfo->biBitCount!=24)
	{
		if (pInfo->biBitCount!=8)
		{
			AfxMessageBox("无效位图");
			delete lpBitmap;
			lpBitmap=0;
			return;
		}
		unsigned int PaletteSize=1<<pInfo->biBitCount;
		if (pInfo->biClrUsed!=0 && pInfo->biClrUsed<PaletteSize) PaletteSize=pInfo->biClrUsed;
		lpBits=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
		RGBQUAD *pPalette=(RGBQUAD *)lpBits;
		lpBits+=sizeof(RGBQUAD)*PaletteSize;
		nLen=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nByteWidth*nHeight;
		BYTE *lpTemp=lpBitmap;
		lpBitmap=new BYTE[nLen];
		BITMAPFILEHEADER bmh;
		BITMAPINFOHEADER bmi;
		bmh.bfType='B'+'M'*256;
		bmh.bfSize=nLen;
		bmh.bfReserved1=0;
		bmh.bfReserved2=0;
		bmh.bfOffBits=54;

		bmi.biSize=sizeof(BITMAPINFOHEADER);
		bmi.biWidth=nWidth;
		bmi.biHeight=nHeight;
		bmi.biPlanes=1;
		bmi.biBitCount=24;
		bmi.biCompression=BI_RGB;
		bmi.biSizeImage=0;
		bmi.biXPelsPerMeter=0;
		bmi.biYPelsPerMeter=0;
		bmi.biClrUsed=0;
		bmi.biClrImportant=0;
		int nBWidth=pInfo->biWidth;
		if (nBWidth%4) nBWidth+=4-(nBWidth%4);
		memset(lpBitmap,0,nLen); //Sets buffers to a specified character
		memcpy(lpBitmap,&bmh,sizeof(BITMAPFILEHEADER));
		memcpy(lpBitmap+sizeof(BITMAPFILEHEADER),&bmi,sizeof(BITMAPINFOHEADER));
		BYTE *lpBits2=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
		int x,y,p1,p2,Palette;
		for(y=0;y<nHeight;y++)
		{
			for(x=0;x<nWidth;x++)
			{
				p1=y*nBWidth+x;
				p2=y*nByteWidth+x*3;
				if (lpBits[p1]<PaletteSize) Palette=lpBits[p1];
				else Palette=0;
				lpBits2[p2]=pPalette[Palette].rgbBlue;
				lpBits2[p2+1]=pPalette[Palette].rgbGreen;
				lpBits2[p2+2]=pPalette[Palette].rgbRed;
			}
		}
		delete lpTemp;
	}
	lpBits=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	if (lpBackup) delete lpBackup;
	lpBackup=new BYTE[nLen];
	memcpy(lpBackup,lpBitmap,nLen);
}

void CCarLPDlg::OpenFile() 
{
	// TODO: Add your control notification handler code here
	CFile File;
	//CFileException e;

	CFileDialog dlg(TRUE,0,0,OFN_HIDEREADONLY,"位图文件|*.bmp|所有文件|*.*||",this);
	if (dlg.DoModal()==IDOK)
	{
		FileName=dlg.GetPathName();    
		bool flag=false;
		flag=File.Open(FileName,CFile::modeReadWrite);//CFile::modeRead,&e

		TCHAR szError[1024];
        //e.GetErrorMessage(szError, 1024);
		//MessageBox(szError);


		if (!flag) return;
		// TODO: add loading code here
		if (lpBitmap) delete lpBitmap;
		nLen=File.GetLength();
		lpBitmap=new BYTE[nLen];
		File.Read(lpBitmap,nLen);
		LoadBitmap();
		if (lpBitmap) source.SetImage(nWidth,nHeight,lpBits);
	}
}

void CCarLPDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	CCarLPDlg::EndDialog(0);
	
	// TODO: Add your message handler code here
	if (lpBitmap) delete lpBitmap;
	if (lpBackup) delete lpBackup;
	if (lpPoints) delete lpPoints;
}

void HorzMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void VertMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void CornerMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));

 //geometrical transformations
void CCarLPDlg::Geometrical()
{
	if (lpBitmap)
	{
		CProcessDlg dlg;
		dlg.Introduce="几何变换";
		dlg.Process1=HorzMirror;
		dlg.Option1="水平镜像";
		dlg.Process2=VertMirror;
		dlg.Option2="垂直镜像";
		dlg.Process3=CornerMirror;
		dlg.Option3="对角镜像";
		dlg.pDest=&dest;
		dlg.lpInput=lpBits;
		dlg.nWidth=nWidth;
		dlg.nHeight=nHeight;
		dlg.DoModal();
	}
	
}

void HistogramEq1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput);

//直方图均衡化
void CCarLPDlg::HistogramEq() 
{
	if (lpBitmap==0) return;
	BYTE *lpOutput=new BYTE[nByteWidth*nHeight];
	HistogramEq1(nWidth,nHeight,lpBits,lpOutput);
	dest.SetImage(nWidth,nHeight,lpOutput);
	delete lpOutput;
	NoColor();
}

/*
enum {INPUT_FILE,INPUT_GRAB,INPUT_SCAN,
HISTOEQ,HISTOSHOW,SMOOTH,
SHARPEN,PSEUDOCOLOR,BRIGHTMODIFY,
MED,MEAN,GEOMETRICAL,
FOURIER,WALSH,DCT,
SAVEAS,PRINT
};
*/

//responds to the mouse click
LRESULT  CCarLPDlg::OnClick(WPARAM pos,LPARAM dump)
{
	int ID=buttonid[pos];
//	if (Grabbing)
//	{
//		if (ID==INPUT_SNAP) Snap();
//		else MessageBox("采集图像中,请选择\"捕捉图像\"结束");
//	}
	switch(ID)
	{
	case INPUT_FILE:  //open a bitmap
		OpenFile();
		break;
	case INPUT_PASTE: //paste an opened bitmap
		Paste();
		break;
	case INPUT_SCAN:  //scan an opened bitmap
		Scan();
		break;
	case INPUT_GRAB:
//		CFlashWnd::ShowFlash();
//		Grab();
		break;
	case NOCOLOR:     //change to grey bitmap
		NoColor();
		break;
	case NOISE_GAUSSIAN:  //random noise 
		Noise(0);
		break;
	case NOISE_PEPPER:    //pepper noise 
		Noise(1);
		break;
	case INPUT_RESTORE:   //restore the bitmap
		Restore();
		break; 
	case GEOMETRICAL:     //geometircal transformaton
		Geometrical();
		break;
	case HISTOEQ://直方图修改(直方图均衡化)
		HistogramEq();
		break;
	case HISTOSHOW://显示直方图
		if (lpBitmap)
		{
			CHistogramDlg dlg;
			dlg.lpSource=lpBits;
			dlg.lpDest=dest.lpBits;
			dlg.nWidth=nWidth;
			dlg.nHeight=nHeight;
			dlg.nDestWidth=dest.nWidth;
			dlg.nDestHeight=dest.nHeight;
			dlg.DoModal();
		}
		break;
	case SMOOTH://中值滤波
		Smooth();
		break;
	case EDGE: //边缘提取
		Edge();
		break;
	case TRANSFORM: //analytic transformation
		Transform();
		break;
	case BRIGHTMODIFY: //inverse grey
		BrightModify();
		break;
	case BIVALUE:     //change the opened bitmap to binary bitmap
		BiValue();
		break;
	case PSEUDOCOLOR: //change the opened bitmap to pseudo_color bitmap 
		PseudoColor();
		break;
	case OUTPUT_SAVEAS: //save the output bitmap
		SaveAs();
		break;
	case OUTPUT_COPY:   //copy to the clipboard
		Copy();
		break;
	case OUTPUT_PRINT: //print the bitmap
		PrintImages();
		break;
	case -1:          //exit the car license recognition
		CDialog::OnCancel();
		break;
	case -2:          //minimize the CarLPDlg
		ShowWindow(SW_MINIMIZE);
		break;
	case -3:
		HoughIMG();
		break;
	}
	return 0;
}

void Med1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void Mean1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void Gauss1(int nWidth,int nHeight,BYTE *lpIn,BYTE *lpOut,void (* Progress)(int Pos));

void CCarLPDlg::Smooth()
{
	if (!lpBitmap) return;
	CProcessDlg dlg;
	dlg.Introduce="图像复原";
	dlg.Process1=Med1;
	dlg.Option1="中值滤波";
	dlg.Process2=Mean1;
	dlg.Option2="均值滤波";
	dlg.Process3=Gauss1;
	dlg.Option3="高斯模糊";
	dlg.pDest=&dest;
	dlg.lpInput=lpBits;
	dlg.nWidth=nWidth;
	dlg.nHeight=nHeight;
	dlg.DoModal();
	if (dlg.Performed==3)
	{
		NoColor();
	}
}

void Fourier1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void Walsh1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void Dct1(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));

void CCarLPDlg::Transform()
{
	if (lpBitmap)
	{
		CProcessDlg dlg;
		dlg.Introduce="图像变换";
		dlg.Process1=Fourier1;
		dlg.Option1="Fourier变换";
		dlg.Process2=Walsh1;
		dlg.Option2="Walsh变换";
		dlg.Process3=Dct1;
		dlg.Option3="离散余弦变换";

		dlg.pDest=&dest;
		dlg.lpInput=lpBits;
		dlg.nWidth=nWidth;
		dlg.nHeight=nHeight;
		dlg.DoModal();
	}
}


//paste a bitmap from the clipboard
void CCarLPDlg::Paste()
{
	HGLOBAL hMem;
	BYTE *pMem;
	//Nonzero if the Clipboard is opened,or 0 if another application or window has the Clipboard open
	if (OpenClipboard())
	{
		hMem=GetClipboardData(CF_DIB);
		if (hMem)
		{
			pMem=(BYTE *)GlobalLock(hMem);//retrieves data from the clipboard in a specified format
			if (lpBitmap) delete lpBitmap;
			int size=GlobalSize(hMem);
			nLen=size+sizeof(BITMAPFILEHEADER);
			lpBitmap=new BYTE[nLen];
			BITMAPFILEHEADER bmh;
			bmh.bfType='B'+'M'*256;

⌨️ 快捷键说明

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