7 #include "stickletrack.h"
10 #define SEPCOST_CIRCUM 1
11 #define SEPCOST_SHORTAXIS 2
12 #define SEPCOST_LONGAXIS 3
13 #define SEPCOST_ANGLE 4
14 #define SEPCOST_DIST 5
21 vector<Point> *points;
30 static int enablecontours=0;
40 vector<tagcost> tagcosts;
43 bool sort_tags (tagcost i, tagcost j) {
44 return i.tagcost < j.tagcost;
47 bool sort_blobbs (blobbcost i, blobbcost j) {
48 return i.tagcosts[0].tagcost < j.tagcosts[0].tagcost;
51 Mat& tracking_getFrame() {
55 void tracking_showFrame() {
56 imshow("contours_picture", mContours);
59 bool tracking_isEnabled () {
60 return enablecontours;
63 void tracking_init() {
64 namedWindow("contours", CV_WINDOW_KEEPRATIO);
65 namedWindow("contours_picture", CV_WINDOW_KEEPRATIO);
67 createTrackbar("Enable", "contours", &enablecontours, 1, NULL, 0);
68 createTrackbar("Manyfish", "contours", &Prefs.manyfish, 10, NULL, 0);
69 createTrackbar("min area", "contours", &Prefs.contours_minarea, 100, &trackbarCallbackUpdateNormPrefs, 0);
70 createTrackbar("max area", "contours", &Prefs.contours_maxarea, 100, &trackbarCallbackUpdateNormPrefs, 0);
71 createTrackbar("min circum", "contours", &Prefs.contours_mincircum, 100, &trackbarCallbackUpdateNormPrefs, 0);
72 createTrackbar("max circum", "contours", &Prefs.contours_maxcircum, 100, &trackbarCallbackUpdateNormPrefs, 0);
73 createTrackbar("min shortaxis", "contours", &Prefs.contours_minshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0);
74 createTrackbar("max shortaxis", "contours", &Prefs.contours_maxshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0);
75 createTrackbar("min longaxis", "contours", &Prefs.contours_minlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0);
76 createTrackbar("max longaxis", "contours", &Prefs.contours_maxlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0);
77 createTrackbar("max speed", "contours", &Prefs.contours_maxspeed, 100, &trackbarCallbackUpdateNormPrefs, 0);
78 createTrackbar("max rotation speed", "contours", &Prefs.contours_maxrot, 100, &trackbarCallbackUpdateNormPrefs, 0);
80 mContours = Mat::zeros(Props.height, Props.width, CV_8UC3);
83 double kitvalue (double min, double max) {
84 return min + pow( cos(frametime*10), 2)*(max-min);
87 void findFishBlobbs(vector<fishblobb>& fishblobbs, vector< vector<Point> >& contours, vector<Vec4i>& hierarchy) {
88 for (int icontour=0; icontour >= 0; icontour = hierarchy[icontour][0]) {
91 if ( contours[icontour].size() < 5 )
94 newblobb.circum = contours[icontour].size();
95 if ( newblobb.circum < normalPrefs.contours_mincircum || newblobb.circum > normalPrefs.contours_maxcircum )
98 newblobb.area = contourArea(contours[icontour]);
99 if ( newblobb.area < normalPrefs.contours_minarea || newblobb.area > normalPrefs.contours_maxarea )
102 RotatedRect minEllipse = fitEllipse(contours[icontour]);
103 newblobb.longaxis = minEllipse.size.height;
104 newblobb.shortaxis = minEllipse.size.width;
105 if ( newblobb.shortaxis < normalPrefs.contours_minshortaxis || newblobb.shortaxis > normalPrefs.contours_maxshortaxis
106 || newblobb.longaxis < normalPrefs.contours_minlongaxis || newblobb.longaxis > normalPrefs.contours_maxlongaxis )
109 newblobb.x = minEllipse.center.x;
110 newblobb.y = minEllipse.center.y;
113 Mat mFish = Mat::zeros(Props.height, Props.width, CV_8U);
116 Point2f recHead, recTail;
118 newblobb.identified = false;
119 newblobb.points = &contours[icontour];
121 newblobb.contourid = icontour;
123 /* find head/tail sensitive angle
124 maybe the method could be improved */
125 drawContours( mFish, contours, icontour, Scalar(255, 255, 255), CV_FILLED );
126 fishMom = moments(mFish);
128 massCenter = Point2f( fishMom.m10/fishMom.m00 , fishMom.m01/fishMom.m00 );
129 minRect = minAreaRect(contours[ icontour ]);
131 recHead = Point2f( minRect.center.x + newblobb.longaxis*cos(minEllipse.angle * M_PI / 180 + M_PI/2),
132 minRect.center.y + newblobb.longaxis*sin(minEllipse.angle * M_PI / 180 + M_PI/2) );
133 recTail = Point2f( minRect.center.x + newblobb.longaxis*cos(minEllipse.angle * M_PI / 180 - M_PI/2),
134 minRect.center.y + newblobb.longaxis*sin(minEllipse.angle * M_PI / 180 - M_PI/2) );
136 if ( pow( recHead.x - massCenter.x, 2 ) + pow( recHead.y - massCenter.y, 2 ) > pow( recTail.x - massCenter.x, 2 ) + pow( recTail.y - massCenter.y, 2 ) ) {
137 newblobb.angle = minEllipse.angle + 180;
139 if (newblobb.angle > 360)
140 newblobb.angle = newblobb.angle - 360;
143 newblobb.angle = minEllipse.angle;
146 newblobb.tail = Point2f( minEllipse.center.x + newblobb.longaxis*cos(newblobb.angle * M_PI / 180 - M_PI/2),
147 minEllipse.center.y + newblobb.longaxis*sin(newblobb.angle * M_PI / 180 - M_PI/2) );
148 newblobb.head = Point2f( minEllipse.center.x + newblobb.longaxis*cos(newblobb.angle * M_PI / 180 + M_PI/2),
149 minEllipse.center.y + newblobb.longaxis*sin(newblobb.angle * M_PI / 180 + M_PI/2) );
151 fishblobbs.push_back(newblobb);
155 void tagCare(vector<tag>& tags) {
156 while ( tags.size() > Prefs.manyfish )
159 while ( tags.size() < Prefs.manyfish ) {
164 tmp.lastseen = frametime;
166 switch ( tags.size() ) {
168 tmp.color = Scalar( 0, 255, 255 );
171 tmp.color = Scalar( 255, 0, 255 );
174 tmp.color = Scalar( 255, 255, 0 );
177 tmp.color = Scalar( 255, 0, 0 );
180 tmp.color = Scalar( 0, 255, 0 );
183 tmp.color = Scalar( 0, 0, 255 );
186 tmp.color = Scalar( rand()%255, rand()%255, rand()%255 );
199 void matchBlobbsNTags(vector<tag>& tags, vector<fishblobb>& fishblobbs) {
200 vector<blobbcost> blobbcosts;
202 for (int iblobb = 0; iblobb < fishblobbs.size(); iblobb++) {
203 double sepcost[tags.size()][6];
207 blobbCost.blobbid = iblobb;
209 for (int imax = 0; imax < 6; imax++)
212 for (int itag = 0; itag < tags.size(); itag++) {
213 double anglediff = fabs (fishblobbs[iblobb].angle - tags[itag].angle);
215 if ( anglediff > 180 )
216 anglediff = 360 - anglediff;
218 sepcost[itag][SEPCOST_ANGLE] = anglediff;
220 sepcost[itag][SEPCOST_AREA] = fabs ( fishblobbs[iblobb].area - tags[itag].area );
221 sepcost[itag][SEPCOST_CIRCUM] = fabs ( fishblobbs[iblobb].circum - tags[itag].circum );
222 sepcost[itag][SEPCOST_SHORTAXIS] = fabs ( fishblobbs[iblobb].shortaxis - tags[itag].shortaxis );
223 sepcost[itag][SEPCOST_LONGAXIS] = fabs ( fishblobbs[iblobb].longaxis - tags[itag].longaxis );
224 sepcost[itag][SEPCOST_DIST] = sqrt( pow(tags[itag].x - fishblobbs[iblobb].x, 2) + pow(tags[itag].y - fishblobbs[iblobb].y, 2) );
226 for (int imax = 0; imax < 6; imax++)
227 if ( maxcost[imax] < sepcost[itag][imax] )
228 maxcost[imax] = sepcost[itag][imax];
231 for (int itag = 0; itag < tags.size(); itag++) {
234 newTagCost.tagid = itag;
235 newTagCost.tagcost = 0;
237 if ( sepcost[itag][SEPCOST_DIST] > ( frametime - tags[itag].lastseen )*normalPrefs.contours_maxspeed
238 || sepcost[itag][SEPCOST_ANGLE] > ( frametime - tags[itag].lastseen )*normalPrefs.contours_maxrot )
241 for (int isep = 0; isep < 6; isep++)
242 newTagCost.tagcost += sepcost[itag][isep] / maxcost[isep];
244 blobbCost.tagcosts.push_back( newTagCost );
247 if ( blobbCost.tagcosts.size() > 0 ) {
248 sort( blobbCost.tagcosts.begin(), blobbCost.tagcosts.end(), sort_tags );
249 blobbcosts.push_back( blobbCost );
253 for (int itag = 0; itag < tags.size() && blobbcosts.size() > 0; itag++) {
254 sort( blobbcosts.begin(), blobbcosts.end(), sort_blobbs );
256 int tagnow = blobbcosts[0].tagcosts[0].tagid;
258 tags[tagnow].virgin = false;
259 tags[tagnow].x = fishblobbs[ blobbcosts[0].blobbid ].x;
260 tags[tagnow].y = fishblobbs[ blobbcosts[0].blobbid ].y;
261 tags[tagnow].shortaxis = fishblobbs[ blobbcosts[0].blobbid ].shortaxis;
262 tags[tagnow].longaxis = fishblobbs[ blobbcosts[0].blobbid ].longaxis;
263 tags[tagnow].angle = fishblobbs[ blobbcosts[0].blobbid ].angle;
264 tags[tagnow].lastseen = frametime;
265 tags[tagnow].area = fishblobbs[ blobbcosts[0].blobbid ].area;
266 tags[tagnow].circum = fishblobbs[ blobbcosts[0].blobbid ].circum;
267 tags[tagnow].head = fishblobbs[ blobbcosts[0].blobbid ].head;
268 tags[tagnow].tail = fishblobbs[ blobbcosts[0].blobbid ].tail;
270 blobbcosts.erase( blobbcosts.begin() );
272 for (int iblobb = 0; iblobb < blobbcosts.size(); iblobb++) {
274 for (int itag = 0; itag < blobbcosts[iblobb].tagcosts.size(); itag++)
275 if ( blobbcosts[iblobb].tagcosts[itag].tagid == tagnow ) {
276 blobbcosts[iblobb].tagcosts.erase( blobbcosts[iblobb].tagcosts.begin() + itag );
280 if ( blobbcosts[iblobb].tagcosts.size() == 0 ) {
281 blobbcosts.erase( blobbcosts.begin() + iblobb );
288 void drawTags (const vector<tag>& tags) {
292 for (int itag = 0; itag < tags.size(); itag++) {
293 double recside = sqrt( kitvalue( normalPrefs.contours_minarea, normalPrefs.contours_maxarea) );
294 rRect = RotatedRect( Point(tags[itag].x, tags[itag].y), Size2f(recside, recside), tags[itag].angle );
295 rRect.points(vertices);
296 for (int i = 0; i < 4; i++)
297 line(mContours, vertices[i], vertices[(i+1)%4], Scalar(255,255,255), Props.diagonal/1000.0);
299 ellipse(mContours, Point(tags[itag].x, tags[itag].y),
300 Size( kitvalue( normalPrefs.contours_minshortaxis, normalPrefs.contours_maxshortaxis ),
301 kitvalue( normalPrefs.contours_minlongaxis, normalPrefs.contours_maxlongaxis ) ),
302 tags[itag].angle, 0, 360, Scalar(255,255,255), Props.diagonal/1000.0, 8);
304 circle(mContours, Point(tags[itag].x, tags[itag].y), kitvalue( normalPrefs.contours_mincircum, normalPrefs.contours_maxcircum )/(2*M_PI), Scalar(255,255,255),
305 Props.diagonal/1000.0, 8);
307 if ( ! tags[itag].virgin ) {
308 float searchradius = ( frametime - tags[itag].lastseen )*normalPrefs.contours_maxspeed;
309 float searchangle = ( frametime - tags[itag].lastseen )*normalPrefs.contours_maxrot;
310 ellipse(mContours, Point(tags[itag].x, tags[itag].y), Size(searchradius, searchradius), tags[itag].angle, 90-searchangle, 90+searchangle,
311 tags[itag].color, Props.diagonal/1000.0, 8);
313 rRect = RotatedRect( Point(tags[itag].x, tags[itag].y), Size2f(sqrt(tags[itag].area), sqrt(tags[itag].area)), tags[itag].angle );
314 rRect.points(vertices);
315 for (int i = 0; i < 4; i++)
316 line(mContours, vertices[i], vertices[(i+1)%4], tags[itag].color, 2);
318 ellipse(mContours, Point(tags[itag].x, tags[itag].y),
319 Size( tags[itag].shortaxis, tags[itag].longaxis ), tags[itag].angle, 0, 360, tags[itag].color, 2*Props.diagonal/1000.0, 8);
321 circle(mContours, Point(tags[itag].x, tags[itag].y), tags[itag].circum / (2*M_PI), tags[itag].color, 2*Props.diagonal/1000.0, 8);
323 circle(mContours, tags[itag].head, 5*Props.diagonal/1000.0, tags[itag].color, -1, 8);
328 void tracking_locateTags (vector<tag>& tags, Mat combinedmask_contour) {
329 mContours = Mat::zeros(Props.height, Props.width, CV_8UC3);
331 if (enablecontours) {
332 vector< vector<Point> > contours;
333 vector<Vec4i> hierarchy;
334 vector <fishblobb> fishblobbs;
336 findContours( combinedmask_contour, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
338 if ( contours.size() > 0 ) {
340 for(int idx = 0 ; idx >= 0; idx = hierarchy[idx][0] )
341 drawContours(mContours, contours, idx, Scalar(255, 255, 255), CV_FILLED, 8, hierarchy);
343 findFishBlobbs(fishblobbs, contours, hierarchy);
345 for ( int iblobb = 0; iblobb < fishblobbs.size(); iblobb++) {
346 Scalar color( rand()%255, rand()%255, rand()%255 );
347 drawContours( mContours, contours, fishblobbs[iblobb].contourid, color, CV_FILLED, 8, hierarchy );
351 matchBlobbsNTags(tags, fishblobbs);