ydpdView.cpp
上传用户:seppec
上传日期:2010-02-09
资源大小:2347k
文件大小:11k
- // ydpdView.cpp : implementation of the CYdpdView class
- //
- #include "stdafx.h"
- #include "ydpd.h"
- #include "ydpdDoc.h"
- #include "ydpdView.h"
- #include "DIB.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView
- DIB cDib;
- extern HANDLE hDIBBK1;
- extern HANDLE hDIBBK2;
- extern HANDLE hDIBBKGray;
- extern HANDLE hDIBBKGradient;
- HANDLE hDIBCurrent = NULL;
- CPoint ImportantPoint,ImportantPoint2,ImpPoiCur;
- CPoint ImportantPointSecond;
- int ImportantColorInfo[RADIUS*2+1][RADIUS*2+1][3];
- int locax,locay;
- IMPLEMENT_DYNCREATE(CYdpdView, CScrollView)
- BEGIN_MESSAGE_MAP(CYdpdView, CScrollView)
- //{{AFX_MSG_MAP(CYdpdView)
- ON_COMMAND(ID_MENU_BK_FIRST, OnMenuBkFirst)
- ON_COMMAND(ID_MENU_BK_SECOND, OnMenuBkSecond)
- ON_COMMAND(ID_MENU_BK_GRAY, OnMenuBkGray)
- ON_COMMAND(ID_MENU_GRADIENT, OnMenuGradient)
- ON_COMMAND(ID_MENU_PIPEIZHONGXIN, OnMenuPipeizhongxin)
- ON_COMMAND(ID_MENU_TEZHENGQUYU, OnMenuTezhengquyu)
- ON_COMMAND(ID_MENU_PIPEI, OnMenuPipei)
- ON_COMMAND(ID_MEDIFILTER, OnMedifilter)
- ON_COMMAND(ID_MENU_FILElOAD, OnMENUFILElOAD)
- ON_COMMAND(ID_MENU_YDPD_PIPEI, OnMenuYdpdPipei)
- ON_COMMAND(ID_MENU_YDPD_MEDI, OnMenuYdpdMedi)
- ON_COMMAND(ID_MENU_YDPD_LKTQ, OnMenuYdpdLktq)
-
- ON_COMMAND(ID_SECOND_PIPEI, OnSecondPipei)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView construction/destruction
- CYdpdView::CYdpdView()
- {
- hDIBBK1 = NULL;
- hDIBBK2 = NULL;
- hDIBBKGray = NULL;
- hDIBBKGradient = NULL;
- isTeZhengZhi = false;
- isBKChonsen = false;
- m_Pointnum =0;
- }
- CYdpdView::~CYdpdView()
- {
- }
- BOOL CYdpdView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CScrollView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView drawing
- void CYdpdView::OnDraw(CDC* pDC)
- {
- CYdpdDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if(hDIBBK1 && hDIBBKGray== NULL)
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(0,0,m_bk_first_width,m_bk_first_height),
- hDIBBK1,CRect(0,0,m_bk_first_width,m_bk_first_height),SRCCOPY);
- if(hDIBBKGray && hDIBBKGradient == NULL)
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(0,0,m_bk_first_width,m_bk_first_height),
- hDIBBKGray,CRect(0,0,m_bk_first_width,m_bk_first_height),SRCCOPY);
- if(hDIBBKGradient)
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(0,0,m_bk_first_width,m_bk_first_height),
- hDIBBKGradient,CRect(0,0,m_bk_first_width,m_bk_first_height),SRCCOPY);
- if(hDIBBK2)
- { SetScrollSizes(MM_TEXT,CSize(m_bk_first_width*2+20,m_bk_first_height*2+10));
- //SetScrollSizes(MM_TEXT,CSize(m_bk_first_width*2+20,m_bk_first_height));
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(m_bk_first_width+5,0,m_bk_first_width*2+5,m_bk_first_height),
- hDIBBK2,CRect(0,0,m_bk_first_width,m_bk_first_height),SRCCOPY);
- }
- if(hDIBCurrent)
- {
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(m_bk_first_width+5,m_bk_first_height+5,m_bk_first_width*2+5,m_bk_first_height*2+5),
- hDIBCurrent,CRect(0,0,m_bk_first_width,m_bk_first_height),SRCCOPY);
- SetScrollSizes(MM_TEXT,CSize(m_bk_first_width*2+20,m_bk_first_height*2+10));
- }
- if(this->isTeZhengZhi)
- cDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(0,m_bk_first_height+10,RADIUS*2+1,m_bk_first_height+10+RADIUS*2+1),
- hDIBBK1,CRect(ImportantPoint.x-RADIUS,m_bk_first_height -ImportantPoint.y-RADIUS,ImportantPoint.x+RADIUS,m_bk_first_height -ImportantPoint.y +RADIUS),SRCCOPY);
-
- }
- void CYdpdView::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- // TODO: calculate the total size of this view
- sizeTotal.cx = sizeTotal.cy = 100;
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView printing
- BOOL CYdpdView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CYdpdView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CYdpdView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView diagnostics
- #ifdef _DEBUG
- void CYdpdView::AssertValid() const
- {
- CScrollView::AssertValid();
- }
- void CYdpdView::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
- CYdpdDoc* CYdpdView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CYdpdDoc)));
- return (CYdpdDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CYdpdView message handlers
- void CYdpdView::OnMenuBkFirst()
- {
-
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL,NULL);
- if(dlg.DoModal() == IDOK)
- {
- CString a;
- a = dlg.GetPathName();
- hDIBBK1 = cDib.LoadDIB(a);
- if(!hDIBBK1)
- {
- AfxMessageBox("open file error");
- return;
- }
- LPBITMAPINFOHEADER lpbi;
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBBK1);
- this->m_bk_first_width = lpbi->biWidth;
- this->m_bk_first_height = lpbi->biHeight;
- GlobalUnlock(hDIBBK1);
- Invalidate();
- }
- }
- void CYdpdView::OnMenuBkSecond()
- {
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL,NULL);
- if(dlg.DoModal() == IDOK)
- {
- hDIBBK2 = cDib.LoadDIB(dlg.GetPathName());
- if(!hDIBBK2)
- {
- AfxMessageBox("open file error");
- return;
- }
-
- Invalidate();
- }
-
- }
- void CYdpdView::OnMenuBkGray()
- {
- hDIBBKGray = cDib.ToGray(hDIBBK1);
- Invalidate();
-
- }
- void CYdpdView::OnMenuGradient()
- {
- hDIBBKGradient = cDib.Gradient(hDIBBKGray);
- Invalidate();
-
- }
- void CYdpdView::OnMenuPipeizhongxin()
- {
- cDib.LocateImporntPoint(hDIBBKGradient,RADIUS,&ImportantPoint);
- Invalidate();
-
- }
- void CYdpdView::OnMenuTezhengquyu()
- {
- long lOffset;
- WORD wBytesPerLine;
- LPBYTE lpData;
- GlobalLock(hDIBBK1);
- wBytesPerLine = cDib.BytePerLine(hDIBBK1);
- lpData = cDib.FindDIBBits(hDIBBK1);
- for(int i=-RADIUS;i<=RADIUS;i++)
- for(int j=-RADIUS;j<=RADIUS;j++)
- {
- lOffset = cDib.PixelOffset(i+ImportantPoint.y,j+ImportantPoint.x,wBytesPerLine);
- ImportantColorInfo[i+RADIUS][j+RADIUS][0] = *(lpData+lOffset++);
- ImportantColorInfo[i+RADIUS][j+RADIUS][1] = *(lpData+lOffset++);
- ImportantColorInfo[i+RADIUS][j+RADIUS][2] = *(lpData+lOffset++);
- }
- this->isTeZhengZhi = true;
- GlobalUnlock(hDIBBK1);
- Invalidate();
- }
- void CYdpdView::OnMenuPipei()
- {
- cDib.MatchImportantPoint(hDIBBK2,ImportantColorInfo,&ImportantPoint2);
- if(cDib.ComPareImg(hDIBBK1,hDIBBK2,ImportantPoint,ImportantPoint2))
- {
- AfxMessageBox("match successed");
- this->isBKChonsen = true;
- }
- else
- AfxMessageBox("match failed");
- Invalidate();
- }
- void CYdpdView::OnMedifilter()
- {
-
- if(hDIBBK1)
- cDib.MedianFilterDIB(hDIBBK1);
- if(hDIBBK2)
- cDib.MedianFilterDIB(hDIBBK2);
- Invalidate();
- }
- void CYdpdView::OnMENUFILElOAD()
- {
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,NULL,NULL);
- if(dlg.DoModal() == IDOK)
- {
- CString a;
- a = dlg.GetPathName();
- hDIBCurrent = cDib.LoadDIB(a);
- if(!hDIBCurrent)
- {
- AfxMessageBox("open file error");
- return;
- }
-
-
- }
-
- }
- void CYdpdView::OnMenuYdpdPipei()
- {
- bool kk = cDib.MatchImportantPoint(hDIBCurrent,ImportantColorInfo,&ImportantPoint2);
-
- if(cDib.ComPareImg(hDIBBK1,hDIBCurrent,ImportantPoint,ImportantPoint2))
- {
- AfxMessageBox("match successed");
- this->isBKChonsen = true;
- }
- else
- AfxMessageBox("match failed");
-
- Invalidate();
-
- }
- void CYdpdView::OnMenuYdpdMedi()
- {
- if(hDIBCurrent)
- {
- cDib.MedianFilterDIB(hDIBCurrent);
- Invalidate();
- }
-
- }
- void CYdpdView::OnMenuYdpdLktq()
- {
- cDib.CompareImg2(hDIBBK1,hDIBCurrent,ImportantPoint,ImportantPoint2);
- LPBITMAPFILEHEADER lpbi;
- lpbi = (LPBITMAPFILEHEADER)GlobalLock(hDIBCurrent);
- LPBYTE lpData = cDib.FindDIBBits(hDIBCurrent);
- long lOffset;
- WORD wBytesPerLine = cDib.BytePerLine(hDIBCurrent);
-
- for(int i= -RADIUS;i<=RADIUS;i++)
- {
- lOffset = cDib.PixelOffset(i+ImportantPoint2.y,RADIUS+ImportantPoint2.x,wBytesPerLine);
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- }
- for( i= -RADIUS;i<=RADIUS;i++)
- {
- lOffset = cDib.PixelOffset(i+ImportantPoint2.y,ImportantPoint2.x-RADIUS,wBytesPerLine);
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- }
- for( i= -RADIUS;i<=RADIUS;i++)
- {
- lOffset = cDib.PixelOffset(ImportantPoint2.y-RADIUS,i+ImportantPoint2.x,wBytesPerLine);
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- }
- for( i= -RADIUS;i<=RADIUS;i++)
- {
- lOffset = cDib.PixelOffset(RADIUS+ImportantPoint2.y,i+ImportantPoint2.x,wBytesPerLine);
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- *(lpData+lOffset++) = 0;
- }
- GlobalUnlock(hDIBCurrent);
- Invalidate();
- }
- //实现图片特征区域的再次匹配
- void CYdpdView::OnSecondPipei()
- {
- LPBITMAPINFOHEADER lpbi;
- LPBYTE lpData;
- WORD wBytesPerLine;
- int height1,width1,height2,width2;
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBBK1);
- //下面的一段代码设定第二个特征区域的查找范围
- //第一个特征区域的纵坐标大于原图高度的一半
- if(lpbi->biHeight/2 < ImportantPoint.y)
- {
- //设定考察的两个纵坐标位置
- height2 = lpbi->biHeight/2;
- height1 = 0;
- }
- else//第一个特征区域的坐标小于等于原图高度的一半
- { //设定纵坐标位置
- height2 = lpbi->biHeight;
- height1 = lpbi->biHeight/2;
- }
- //如果第一个特征区域的横坐标大于原来图片的一半
- if(lpbi->biWidth/2 < ImportantPoint.x)
- { //设定考察区域横坐标的两个值
- width1 = 0;
- width2 = lpbi->biWidth/2;
- }
- else //第一个特征区域的横坐标小于等于原来图片的一半
- { //设定考察区域的横坐标的两个值
- width1 = lpbi->biWidth/2;
- width2 = lpbi->biWidth;
- }
-
- lpData = cDib.FindDIBBits(hDIBBK1);
- //得到每行象素的字节数
- wBytesPerLine = cDib.BytePerLine(hDIBBK1);
- int tempsum=0,sum=0;
- long lOffset;
- //在设定的区域内部查找第二个特征区域
- for(int i=height1+RADIUS;i<height2-RADIUS;i++)
- for(int j=width2+ RADIUS;j<width2-RADIUS;j++)
- { tempsum =0;
- //考察以RADIUS82+1为边长的正方形区域
- for(int k1=-RADIUS;k1<=RADIUS;k1++)
- for(int k2 =-RADIUS;k2<=RADIUS;k2++)
- {
- //得到数据偏移
- lOffset = cDib.PixelOffset(i+k1,j+k2,wBytesPerLine);
- //得到颜色值
- int color = *(lpData+lOffset);
- //增加当前tempsum的值
- tempsum +=color;
- }
- if(tempsum>sum)//当前区域特征更明显
- {
- sum = tempsum;
- //设定区域中心点坐标
- ImportantPointSecond.x = j;
- ImportantPointSecond.y = i;
-
- }
- }
- GlobalUnlock(hDIBBK1);
- //按照新找到的特征区域,再次匹配中心点
- bool kk = cDib.MatchImportantPoint(hDIBCurrent,ImportantColorInfo,&ImportantPoint2);
- //比较图片的相似度
- if(cDib.ComPareImg(hDIBBK1,hDIBCurrent,ImportantPoint,ImportantPoint2))
- AfxMessageBox("match successed");
- else
- AfxMessageBox("match failed");
-
- Invalidate();
- }