PlateLocateView.cpp
资源名称:车牌识别.rar [点击查看]
上传用户:sgmlaoniu
上传日期:2013-03-16
资源大小:403k
文件大小:21k
源码类别:
Windows编程
开发平台:
Visual C++
- // PlateLocateView.cpp : implementation of the CPlateLocateView class
- //
- #include "stdafx.h"
- #include "PlateLocate.h"
- #include "MainFrm.h"
- #include "DlgIntensity.h"
- #include "PlateLocateDoc.h"
- #include "PlateLocateView.h"
- #include "PlateLocateMethod.h"
- #include "ColorTable.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView
- IMPLEMENT_DYNCREATE(CPlateLocateView, CView)
- BEGIN_MESSAGE_MAP(CPlateLocateView, CView)
- //{{AFX_MSG_MAP(CPlateLocateView)
- ON_WM_ERASEBKGND()
- ON_COMMAND(ID_PLATE_PRE, OnPreProcess)
- ON_COMMAND(ID_VIEW_INTENSITY, OnViewIntensity)
- ON_COMMAND(ID_PLATE_TOGRAY, OnPlateToGray)
- ON_COMMAND(ID_PLATE_EQUA, OnPlateEqua)
- ON_COMMAND(ID_PLATE_ROBERT, OnPlateRobert)
- ON_COMMAND(ID_PLATE_LOCATE, OnPlateLocate)
- ON_COMMAND(ID_PLATE_CUT, OnPlateCut)
- ON_COMMAND(ID_BUTTON_TOGRAY, OnPlateToGray)
- ON_COMMAND(ID_BUTTON_EQUA, OnPlateEqua)
- ON_COMMAND(ID_BUTTON_ROBERT, OnPlateRobert)
- ON_COMMAND(ID_BUTTON_PRE, OnPreProcess)
- ON_COMMAND(ID_BUTTON_LOCATE, OnPlateLocate)
- ON_COMMAND(ID_BUTTON_CUT, OnPlateCut)
- ON_COMMAND(ID_PLATE_FILTER, OnPlateFilter)
- ON_COMMAND(ID_BUTTON_FILTER, OnPlateFilter)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView construction/destruction
- CPlateLocateView::CPlateLocateView()
- {
- // TODO: add construction code here
- m_ipzLeft=0;
- m_ipzRight=0;
- m_ipzBottom=0;
- m_ipzTop=0;
- }
- CPlateLocateView::~CPlateLocateView()
- {
- }
- BOOL CPlateLocateView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView drawing
- void CPlateLocateView::OnDraw(CDC* pDC)
- {
- // 显示等待光标
- BeginWaitCursor();
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // 获取DIB
- HDIB hDIB = pDoc->GetHDIB();
- // 判断DIB是否为空
- if (hDIB != NULL)
- {
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
- // 获取DIB宽度
- int cxDIB = (int) ::DIBWidth(lpDIB);
- // 获取DIB高度
- int cyDIB = (int) ::DIBHeight(lpDIB);
- ::GlobalUnlock((HGLOBAL) hDIB);
- CRect rcDIB;
- rcDIB.top = rcDIB.left = 0;
- rcDIB.right = cxDIB;
- rcDIB.bottom = cyDIB;
- CRect rcDest;
- // 判断是否是打印
- if (pDC->IsPrinting())
- {
- // 是打印,计算输出图像的位置和大小,以便符合页面
- // 获取打印页面的水平宽度(象素)
- int cxPage = pDC->GetDeviceCaps(HORZRES);
- // 获取打印页面的垂直高度(象素)
- int cyPage = pDC->GetDeviceCaps(VERTRES);
- // 获取打印机每英寸象素数
- int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
- int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
- // 计算打印图像大小(缩放,根据页面宽度调整图像大小)
- rcDest.top = rcDest.left = 0;
- rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
- / ((double)cxDIB * cxInch));
- rcDest.right = cxPage;
- // 计算打印图像位置(垂直居中)
- int temp = cyPage - (rcDest.bottom - rcDest.top);
- rcDest.bottom += temp/2;
- rcDest.top += temp/2;
- }
- else
- // 非打印
- {
- // 不必缩放图像
- rcDest = rcDIB;
- }
- // 输出DIB
- ::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
- &rcDIB, pDoc->GetDocPalette());
- }
- // 恢复正常光标
- EndWaitCursor();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView printing
- BOOL CPlateLocateView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CPlateLocateView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CPlateLocateView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView diagnostics
- #ifdef _DEBUG
- void CPlateLocateView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CPlateLocateView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CPlateLocateDoc* CPlateLocateView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPlateLocateDoc)));
- return (CPlateLocateDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CPlateLocateView message handlers
- LRESULT CPlateLocateView::OnDoRealize(WPARAM wParam, LPARAM)
- {
- ASSERT(wParam != NULL);
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 判断DIB是否为空
- if (pDoc->GetHDIB() == NULL)
- {
- // 直接返回
- return 0L;
- }
- // 获取Palette
- CPalette* pPal = pDoc->GetDocPalette();
- if (pPal != NULL)
- {
- // 获取MainFrame
- CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
- ASSERT_KINDOF(CMainFrame, pAppFrame);
- CClientDC appDC(pAppFrame);
- // All views but one should be a background palette.
- // wParam contains a handle to the active view, so the SelectPalette
- // bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
- CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
- if (oldPalette != NULL)
- {
- UINT nColorsChanged = appDC.RealizePalette();
- if (nColorsChanged > 0)
- pDoc->UpdateAllViews(NULL);
- appDC.SelectPalette(oldPalette, TRUE);
- }
- else
- {
- TRACE0("tCCh1_1View::OnPaletteChanged中调用SelectPalette()失败!n");
- }
- }
- return 0L;
- }
- BOOL CPlateLocateView::OnEraseBkgnd(CDC* pDC)
- {
- // 主要是为了设置子窗体默认的背景色
- // 背景色由文档成员变量m_refColorBKG指定
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 创建一个Brush
- CBrush brush(pDoc->m_refColorBKG);
- // 保存以前的Brush
- CBrush* pOldBrush = pDC->SelectObject(&brush);
- // 获取重绘区域
- CRect rectClip;
- pDC->GetClipBox(&rectClip);
- // 重绘
- pDC->PatBlt(rectClip.left, rectClip.top, rectClip.Width(), rectClip.Height(), PATCOPY);
- // 恢复以前的Brush
- pDC->SelectObject(pOldBrush);
- // 返回
- return TRUE;
- //return CView::OnEraseBkgnd(pDC);
- }
- void CPlateLocateView::OnViewIntensity()
- {
- // 查看当前图像灰度直方图
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 指向DIB的指针
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图,其它的可以类推)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- MessageBox("目前只支持查看256色位图灰度直方图!", "系统提示" , MB_ICONINFORMATION | MB_OK);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 返回
- return;
- }
- // 更改光标形状
- BeginWaitCursor();
- // 创建对话框
- CDlgIntensity dlgPara;
- // 初始化变量值
- dlgPara.m_lpDIBBits = lpDIBBits;
- dlgPara.m_lWidth = ::DIBWidth(lpDIB);
- dlgPara.m_lHeight = ::DIBHeight(lpDIB);
- dlgPara.m_iLowGray = 0;
- dlgPara.m_iUpGray = 255;
- // 显示对话框,提示用户设定平移量
- if (dlgPara.DoModal() != IDOK)
- {
- // 返回
- return;
- }
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }
- void CPlateLocateView::OnTempSubrect()
- {
- CPlateLocateDoc* pDoc = GetDocument();
- HDIB hDIB,hNewDIB;
- hDIB=pDoc->GetHDIB();
- long lWidth; //图像宽度和高度
- long lHeight;
- // 指向DIB的指针
- LPSTR lpDIB;
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- // 找到DIB图像象素起始位置
- lWidth = ::DIBWidth(lpDIB); //DIB 宽度
- lHeight = ::DIBHeight(lpDIB); //DIB 高度
- //假定的剪裁区域(车牌附近)
- //
- CRect rect(m_ipzLeft,m_ipzTop,m_ipzRight,m_ipzBottom);
- // CRect rect(m_ipzLeft,190,m_ipzRight,240);
- //CRect rect(0,190,lWidth,260);
- // CRect rect(0,m_ipzTop,lWidth,m_ipzBottom);
- hNewDIB= myCropDIB(hDIB,rect);
- if (OpenClipboard())
- {
- EmptyClipboard();
- SetClipboardData (CF_DIB, CopyHandle((HANDLE) hNewDIB ));
- CloseClipboard();
- }
- }
- void CPlateLocateView::OnPlateToGray()
- {
- CPlateLocateDoc* pDoc = GetDocument(); //获取文档
- LPSTR lpDIB; //指向DIB的指针
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- ConvertToGrayScale(lpDIB);
- OnEditCopy();
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- pDoc->SetModifiedFlag(TRUE);
- pDoc->UpdateAllViews(NULL);
- }
- void CPlateLocateView::OnPlateEqua()
- {
- // 灰度均衡
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 指向DIB的指针
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的直方图均衡,其它的可以类推)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- MessageBox("目前只支持256色位图的直方图均衡!", "系统提示" ,
- MB_ICONINFORMATION | MB_OK);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 返回
- return;
- }
- // 更改光标形状
- BeginWaitCursor();
- // 调用InteEqualize()函数进行直方图均衡
- InteEqualize(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB));
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 更新视图
- pDoc->UpdateAllViews(NULL);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }
- void CPlateLocateView::OnPlateRobert()
- {
- //Robert边缘检测运算
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 指向DIB的指针
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的边缘检测,其它的可以类推)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- MessageBox("目前只支持256色位图的运算!", "系统提示" , MB_ICONINFORMATION | MB_OK);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 返回
- return;
- }
- // 更改光标形状
- BeginWaitCursor();
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- // 调用RobertDIB()函数对DIB进行边缘检测
- if (RobertDIB(lpDIBBits, WIDTHBYTES(::DIBWidth(lpDIB) * 8), ::DIBHeight(lpDIB)))
- {
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 更新视图
- pDoc->UpdateAllViews(NULL);
- }
- else
- {
- // 提示用户
- MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
- }
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }
- void CPlateLocateView::OnThresholdTrans()
- {
- CPlateLocateDoc* pDoc=GetDocument(); //获得文档
- LPSTR lpDIB; //指向DIB的指针
- LPSTR lpDIBBits; //指向DIB的象素的指针
- LONG lLineBytes;
- unsigned char * lpSrc; //指向原图像象素点的指针
- long lWidth; //图像宽度和高度
- long lHeight;
- long i,j; //循环变量
- OnEditCopy();
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- lWidth = ::DIBWidth(lpDIB); //DIB 宽度
- lHeight = ::DIBHeight(lpDIB); //DIB 高度
- // 计算图像每行的字节数
- lLineBytes = WIDTHBYTES(lWidth * 8);
- long lCount[256];
- for(i=0;i<256;i++)
- {
- lCount[i]=0; //清零
- }
- if(::DIBNumColors(lpDIB) != 256) //256色位图不作任何处理
- {
- return;
- }
- for(i=0;i<lHeight;i++)
- {
- for(j=0;j<lWidth;j++)
- {
- lpSrc=(unsigned char *)lpDIB+lLineBytes*i+j;
- lCount[*(lpSrc)]++;
- }
- }
- /*// 灰度均衡
- // 获取文档
- //CTypeRecDoc* pDoc = GetDocument();
- // 指向DIB的指针
- // LPSTR lpDIB;
- // 指向DIB象素指针
- //LPSTR lpDIBBits;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的直方图均衡,其它的可以类推)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- MessageBox("目前只支持256色位图的直方图均衡!", "系统提示" ,
- MB_ICONINFORMATION | MB_OK);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 返回
- return;
- }
- // 更改光标形状
- BeginWaitCursor();
- // 调用InteEqualize()函数进行直方图均衡
- InteEqualize(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB));
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 更新视图
- pDoc->UpdateAllViews(NULL);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();*/
- long total = 0;
- long total1 = 0;
- for(i = 0;i < 256;i++)
- {
- total += lCount[i];
- }
- int aver = total / 256;
- int mostClosed = 0;
- for(i = 0;i < 256;i++)
- {
- if((abs(aver - lCount[i]) < abs(aver - lCount[mostClosed])))
- {
- mostClosed = i;
- }
- }
- for(i = 0;i < 100; i++)
- {
- total1 += lCount[i];
- }
- //求窗口变换的上限和下限
- //
- long temp[16];
- int k=0;
- for(k=0;k<16;k++)
- {
- temp[k]=0;
- for(i=k*16;i<(k+1)*16;i++)
- temp[k]+=lCount[i];
- }
- long max=0;
- int t=0;
- for(k=15;k>=0;k--)
- {
- if(temp[k]>max)
- {
- max=temp[k];
- t=k;
- }
- }
- int bLow=0,bUp=0;
- bLow=(t-1)*16;
- // bUp=(t+5)*16;
- // bLow=100;
- bUp=255;
- // 阈值
- INT bThre;
- bThre=(INT)((2*bUp+bLow)/3);
- if(double(total1) / total > 0.2)//图片比较亮
- bThre = 200;
- if(double(total1) / total > 0.25)
- bThre = 160;
- if(double(total1) / total > 0.3)
- bThre = 150;
- if(double(total1) / total > 0.34)
- bThre = 130;
- if(double(total1) / total > 0.38)
- bThre = 110;
- if(double(total1) / total > 0.4)
- bThre = 100;
- if(double(total1) / total > 0.5)
- bThre = 90;
- if(double(total1) / total > 0.6)//图片比较黑
- bThre = 80;
- // 更改光标形状
- BeginWaitCursor();
- // 调用ThresholdTrans()函数进行阈值变换
- ThresholdTrans(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB), 100);
- pDoc->SetModifiedFlag(TRUE);
- pDoc->UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }
- //(2)削弱背景干扰。对图像B作简单的相邻像素灰度值相减,得到新的图像G,
- //即Gi,j=|Pi,j-Pi,j-1|i=0,1,…,439;j=0,1,…,639Gi,0=Pi,0,左边缘直接赋值,
- //不会影响整体效果。
- //
- void CPlateLocateView::OnWeakenBkgInerfere()
- {
- CPlateLocateDoc* pDoc=GetDocument(); //获得文档
- LPSTR lpDIB; //指向DIB的指针
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- //用自定义的模板消弱背景干扰
- myTemplate(lpDIB);
- pDoc->SetModifiedFlag(TRUE);
- pDoc->UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- }
- void CPlateLocateView::OnMidFilter()
- {
- // 中值滤波
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 指向DIB的指针
- LPSTR lpDIB;
- // 指向DIB象素指针
- LPSTR lpDIBBits;
- // 滤波器的高度
- int iFilterH;
- // 滤波器的宽度
- int iFilterW;
- // 中心元素的X坐标
- int iFilterMX;
- // 中心元素的Y坐标
- int iFilterMY;
- // 锁定DIB
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的中值滤波,其它的可以类推)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- MessageBox("目前只支持256色位图的中值滤波!", "系统提示" ,
- MB_ICONINFORMATION | MB_OK);
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 返回
- return;
- }
- // 创建对话框
- // 初始化变量值
- iFilterH = 5;
- iFilterW = 1;
- iFilterMX = 0;
- iFilterMY = 2;
- // 更改光标形状
- BeginWaitCursor();
- // 调用MedianFilter()函数中值滤波
- if (myMedianFilter(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB),
- iFilterH, iFilterW, iFilterMX, iFilterMY))
- {
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 更新视图
- pDoc->UpdateAllViews(NULL);
- }
- else
- {
- // 提示用户
- MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
- }
- // 解除锁定
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }
- void CPlateLocateView::OnFindPlate()
- {
- CPlateLocateDoc* pDoc=GetDocument(); //获得文档
- LPSTR lpDIB; //指向DIB的指针
- long lWidth; //图像宽度和高度
- long lHeight;
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- lWidth = ::DIBWidth(lpDIB); //DIB 宽度
- lHeight = ::DIBHeight(lpDIB); //DIB 高度
- //水平投影,求取车牌子图像的上下边缘位置
- //
- myHprojectDIB(lpDIB, lWidth, lHeight,&m_ipzTop, &m_ipzBottom) ;
- m_ipzLeft=0;
- m_ipzRight=lWidth;
- HDIB hDIB;
- HDIB hNewDIB;
- hDIB=pDoc->GetHDIB();
- //假定的剪裁区域(车牌附近)
- //
- CRect rect(m_ipzLeft,m_ipzTop,m_ipzRight,m_ipzBottom);
- hNewDIB= myCropDIB(hDIB,rect);
- // 判断是否剪裁成功
- if (hNewDIB != NULL)
- {
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 替换DIB,同时释放旧DIB对象
- pDoc->ReplaceHDIB(hNewDIB);
- // 更新DIB大小和调色板
- pDoc->InitDIBData();
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 实现新的调色板
- OnDoRealize((WPARAM)m_hWnd,0);
- }
- //5
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- lWidth = ::DIBWidth(lpDIB); //DIB 宽度
- lHeight =::DIBHeight(lpDIB); //DIB 高度
- myVprojectDIB(lpDIB, lWidth, lHeight,&m_ipzLeft, &m_ipzRight) ;
- pDoc->UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- }
- void CPlateLocateView::OnEditCopy()
- {
- // 复制当前图像
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 打开剪贴板
- if (OpenClipboard())
- {
- // 更改光标形状
- BeginWaitCursor();
- // 清空剪贴板
- EmptyClipboard();
- // 复制当前图像到剪贴板
- SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
- // 关闭剪贴板
- CloseClipboard();
- // 恢复光标
- EndWaitCursor();
- }
- }
- void CPlateLocateView::OnEditPaste()
- {
- // 粘贴图像
- // 创建新DIB
- HDIB hNewDIB = NULL;
- // 打开剪贴板
- if (OpenClipboard())
- {
- // 更改光标形状
- BeginWaitCursor();
- // 读取剪贴板中的图像
- hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
- // 关闭剪贴板
- CloseClipboard();
- // 判断是否读取成功
- if (hNewDIB != NULL)
- {
- // 获取文档
- CPlateLocateDoc* pDoc = GetDocument();
- // 替换DIB,同时释放旧DIB对象
- pDoc->ReplaceHDIB(hNewDIB);
- // 更新DIB大小和调色板
- pDoc->InitDIBData();
- // 设置脏标记
- pDoc->SetModifiedFlag(TRUE);
- // 重新设置滚动视图大小
- // SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
- // 实现新的调色板
- OnDoRealize((WPARAM)m_hWnd,0);
- // 更新视图
- pDoc->UpdateAllViews(NULL);
- }
- // 恢复光标
- EndWaitCursor();
- }
- }
- void CPlateLocateView::OnPreProcess()
- {
- OnEditPaste();
- OnThresholdTrans();
- OnWeakenBkgInerfere();
- }
- void CPlateLocateView::OnPlateLocate()
- {
- OnFindRightAngle();
- }
- void CPlateLocateView::OnPlateCut()
- {
- OnEditPaste();
- OnTempSubrect();
- OnEditPaste();
- }
- void CPlateLocateView::OnFindRightAngle()
- {
- CPlateLocateDoc* pDoc=GetDocument(); //获得文档
- LPSTR lpDIB; //指向DIB的指针
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- CDC* pDC = GetDC();
- myFindRightAngle(pDC,lpDIB,&m_ipzLeft,&m_ipzTop,&m_ipzRight,&m_ipzBottom);
- DrawRectangle(pDC,m_ipzLeft,m_ipzTop,m_ipzRight,m_ipzBottom);
- }
- void CPlateLocateView::OnPlateFilter()
- {
- CPlateLocateDoc* pDoc=GetDocument(); //获得文档
- LPSTR lpDIB; //指向DIB的指针
- LPSTR lpDIBBits; //指向DIB的象素的指针
- LONG lLineBytes;
- //unsigned char * lpSrc; //指向原图像象素点的指针
- long lWidth; //图像宽度和高度
- long lHeight;
- lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());//获得当前位图
- // 找到DIB图像象素起始位置
- lpDIBBits = ::FindDIBBits(lpDIB);
- lWidth = ::DIBWidth(lpDIB); //DIB 宽度
- lHeight = ::DIBHeight(lpDIB); //DIB 高度
- // 计算图像每行的字节数
- lLineBytes = WIDTHBYTES(lWidth * 8);
- // 更改光标形状
- BeginWaitCursor();
- // 调用ThresholdTrans()函数进行阈值变换
- ThresholdTrans(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB), 128);
- pDoc->SetModifiedFlag(TRUE);
- pDoc->UpdateAllViews(NULL);
- ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
- // 恢复光标
- EndWaitCursor();
- }