1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 // LkDemoView.cpp : implementation of the CLkDemoView class
48 #include "LkDemoDoc.h"
49 #include "LkDemoView.h"
54 static char THIS_FILE[] = __FILE__;
57 /////////////////////////////////////////////////////////////////////////////
60 IMPLEMENT_DYNCREATE(CLkDemoView, CView)
62 BEGIN_MESSAGE_MAP(CLkDemoView, CView)
63 //{{AFX_MSG_MAP(CLkDemoView)
73 /////////////////////////////////////////////////////////////////////////////
74 // CLkDemoView construction/destruction
76 CLkDemoView::CLkDemoView()
78 m_canvas.Create( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 24 );
84 CLkDemoView::~CLkDemoView()
88 BOOL CLkDemoView::PreCreateWindow(CREATESTRUCT& cs)
90 return CView::PreCreateWindow(cs);
93 /////////////////////////////////////////////////////////////////////////////
94 // CLkDemoView drawing
96 void CLkDemoView::OnDraw(CDC* pDC)
98 CLkDemoDoc* pDoc = GetDocument();
101 IplImage* img = m_camera.GetFrame().GetImage();
104 HDC dstDC = ::GetDC( m_hWnd );
106 GetClientRect( &dr );
108 m_canvas.Create( img->width, img->height, 24 );
109 IplImage* dst_img = m_canvas.GetImage();
114 cvCopy( img, dst_img );
118 int i, count = m_tracker.GetCount();
119 const CPointArray& array = m_tracker.GetPoints();
121 for( i = 0; i < count; i++ )
125 if( i != m_moved_idx )
127 pt = cvPoint( cvRound(array[i].x), cvRound(array[i].y));
128 color = CV_RGB(0,255,0);
132 pt = cvPoint( cvRound(m_moved_point.x),
133 cvRound(m_moved_point.y));
134 color = CV_RGB(0,0,255);
136 cvCircle( dst_img, pt, 3, color, CV_FILLED );
142 m_canvas.DrawToHDC( dstDC, &dr );
143 ::ReleaseDC( m_hWnd, dstDC );
149 pDC->FillSolidRect( r.left, r.top, r.Width(), r.Height(), RGB(0,0,0));
153 /////////////////////////////////////////////////////////////////////////////
154 // CLkDemoView message handlers
156 void CLkDemoView::OnInitialUpdate()
158 CView::OnInitialUpdate();
160 if( m_camera.Initialize( 320, 240, -1, m_hWnd ) == 0 )
162 MessageBox("Can't initialize camera. Try to change format","Error", MB_OK|MB_ICONERROR );
167 void CLkDemoView::OnDestroy()
169 m_camera.Uninitialize();
173 void CLkDemoView::OnLButtonDown(UINT nFlags, CPoint point)
175 CView::OnLButtonDown(nFlags, point);
177 point = ConvertScreenToImage(point);
178 int index = FindPoint(point);
182 m_moved_point = point;
185 else if( m_gray.GetImage() )
187 m_tracker.AddPoint( point, m_gray );
193 void CLkDemoView::OnRButtonDown(UINT nFlags, CPoint point)
195 CView::OnRButtonDown(nFlags, point);
197 point = ConvertScreenToImage(point);
198 int index = FindPoint(point);
201 m_tracker.RemovePoint( index );
208 void CLkDemoView::OnLButtonUp(UINT nFlags, CPoint point)
210 CView::OnLButtonUp(nFlags, point);
212 if( m_moved_idx > 0 && m_gray.GetImage() )
214 m_tracker.MovePoint( m_moved_idx, m_moved_point, m_gray );
222 void CLkDemoView::OnMouseMove(UINT nFlags, CPoint point)
224 CView::OnMouseMove(nFlags, point);
225 if( nFlags & MK_LBUTTON )
227 if( m_moved_idx > 0 && m_gray.GetImage() )
229 m_moved_point = ConvertScreenToImage(point);
236 void CLkDemoView::CheckUpdate()
238 InvalidateRect( 0, FALSE);
241 BOOL CLkDemoView::OnEraseBkgnd(CDC* pDC)
247 void CLkDemoView::StartTracking()
249 double quality = 0.01;
250 double min_distance = 10;
251 int max_features = 300;
252 if( m_camera.IsInitialized())
255 m_tracker.Initialize( m_camera.GetFrame(), max_features, quality, min_distance );
260 void CLkDemoView::StopTracking()
266 void CLkDemoView::TrackFeatures()
268 if( m_track && m_camera.IsInitialized() && m_camera.IsRunning() )
270 CImage& src = m_camera.GetFrame();
271 m_gray.Create( src.Width(), src.Height(), 8 );
272 m_gray.CopyOf( src, 0 );
273 m_tracker.PushFrame( m_gray );
278 int CLkDemoView::FindPoint( CPoint pt )
280 int i, count = m_tracker.GetCount();
283 const CPointArray& array = m_tracker.GetPoints();
285 for( i = 0; i < count; i++ )
287 CvPoint2D32f p1 = array[i];
288 double d = fabs(pt.x - p1.x) + fabs(pt.y - p1.y);
300 CPoint CLkDemoView::ConvertScreenToImage( CPoint point )
303 GetClientRect( &rect );
305 point.x = point.x * m_gray.Width()/MAX(rect.Width(),1);
306 point.y = point.y * m_gray.Height()/MAX(rect.Height(),1);
311 /*void CLkDemoView::OnLButtonDblClk(UINT nFlags, CPoint point)
313 CView::OnLButtonDblClk(nFlags, point);
315 point = ConvertScreenToImage(point);
316 int index = FindPoint( point );
319 m_tracker.RemovePoint( index );
321 else if( m_gray.GetImage() )
323 m_tracker.AddPoint( point, m_gray );