51 #include "visp/vpConfig.h"
53 #ifdef VISP_HAVE_OPENCV
57 #include "vpKltOpencv.h"
59 void vpKltOpencv::clean()
61 if (image) cvReleaseImage(&image);
62 if (prev_image) cvReleaseImage(&prev_image);
63 if (pyramid) cvReleaseImage(&pyramid);
64 if (prev_pyramid) cvReleaseImage(&prev_pyramid);
73 countPrevFeatures = 0;
76 globalcountFeatures = 0;
79 void vpKltOpencv::cleanAll()
82 if (features) cvFree(&features);
83 if (prev_features) cvFree(&prev_features);
84 if (status) cvFree(&status);
85 if (lostDuringTrack) cvFree(&lostDuringTrack);
86 if (featuresid) cvFree(&featuresid);
87 if (prev_featuresid) cvFree(&prev_featuresid);
96 void vpKltOpencv::reset()
108 countPrevFeatures = 0;
109 globalcountFeatures = 0;
116 harris_free_parameter = 0.04;
134 OnMeasureFeature = 0;
136 initial_guess =
false;
138 features = (CvPoint2D32f*)cvAlloc((
unsigned int)maxFeatures*
sizeof(features[0]));
139 prev_features = (CvPoint2D32f*)cvAlloc((
unsigned int)maxFeatures*
sizeof(prev_features[0]));
140 status = (
char*)cvAlloc((
size_t)maxFeatures);
141 lostDuringTrack = (
bool*)cvAlloc((
size_t)maxFeatures);
142 featuresid = (
long*)cvAlloc((
unsigned int)maxFeatures*
sizeof(long));
143 prev_featuresid = (
long*)cvAlloc((
unsigned int)maxFeatures*
sizeof(long));
152 initialized = copy.initialized;
153 maxFeatures = copy.maxFeatures;
154 countFeatures = copy.countFeatures;
155 countPrevFeatures = copy.countPrevFeatures;
156 globalcountFeatures = copy.globalcountFeatures;
158 win_size = copy.win_size;
159 quality = copy.quality;
160 min_distance = copy.min_distance;
161 harris_free_parameter = copy.harris_free_parameter;
162 block_size = copy.block_size;
163 use_harris = copy.use_harris;
164 pyramid_level = copy.pyramid_level;
167 OnInitialize = copy.OnInitialize;
168 OnFeatureLost = copy.OnFeatureLost;
169 OnNewFeature = copy.OnNewFeature;
170 OnMeasureFeature = copy.OnMeasureFeature;
171 IsFeatureValid = copy.IsFeatureValid;
186 countPrevFeatures = 0;
189 globalcountFeatures = 0;
194 image = cvCreateImage(cvGetSize(copy.image), 8, 1);
196 cvCopy(copy.image, image, 0);
201 prev_image = cvCreateImage(cvGetSize(copy.prev_image), IPL_DEPTH_8U, 1);
203 cvCopy(copy.prev_image, prev_image, 0);
208 pyramid = cvCreateImage(cvGetSize(copy.pyramid), IPL_DEPTH_8U, 1);
210 cvCopy(copy.pyramid, pyramid, 0);
213 if (copy.prev_pyramid)
215 prev_pyramid = cvCreateImage(cvGetSize(copy.prev_pyramid), IPL_DEPTH_8U, 1);
217 cvCopy(copy.prev_pyramid, prev_pyramid, 0);
223 (CvPoint2D32f*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(CvPoint2D32f));
224 for (
int i = 0; i < copy.maxFeatures; i++)
225 features[i] = copy.features[i];
228 if (copy.prev_features) {
230 (CvPoint2D32f*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(CvPoint2D32f));
231 for (
int i = 0; i < copy.maxFeatures; i++)
232 prev_features[i] = copy.prev_features[i];
235 if (copy.featuresid) {
236 featuresid = (
long*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(long));
237 for (
int i = 0; i < copy.maxFeatures; i++)
238 featuresid[i] = copy.featuresid[i];
241 if (copy.prev_featuresid) {
242 prev_featuresid = (
long*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(long));
243 for (
int i = 0; i < copy.maxFeatures; i++)
244 prev_featuresid[i] = copy.prev_featuresid[i];
248 status = (
char*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(char));
249 for (
int i = 0; i < copy.maxFeatures; i++)
250 status[i] = copy.status[i];
253 if (copy.lostDuringTrack) {
254 lostDuringTrack = (
bool*)cvAlloc((
unsigned int)copy.maxFeatures*
sizeof(bool));
255 for (
int i = 0; i < copy.maxFeatures; i++)
256 lostDuringTrack[i] = copy.lostDuringTrack[i];
273 initialized = 0; maxFeatures=input;
275 if (features) cvFree(&features);
276 if (prev_features) cvFree(&prev_features);
277 if (status) cvFree(&status);
278 if (lostDuringTrack) cvFree(&lostDuringTrack);
279 if (featuresid) cvFree(&featuresid);
280 if (prev_featuresid) cvFree(&prev_featuresid);
283 features = (CvPoint2D32f*)cvAlloc((
unsigned int)maxFeatures*
sizeof(CvPoint2D32f));
284 prev_features = (CvPoint2D32f*)cvAlloc((
unsigned int)maxFeatures*
sizeof(CvPoint2D32f));
285 status = (
char*)cvAlloc((
unsigned int)maxFeatures*
sizeof(char));
286 lostDuringTrack = (
bool*)cvAlloc((
unsigned int)maxFeatures*
sizeof(bool));
287 featuresid = (
long*)cvAlloc((
unsigned int)maxFeatures*
sizeof(long));
288 prev_featuresid = (
long*)cvAlloc((
unsigned int)maxFeatures*
sizeof(long));
307 if (I->depth != IPL_DEPTH_8U || I->nChannels != 1) {
312 if (mask->depth != IPL_DEPTH_8U || I->nChannels != 1) {
318 CvSize Sizeim, SizeI;
319 SizeI = cvGetSize(I);
322 Sizeim = cvGetSize(image);
323 if(SizeI.width != Sizeim.width || SizeI.height != Sizeim.height) b_imOK =
false;
325 if(image == NULL || prev_image == NULL || pyramid==NULL || prev_pyramid ==NULL || !b_imOK){
327 image = cvCreateImage(cvGetSize(I), 8, 1);image->origin = I->origin;
328 prev_image = cvCreateImage(cvGetSize(I), IPL_DEPTH_8U, 1);
329 pyramid = cvCreateImage(cvGetSize(I), IPL_DEPTH_8U, 1);
330 prev_pyramid = cvCreateImage(cvGetSize(I), IPL_DEPTH_8U, 1);
334 countPrevFeatures = 0;
337 globalcountFeatures = 0;
346 countFeatures = maxFeatures;
347 countPrevFeatures = 0;
348 IplImage* eig = cvCreateImage(cvGetSize(image), 32, 1);
349 IplImage* temp = cvCreateImage(cvGetSize(image), 32, 1);
350 cvGoodFeaturesToTrack(image, eig, temp, features,
351 &countFeatures, quality, min_distance,
352 mask, block_size, use_harris, harris_free_parameter);
353 cvFindCornerSubPix(image, features, countFeatures, cvSize(win_size, win_size),
354 cvSize(-1,-1),cvTermCriteria(CV_TERMCRIT_ITER|
355 CV_TERMCRIT_EPS,20,0.03));
356 cvReleaseImage(&eig);
357 cvReleaseImage(&temp);
363 for (
int boucle=0; boucle<countFeatures;boucle++) {
364 featuresid[boucle] = globalcountFeatures;
365 globalcountFeatures++;
368 OnNewFeature(_tid, boucle, featuresid[boucle], features[boucle].x,
380 "KLT Not initialized")) ;
385 "Image Not initialized")) ;
388 if (I->depth != IPL_DEPTH_8U || I->nChannels != 1) {
390 "Bad Image format")) ;
395 CV_SWAP(prev_image, image, swap_temp);
396 CV_SWAP(prev_pyramid, pyramid, swap_temp);
402 countPrevFeatures = countFeatures;
403 for (
int boucle=0; boucle<countFeatures;boucle++) {
404 prev_featuresid[boucle] = featuresid[boucle];
407 CvPoint2D32f *swap_features = 0;
408 CV_SWAP(prev_features, features, swap_features);
411 if (countFeatures <= 0)
return;
413 cvCalcOpticalFlowPyrLK( prev_image, image, prev_pyramid, pyramid,
414 prev_features, features, countFeatures,
415 cvSize(win_size, win_size), pyramid_level,
416 status, 0, cvTermCriteria(CV_TERMCRIT_ITER
417 |CV_TERMCRIT_EPS,20,0.03),
421 flags |= CV_LKFLOW_PYR_A_READY;
423 flags = CV_LKFLOW_PYR_A_READY;
424 initial_guess =
false;
428 for (i = k = 0; i < countFeatures ; i++) {
430 lostDuringTrack[i] = 1;
432 OnFeatureLost(_tid, i, featuresid[i], features[i].x,
437 if (IsFeatureValid) {
438 if (!IsFeatureValid(_tid, features[i].x, features[i].y)) {
439 lostDuringTrack[i] = 1;
441 OnFeatureLost(_tid, i, featuresid[i], features[i].x, features[i].y);
445 features[k] = features[i];
446 featuresid[k] = featuresid[i];
448 if (OnMeasureFeature) OnMeasureFeature(_tid, k, featuresid[k], features[k].x, features[k].y);
450 lostDuringTrack[i] = 0;
464 vpColor color,
unsigned int thickness)
466 if ((features == 0) || (I.
bitmap==0) || (!initialized))
490 if (index >= countFeatures)
496 x = features[index].x;
497 y = features[index].y;
498 id = featuresid[index];
513 countPrevFeatures = countFeatures;
514 for (
int boucle=0; boucle<countFeatures;boucle++) {
515 prev_featuresid[boucle] = featuresid[boucle];
518 CvPoint2D32f *swap_features = NULL;
519 CV_SWAP(prev_features, *guess_pts, swap_features);
521 CV_SWAP(features, prev_features, swap_features);
523 flags |= CV_LKFLOW_INITIAL_GUESSES;
525 initial_guess =
true;
538 if (index >= countPrevFeatures)
544 x = prev_features[index].x;
545 y = prev_features[index].y;
546 id = prev_featuresid[index];
556 const float &x,
const float &y)
558 if (maxFeatures == countFeatures)
567 features[countFeatures] = f;
568 featuresid[countFeatures] = id;
574 if (index >= countFeatures)
582 for (
int i=index ; i < countFeatures; i ++) {
583 features[i] = features[i+1];
584 featuresid[i] = featuresid[i+1];
603 const int &nbFeatures,
vpColor color,
unsigned int thickness)
606 for (
int i = 0 ; i < nbFeatures ; i++)
628 const int &nbFeatures,
vpColor color,
unsigned int thickness)
631 for (
int i = 0 ; i < nbFeatures ; i++)
656 const long *featuresid_list,
const int &nbFeatures,
657 vpColor color,
unsigned int thickness)
660 for (
int i = 0 ; i < nbFeatures ; i++)
667 sprintf(
id,
"%ld", featuresid_list[i]);
690 const long *featuresid_list,
const int &nbFeatures,
691 vpColor color,
unsigned int thickness)
694 for (
int i = 0 ; i < nbFeatures ; i++)
701 sprintf(
id,
"%ld", featuresid_list[i]);
void track(const IplImage *I)
Type * bitmap
points toward the bitmap
Class to define colors available for display functionnalities.
void addFeature(const int &id, const float &x, const float &y)
void display(const vpImage< unsigned char > &I, vpColor color=vpColor::red, unsigned int thickness=1)
error that can be emited by ViSP classes.
void setInitialGuess(CvPoint2D32f **guess_pts)
static int round(const double x)
void getFeature(int index, int &id, float &x, float &y) const
void set_u(const double u)
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void set_v(const double v)
void getPrevFeature(int index, int &id, float &x, float &y) const
void initTracking(const IplImage *I, const IplImage *mask=NULL)
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV.
void suppressFeature(int index)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void setMaxFeatures(const int input)