Move the sources to trunk
[opencv] / filters / Tracker3dFilter / trackers / BlobTracker / BlobTrkOut.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) 2002, 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 //   * Redistributions of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistributions 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 // ////////////////////////////////////////////////////////////////////////////
43 //  BlobTrkOut.cpp 
44 //
45 //    This file contains member functions of the BlobTracker class
46 //    that deal with output data.
47 //            FillDestinationData
48 //            GetTrackedObjects
49 //            DrawCross
50 // 
51 // ////////////////////////////////////////////////////////////////////////////
52
53 #include "cvstreams.h"
54 #include "BlobTracker.h"
55 #include "BlobTrkObject.h"
56
57 #undef min
58 #undef max
59 template <class T> static inline T min(T a, T b) { return a < b ? a : b; }
60 template <class T> static inline T max(T a, T b) { return a > b ? a : b; }
61
62 // ////////////////////////////////////////////////////////////////////////////
63 // BlobTracker::FillDestinationData()
64 //
65 // Fills in the destination image based on estimated object location and state.
66 // Markers are displayed to indicate object position and search windows and 
67 // other information of importance.
68 //
69 // Input: 
70 //      image.
71 //
72 // ////////////////////////////////////////////////////////////////////////////
73 void BlobTracker::FillDestinationData(IplImage *image)
74 {
75     if (m_output_options != 0)
76     {
77         for (BlobTrackerObjectList::const_iterator object = m_objects->begin(); object != m_objects->end(); object++)
78         {
79 #if 0
80             if (m_output_options & IBlobTracker::OUTPUT_COLOR_PIXELS)
81             {
82                 CvRect rect = object->GetRect();
83
84                 const unsigned char *pSrc = (const unsigned char *)m_image->imageData + rect.y * image->width;
85                 unsigned char *pDst = (unsigned char *)image->imageData + rect.y * image->width;
86                 for (int i = rect.y; i < rect.y + rect.height; i++)
87                 {
88                     for (int j = rect.x; j < rect.x + rect.width; j++)
89                     {
90                         if (pSrc[j] == color)
91                             pDst[j] = ((i ^ j) & 1) ? 255 : 0;
92                     }
93                     pSrc += image->width;
94                     pDst += image->widthStep;
95                 }
96             }
97 #endif
98
99             if (m_output_options & IBlobTracker::OUTPUT_BOUNDING_BOX)
100             {
101                 CvRect rect = object->GetRect();                                                
102                 cvRectangle(image, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), 0xffffff, 1);
103             }
104
105             if (m_output_options & IBlobTracker::OUTPUT_CROSSHAIRS)
106             {
107                 // Draw a cross at the estimated Object location
108                 double color;
109                 switch (object->GetId())
110                 {
111                 case 0:
112                         color = 0x0000ff;
113                         break;
114                 case 1:
115                         color = 0x00ff00;
116                         break;
117                 case 2:
118                         color = 0xff0000;
119                         break;
120                 default:
121                         color = 0xffffff;
122                         break;
123                 }
124                 DrawCross(image, object->GetCenter(), color);
125             }
126         }
127     }
128 }
129
130 STDMETHODIMP BlobTracker::GetTrackedObjects(ITracker::TrackingInfo &tracked_objects)
131 {
132     tracked_objects.clear();
133
134     for (BlobTrackerObjectList::const_iterator object = m_objects->begin(); object != m_objects->end(); object++)
135         tracked_objects.push_back(cv3dTracker2dTrackedObject(object->GetId(), object->GetCenter()));
136
137     return NOERROR;
138 }
139
140 // ////////////////////////////////////////////////////////////////////////////
141 // BlobTracker::DrawCross(IplImage *image, CvPoint point)
142 //
143 // Draws a cross on the image at the point indicated
144 //
145 // Inputs: 
146 //        image: image in which cross is being placed
147 //        point: coordinates of center of cross
148 //
149 // ////////////////////////////////////////////////////////////////////////////
150
151 void BlobTracker::DrawCross(IplImage *image, CvPoint point, double color)
152 {
153     const int SIZE_OF_CROSS = 20;
154
155     int Left = max((int)point.x - SIZE_OF_CROSS, 0);
156     int Right = min((int)point.x + SIZE_OF_CROSS, (int)image->width - 1);
157     int Top = max((int)point.y - SIZE_OF_CROSS, 0);
158     int Bottom = min((int)point.y + SIZE_OF_CROSS, (int)image->height - 1);
159
160     cvLine(image, cvPoint(point.x, Top), cvPoint(point.x, Bottom), color);
161     cvLine(image, cvPoint(Left, point.y), cvPoint(Right, point.y), color);
162 }