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
45 #pragma warning( disable: 4100 4663 4189 4101 4018 4710 )
52 /////////////////////////////////////////////////////////////////////////////
53 // CLkDemoView construction/destruction
55 CLkDemoView::CLkDemoView()
57 //m_canvas.Create( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 24 );
65 CLkDemoView::~CLkDemoView()
69 void CLkDemoView::SetSize (int w,int h)
75 CvPoint CLkDemoView::ConvertScreenToImage( CvPoint point )
77 IplImage* img = m_gray.GetImage();
80 CvSize size = cvGetSize(img);
82 point.x *= size.width/m_w;
83 point.y *= size.height/m_h;
86 point.x = point.y = 0;
91 /////////////////////////////////////////////////////////////////////////////
92 // CLkDemoView drawing
94 /*void CLkDemoView::OnDraw(CDC* pDC)
96 CLkDemoDoc* pDoc = GetDocument();
99 IplImage* img = m_camera.GetFrame().GetImage();
102 HDC dstDC = ::GetDC( m_hWnd );
103 HDC srcDC = m_canvas.GetDC();
108 GetClientRect( &dr );
109 IplImage* dst_img = m_canvas.GetImage();
110 IplROI roi = RectToROI( dr );
115 //iplResize( img, dst_img, roi.width, img->width, roi.height, img->height,
116 // IPL_INTER_LINEAR );
119 {(double)roi.width/img->width, 0, 0},
120 {0, (double)roi.height/img->height, 0}
123 iplWarpAffine( img, dst_img, a, IPL_INTER_LINEAR );
127 iplSet( dst_img, 0 );
130 int i, count = m_tracker.GetCount();
131 const CPointArray& array = m_tracker.GetPoints();
132 double kx = ((double)roi.width)/(img->width + 0.001);
133 double ky = ((double)roi.height)/(img->height + 0.001);
135 for( i = 0; i < count; i++ )
139 if( i != m_moved_idx )
141 pt = cvPoint( cvRound(array[i].x*kx), cvRound(array[i].y*ky));
142 color = RGB(0,255,0);
146 pt = cvPoint( cvRound(m_moved_point.x*kx),
147 cvRound(m_moved_point.y*ky));
148 color = RGB(255,0,0);
150 cvCircle( dst_img, pt, 3, color, CV_FILLED );
160 BitBlt( dstDC, 0, 0, dr.Width(), dr.Height(), m_canvas.GetDC(), 0, 0, SRCCOPY );
162 ::ReleaseDC( m_hWnd, dstDC );
168 pDC->FillSolidRect( r.left, r.top, r.Width(), r.Height(), RGB(0,0,0));
172 /////////////////////////////////////////////////////////////////////////////
173 // CLkDemoView message handlers
175 /*void CLkDemoView::OnLButtonDown(UINT nFlags, CvPoint point)
177 CView::OnLButtonDown(nFlags, point);
179 point = ConvertScreenToImage(point);
180 int index = FindPoint(point);
184 m_moved_point = point;
187 else if( m_gray.GetImage() )
189 m_tracker.AddPoint( point, m_gray );
195 void CLkDemoView::OnRButtonDown(UINT nFlags, CvPoint point)
197 CView::OnRButtonDown(nFlags, point);
199 point = ConvertScreenToImage(point);
200 int index = FindPoint(point);
203 m_tracker.RemovePoint( index );
210 void CLkDemoView::OnLButtonUp(UINT nFlags, CvPoint point)
212 CView::OnLButtonUp(nFlags, point);
214 if( m_moved_idx > 0 && m_gray.GetImage() )
216 m_tracker.MovePoint( m_moved_idx, m_moved_point, m_gray );
224 void CLkDemoView::OnMouseMove(UINT nFlags, CvPoint point)
226 CView::OnMouseMove(nFlags, point);
227 if( nFlags & MK_LBUTTON )
229 if( m_moved_idx > 0 && m_gray.GetImage() )
231 m_moved_point = ConvertScreenToImage(point);
237 void CLkDemoView::StartTracking(CImage& frame)
239 double quality = 0.01;
240 double min_distance = 10;
241 int max_features = 300;
244 m_tracker.Initialize( frame, max_features, quality, min_distance );
248 void CLkDemoView::StopTracking()
254 void CLkDemoView::TrackFeatures(CImage& frame)
258 m_gray.Create( frame.Width(), frame.Height(), 8 ,frame.GetImage()->origin);
259 m_gray.CopyOf( frame, 0 );
260 m_tracker.PushFrame( m_gray );
265 int CLkDemoView::FindPoint( CvPoint pt )
267 int i, count = m_tracker.GetCount();
270 const PointArray& array = m_tracker.GetPoints();
272 for( i = 0; i < count; i++ )
274 CvPoint2D32f p1 = array[i];
275 double d = fabs(pt.x - p1.x) + fabs(pt.y - p1.y);
287 /*CvPoint CLkDemoView::ConvertScreenToImage( CvPoint point )
290 GetClientRect( &rect );
291 IplImage* img = m_gray.GetImage();
293 point.x = point.x * iplWidth(img)/MAX(rect.Width(),1);
294 point.y = point.y * iplHeight(img)/MAX(rect.Height(),1);
299 /*void CLkDemoView::OnLButtonDblClk(UINT nFlags, CvPoint point)
301 CView::OnLButtonDblClk(nFlags, point);
303 point = ConvertScreenToImage(point);
304 int index = FindPoint( point );
307 m_tracker.RemovePoint( index );
309 else if( m_gray.GetImage() )
311 m_tracker.AddPoint( point, m_gray );