Update the changelog
[opencv] / apps / cvlkdemo / demoview.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 // LkDemoView.cpp : implementation of the CLkDemoView class
43
44 #if _MSC_VER >= 1200
45 #pragma warning( disable: 4100 4663 4189 4101 4018 4710 )
46 #endif
47
48 #include <math.h>
49 #include "demoview.h"
50
51
52 /////////////////////////////////////////////////////////////////////////////
53 // CLkDemoView construction/destruction
54
55 CLkDemoView::CLkDemoView()
56 {
57     //m_canvas.Create( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 24 );
58     m_track = false;
59     m_trackinit = false;
60     m_night_mode = false;
61     m_moved_idx = -1;
62     m_started = false;
63 }
64
65 CLkDemoView::~CLkDemoView()
66 {
67 }
68
69 void CLkDemoView::SetSize (int w,int h)
70 {
71     m_w = w;
72     m_h = h;
73 }
74
75 CvPoint CLkDemoView::ConvertScreenToImage( CvPoint point )
76 {
77     IplImage* img = m_gray.GetImage();
78     if( img )
79     {
80         CvSize size = cvGetSize(img);
81     
82         point.x *= size.width/m_w;
83         point.y *= size.height/m_h;
84     }
85     else
86         point.x = point.y = 0;
87     
88     return point;
89 }
90
91 /////////////////////////////////////////////////////////////////////////////
92 // CLkDemoView drawing
93
94 /*void CLkDemoView::OnDraw(CDC* pDC)
95 {
96     CLkDemoDoc* pDoc = GetDocument();
97     ASSERT_VALID(pDoc);
98
99     IplImage* img = m_camera.GetFrame().GetImage();
100     if( img )
101     {
102         HDC dstDC = ::GetDC( m_hWnd );
103         HDC srcDC = m_canvas.GetDC();
104
105         TrackFeatures();
106
107         CRect sr, dr;
108         GetClientRect( &dr );
109         IplImage* dst_img = m_canvas.GetImage();
110         IplROI roi = RectToROI( dr );
111         dst_img->roi = &roi;
112         
113         if( !m_night_mode )
114         {
115             //iplResize( img, dst_img, roi.width, img->width, roi.height, img->height, 
116             //           IPL_INTER_LINEAR );
117             double a[2][3] =
118             {
119                 {(double)roi.width/img->width, 0, 0},
120                 {0, (double)roi.height/img->height, 0}
121             };
122                             
123             iplWarpAffine( img, dst_img, a, IPL_INTER_LINEAR );
124         }
125         else
126         {
127             iplSet( dst_img, 0 );
128         }
129
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);
134
135         for( i = 0; i < count; i++ )
136         {
137             CvPoint pt;
138             int color;
139             if( i != m_moved_idx )
140             {
141                 pt = cvPoint( cvRound(array[i].x*kx), cvRound(array[i].y*ky));
142                 color = RGB(0,255,0);
143             }
144             else
145             {
146                 pt = cvPoint( cvRound(m_moved_point.x*kx),
147                               cvRound(m_moved_point.y*ky));
148                 color = RGB(255,0,0);
149             }
150             cvCircle( dst_img, pt, 3, color, CV_FILLED );
151         }
152         
153         dst_img->roi = 0;
154
155         if( m_track )
156         {
157             m_track = count > 0;
158         }
159
160         BitBlt( dstDC, 0, 0, dr.Width(), dr.Height(), m_canvas.GetDC(), 0, 0, SRCCOPY );
161
162         ::ReleaseDC( m_hWnd, dstDC );
163     }
164     else
165     {
166         CRect r;
167         pDC->GetClipBox(&r);
168         pDC->FillSolidRect( r.left, r.top, r.Width(), r.Height(), RGB(0,0,0));
169     }
170 }*/
171
172 /////////////////////////////////////////////////////////////////////////////
173 // CLkDemoView message handlers
174
175 /*void CLkDemoView::OnLButtonDown(UINT nFlags, CvPoint point) 
176 {
177     CView::OnLButtonDown(nFlags, point);
178     {
179         point = ConvertScreenToImage(point);
180         int index = FindPoint(point);
181         if( index > 0 )
182         {
183             m_moved_idx = index;
184             m_moved_point = point;
185             CheckUpdate();
186         }
187         else if( m_gray.GetImage() )
188         {
189             m_tracker.AddPoint( point, m_gray );
190         }
191     }
192 }
193
194
195 void CLkDemoView::OnRButtonDown(UINT nFlags, CvPoint point) 
196 {
197     CView::OnRButtonDown(nFlags, point);
198     {
199         point = ConvertScreenToImage(point);
200         int index = FindPoint(point);
201         if( index > 0 )
202         {
203             m_tracker.RemovePoint( index );
204             CheckUpdate();
205         }
206     }
207 }
208
209
210 void CLkDemoView::OnLButtonUp(UINT nFlags, CvPoint point) 
211 {
212     CView::OnLButtonUp(nFlags, point);
213     {
214         if( m_moved_idx > 0 && m_gray.GetImage() )
215         {
216             m_tracker.MovePoint( m_moved_idx, m_moved_point, m_gray );
217             m_moved_idx = -1;
218             CheckUpdate();
219         }
220     }
221 }
222
223
224 void CLkDemoView::OnMouseMove(UINT nFlags, CvPoint point) 
225 {
226     CView::OnMouseMove(nFlags, point);
227     if( nFlags & MK_LBUTTON )
228     {
229         if( m_moved_idx > 0 && m_gray.GetImage() )
230         {
231             m_moved_point = ConvertScreenToImage(point);
232             CheckUpdate();
233         }
234     }
235 }*/
236
237 void  CLkDemoView::StartTracking(CImage& frame)
238 {
239     double quality = 0.01;
240     double min_distance = 10;
241     int max_features = 300;
242     
243     m_track = true;
244     m_tracker.Initialize( frame, max_features, quality, min_distance );
245     
246 }
247
248 void  CLkDemoView::StopTracking()
249 {
250     m_track = false;
251     
252 }
253
254 void  CLkDemoView::TrackFeatures(CImage& frame)
255 {
256     if( m_track )
257     {
258         m_gray.Create( frame.Width(), frame.Height(), 8 ,frame.GetImage()->origin);
259         m_gray.CopyOf( frame, 0 );
260         m_tracker.PushFrame( m_gray );
261     }
262 }
263
264
265 int  CLkDemoView::FindPoint( CvPoint pt )
266 {
267     int i, count = m_tracker.GetCount();
268     double min_dist = 6;
269     int min_idx = -1;
270     const PointArray& array = m_tracker.GetPoints();
271
272     for( i = 0; i < count; i++ )
273     {
274         CvPoint2D32f p1 = array[i];
275         double d = fabs(pt.x - p1.x) + fabs(pt.y - p1.y); 
276         if( d < min_dist )
277         {
278             min_dist = d;
279             min_idx = i;
280         }
281     }
282
283     return min_idx;
284 }
285
286
287 /*CvPoint CLkDemoView::ConvertScreenToImage( CvPoint point )
288 {
289     CRect rect;
290     GetClientRect( &rect );
291     IplImage* img = m_gray.GetImage();
292
293     point.x = point.x * iplWidth(img)/MAX(rect.Width(),1);
294     point.y = point.y * iplHeight(img)/MAX(rect.Height(),1);
295
296     return point;
297 }*/
298
299 /*void CLkDemoView::OnLButtonDblClk(UINT nFlags, CvPoint point) 
300 {
301     CView::OnLButtonDblClk(nFlags, point);
302
303     point = ConvertScreenToImage(point);
304     int index = FindPoint( point );
305     if( index >= 0 )
306     {
307         m_tracker.RemovePoint( index );
308     }
309     else if( m_gray.GetImage() )
310     {
311         m_tracker.AddPoint( point, m_gray );
312     }
313     CheckUpdate();
314 }*/