matcherView.cpp
上传用户:quan1896
上传日期:2013-04-17
资源大小:94k
文件大小:11k
源码类别:

2D图形编程

开发平台:

Visual C++

  1. // matcherView.cpp : implementation of the CMatcherView class
  2. //
  3. #include "stdafx.h"
  4. #include "matcher.h"
  5. #include "MyOpenCV.h"
  6. #include "matcherDoc.h"
  7. #include "matcherView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CMatcherView
  15. IMPLEMENT_DYNCREATE(CMatcherView, CScrollView)
  16. BEGIN_MESSAGE_MAP(CMatcherView, CScrollView)
  17. //{{AFX_MSG_MAP(CMatcherView)
  18. ON_WM_MOUSEMOVE()
  19. ON_WM_CONTEXTMENU()
  20. ON_COMMAND(ID_DELETE, OnDelete)
  21. //}}AFX_MSG_MAP
  22. // Standard printing commands
  23. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMatcherView construction/destruction
  29. CMatcherView::CMatcherView()
  30. {
  31. // TODO: add construction code here
  32. //已转移至OnUpdate中
  33. // m_frame1.Create(GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),24);
  34. LastHitIndex=-1;
  35. }
  36. CMatcherView::~CMatcherView()
  37. {
  38. }
  39. BOOL CMatcherView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41. // TODO: Modify the Window class or styles here by modifying
  42. //  the CREATESTRUCT cs
  43. return CScrollView::PreCreateWindow(cs);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CMatcherView drawing
  47. void CMatcherView::OnDraw(CDC* pDC)
  48. {
  49. CMatcherDoc* pDoc = GetDocument();
  50. ASSERT_VALID(pDoc);
  51. // TODO: add draw code for native data here
  52.     m_frame1.Show(pDC->GetSafeHdc(),0,0,pDoc->m_imgsize1.width,pDoc->m_imgsize1.height,0,0);
  53. ReleaseDC(pDC);
  54. }
  55. void CMatcherView::OnInitialUpdate()
  56. {
  57. CScrollView::OnInitialUpdate();
  58. CSize sizeTotal;
  59. // TODO: calculate the total size of this view
  60. sizeTotal.cx = sizeTotal.cy = 100;
  61. SetScrollSizes(MM_TEXT, sizeTotal);
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CMatcherView printing
  65. BOOL CMatcherView::OnPreparePrinting(CPrintInfo* pInfo)
  66. {
  67. // default preparation
  68. return DoPreparePrinting(pInfo);
  69. }
  70. void CMatcherView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  71. {
  72. // TODO: add extra initialization before printing
  73. }
  74. void CMatcherView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  75. {
  76. // TODO: add cleanup after printing
  77. }
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CMatcherView diagnostics
  80. #ifdef _DEBUG
  81. void CMatcherView::AssertValid() const
  82. {
  83. CScrollView::AssertValid();
  84. }
  85. void CMatcherView::Dump(CDumpContext& dc) const
  86. {
  87. CScrollView::Dump(dc);
  88. }
  89. CMatcherDoc* CMatcherView::GetDocument() // non-debug version is inline
  90. {
  91. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatcherDoc)));
  92. return (CMatcherDoc*)m_pDocument;
  93. }
  94. #endif //_DEBUG
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CMatcherView message handlers
  97. //Called by the framework after the view’s document has been modified;
  98. // this function is called by CDocument::UpdateAllViews 
  99. void CMatcherView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  100. {
  101. // TODO: Add your specialized code here and/or call the base class
  102. CMatcherDoc* pDoc = GetDocument();
  103. //使Scrollbar符合图像大小
  104. if(pDoc->IMG1OPEN)
  105. SetScrollSizes(MM_TEXT,CSize(pDoc->m_imgsize1.width,pDoc->m_imgsize1.height));
  106. //保证每次创建的显示图像内存大小与待打开图像大小一致
  107. m_frame1.Create(pDoc->m_imgsize1.width,pDoc->m_imgsize1.height,24);
  108. //填充显示图像内存区域
  109. if(pDoc->IMG1OPEN)
  110. {
  111.   ShownImage1=m_frame1.GetImage();
  112. cvSetImageROI(ShownImage1,cvRect(0,0,pDoc->m_imgsize1.width,pDoc->m_imgsize1.height));
  113. cvCopy(pDoc->showImg1,ShownImage1);
  114. Invalidate(FALSE);
  115. }
  116. }
  117. //创建浮动弹出菜单(用于删除匹配点对)
  118. void CMatcherView::OnContextMenu(CWnd* pWnd, CPoint point) 
  119. {
  120. // TODO: Add your message handler code here
  121. if(LastHitIndex!=-1)
  122. {
  123. CMenu menu;
  124. menu.LoadMenu(IDR_POPUP);
  125. menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
  126.    point.x,point.y,this);
  127. }
  128. }
  129. //相应弹出菜单-删除选择的匹配点对
  130. void CMatcherView::OnDelete() 
  131. {
  132. // TODO: Add your command handler code here
  133. CMatcherDoc* pDoc = GetDocument();
  134. if(LastHitIndex!=-1)
  135. {
  136. //改变删除匹配点对颜色
  137. int x1,y1,x2,y2;
  138. x1=cvRound(pDoc->Fmatches1[LastHitIndex].x);
  139. y1=cvRound(pDoc->Fmatches1[LastHitIndex].y);
  140. x2=cvRound(pDoc->Fmatches2[LastHitIndex].x);
  141. y2=cvRound(pDoc->Fmatches2[LastHitIndex].y);
  142. //view1
  143. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(0,0,0),1,4);
  144. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(0,0,0),1,0);
  145. //view2
  146. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(0,0,0),1,4);
  147. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(0,0,0),1,0);
  148. //在状态输出窗口2显示信息
  149. CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;//得到主窗口指针
  150. CString strText;
  151. strText.Format(">>NO.%d->(%f,%f)-----Deleted !",
  152. LastHitIndex,pDoc->Fmatches1[LastHitIndex].x,
  153. pDoc->Fmatches1[LastHitIndex].y);
  154. pFrame->m_wndOutput2.m_ListCtrl.DeleteItem(0);
  155. pFrame->m_wndOutput2.m_ListCtrl.InsertItem(0,strText);
  156. //删除选择的匹配点对
  157. pDoc->Fmatches1[LastHitIndex]=pDoc->Fmatches1[pDoc->nFmatches];
  158. pDoc->Fmatches2[LastHitIndex]=pDoc->Fmatches2[pDoc->nFmatches];
  159. pDoc->nFmatches--;
  160. pDoc->UpdateAllViews(NULL);
  161. }
  162. }
  163. //手工删除或添加匹配点对
  164. void CMatcherView::OnMouseMove(UINT nFlags, CPoint point) 
  165. {
  166. // TODO: Add your message handler code here and/or call default
  167. //在状态栏显示鼠标坐标
  168. CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
  169. CStatusBar* PStatus=&pFrame->m_wndStatusBar;
  170. CString statusmessage;
  171. statusmessage.Format("X: %d ; Y: %d ",point.x,point.y);
  172. PStatus->SetPaneText(1,statusmessage);
  173. CMatcherDoc* pDoc = GetDocument();
  174. if((pDoc->RANSACDONE||pDoc->GUIMATDONE)&&pDoc->ENMANUALDEL)
  175. {
  176. //以下基于Fmatches和Gmatches的代码分开写,是以代码量换速度
  177. //基于Fmatches------------------------------------------------------------------
  178. if(pDoc->RANSACDONE&&!pDoc->GUIMATDONE&&pDoc->ManParPage.m_ManDelBase==0)
  179. {
  180. for(int i=0;i<pDoc->nFmatches;i++)
  181. {
  182. CRect rect;
  183. rect.left =(long)pDoc->Fmatches1[i].x-2;
  184. rect.right=(long)pDoc->Fmatches1[i].x+2;
  185. rect.top=(long)pDoc->Fmatches1[i].y-2;
  186. rect.bottom=(long)pDoc->Fmatches1[i].y+2;
  187. if(rect.PtInRect(point)&&(LastHitIndex!=i))
  188. {
  189. //改变击中角点及连线颜色
  190. int x1,y1,x2,y2;
  191. if(LastHitIndex==-1)
  192. {
  193. LastHitIndex=i;
  194. x1=cvRound(pDoc->Fmatches1[LastHitIndex].x);
  195. y1=cvRound(pDoc->Fmatches1[LastHitIndex].y);
  196. x2=cvRound(pDoc->Fmatches2[LastHitIndex].x);
  197. y2=cvRound(pDoc->Fmatches2[LastHitIndex].y);
  198. //view1
  199. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(255,0,0),1,4);
  200. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  201. //view2
  202. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(255,0,0),1,4);
  203. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  204. }
  205. else
  206. {
  207. x1=cvRound(pDoc->Fmatches1[LastHitIndex].x);
  208. y1=cvRound(pDoc->Fmatches1[LastHitIndex].y);
  209. x2=cvRound(pDoc->Fmatches2[LastHitIndex].x);
  210. y2=cvRound(pDoc->Fmatches2[LastHitIndex].y);
  211. //view1
  212. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(0,255,0),1,4);
  213. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,255,0),1,0);
  214. //view2
  215. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(0,255,0),1,4);
  216. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,255,0),1,0);
  217. LastHitIndex=i;
  218. x1=cvRound(pDoc->Fmatches1[LastHitIndex].x);
  219. y1=cvRound(pDoc->Fmatches1[LastHitIndex].y);
  220. x2=cvRound(pDoc->Fmatches2[LastHitIndex].x);
  221. y2=cvRound(pDoc->Fmatches2[LastHitIndex].y);
  222. //view1
  223. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(255,0,0),1,4);
  224. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  225. //view2
  226. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(255,0,0),1,4);
  227. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  228. }
  229. pDoc->UpdateAllViews(NULL);
  230. //在输出窗口2显示状态信息
  231. CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;//得到主窗口指针
  232. CString strText;
  233. strText.Format(">>NO.%d->(%f,%f)",i,pDoc->Fmatches1[i].x,pDoc->Fmatches1[i].y);
  234. pFrame->m_wndOutput2.m_ListCtrl.InsertItem(0,strText);
  235. }
  236. }
  237. }
  238. //基于Gmatches------------------------------------------------------------------
  239. else if(pDoc->GUIMATDONE&&pDoc->ManParPage.m_ManDelBase==1)
  240. {
  241. for(int i=0;i<pDoc->nGmatches;i++)
  242. {
  243. CRect rect;
  244. rect.left =(long)pDoc->Gmatches1[i].x-2;
  245. rect.right=(long)pDoc->Gmatches1[i].x+2;
  246. rect.top=(long)pDoc->Gmatches1[i].y-2;
  247. rect.bottom=(long)pDoc->Gmatches1[i].y+2;
  248. if(rect.PtInRect(point)&&(LastHitIndex!=i))
  249. {
  250. //改变击中角点及连线颜色
  251. int x1,y1,x2,y2;
  252. if(LastHitIndex==-1)
  253. {
  254. LastHitIndex=i;
  255. x1=cvRound(pDoc->Gmatches1[LastHitIndex].x);
  256. y1=cvRound(pDoc->Gmatches1[LastHitIndex].y);
  257. x2=cvRound(pDoc->Gmatches2[LastHitIndex].x);
  258. y2=cvRound(pDoc->Gmatches2[LastHitIndex].y);
  259. //view1
  260. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(255,0,0),1,4);
  261. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  262. //view2
  263. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(255,0,0),1,4);
  264. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  265. }
  266. else
  267. {
  268. x1=cvRound(pDoc->Gmatches1[LastHitIndex].x);
  269. y1=cvRound(pDoc->Gmatches1[LastHitIndex].y);
  270. x2=cvRound(pDoc->Gmatches2[LastHitIndex].x);
  271. y2=cvRound(pDoc->Gmatches2[LastHitIndex].y);
  272. //view1
  273. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(0,255,0),1,4);
  274. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,255,0),1,0);
  275. //view2
  276. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(0,255,0),1,4);
  277. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,255,0),1,0);
  278. LastHitIndex=i;
  279. x1=cvRound(pDoc->Gmatches1[LastHitIndex].x);
  280. y1=cvRound(pDoc->Gmatches1[LastHitIndex].y);
  281. x2=cvRound(pDoc->Gmatches2[LastHitIndex].x);
  282. y2=cvRound(pDoc->Gmatches2[LastHitIndex].y);
  283. //view1
  284. quPlotCross(pDoc->showImg1,cvPoint(x1,y1),CV_RGB(255,0,0),1,4);
  285. cvLine(pDoc->showImg1,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  286. //view2
  287. quPlotCross(pDoc->showImg2,cvPoint(x2,y2),CV_RGB(255,0,0),1,4);
  288. cvLine(pDoc->showImg2,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(255,0,0),1,0);
  289. }
  290. pDoc->UpdateAllViews(NULL);
  291. //在输出窗口2显示状态信息
  292. CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;//得到主窗口指针
  293. CString strText;
  294. strText.Format(">>NO.%d->(%f,%f)",i,pDoc->Gmatches1[i].x,pDoc->Gmatches1[i].y);
  295. pFrame->m_wndOutput2.m_ListCtrl.InsertItem(0,strText);
  296. }
  297. }
  298. }
  299. }
  300. CScrollView::OnMouseMove(nFlags, point);
  301. }