Reinit goodFeaturesToTrack and LukasKanade optical flow.

asked 2015-04-29 06:44:36 -0600

215 gravatar image

updated 2015-04-29 08:05:40 -0600

Hi guys.. I am at the moment using goodFeaturesToTrack + Lukas kanade optical flow to track objects. When i lose the object (defined by bool) I have to reinitiate the goodFeaturesToTrack and LukasKanade optical flow. This might seem like a stupid question, and has indeed been for me for couple of weeks, but i cannot seems to perform the reinitiating stage. I clear the points i have been tracking.. Use goodFeatureToTrack on the new location of the mask, Lukas kanade to track the points.. I mark the the good features with a circle on the image, so in know that i started all over, but the seem to be spread as before which make me wonder if it actually is doing what it is supposed to do?

there is a lot of code.. i don't it scare you away..

            else // performing reinit
            {
                Rect redetected;
                redetected = box(gray);
                cout << "i did it" << endl;
                cout <<redetected.width  <<" , " <<redetected.height << endl;
                if(redetected.width<face.width)
                {
                    Rect NewBOx;
                    NewBOx.x  = backup_center.x-face.width/2;
                    NewBOx.y  = backup_center.y-face.height/2/2;
                    NewBOx.width = face.width;
                    NewBOx.height = face.height;
                    face = NewBOx;
                }
                points[1].clear();
                point_final.clear();
                rectangle(image,face,Scalar(255,255,0));
                imshow("test",image);
                mask(face).setTo(Scalar(255));
                vector<Point2f> tmp;
                goodFeaturesToTrack(gray, tmp, MAX_COUNT, 0.01, 10, mask, 3, 0, 0.04);
                cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
                points[1].push_back(tmp[0]);
                calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize, 3, termcrit, OPTFLOW_LK_GET_MIN_EIGENVALS, qualitylevel);
                calcOpticalFlowPyrLK(gray, prevGray, points[1], point_final, status2, err2, winSize, 3, termcrit, OPTFLOW_LK_GET_MIN_EIGENVALS, qualitylevel);
            }

I tried debugging, and can see that GoodFeaturesToTrack only adds one circle, which isn't nessesarly within the mask, how do i make it add more than one.. I doesn't make sense that it doesn't add more since i have deleted all previous points. ..

It's reinit stage only that seem to be troublesome.. Thanks in advance.

edit retag flag offensive close merge delete

Comments

Problem solved..

   points[1].push_back(tmp[0]); // adds only one point

I so much hate my life!!!

I still have problem making removing the old ones.. they still seems to appear, The circles don't get removed.

215 gravatar image215 ( 2015-04-29 08:22:00 -0600 )edit

may be you can try tmp.clear() and after goodFeaturesToTrack(gray, tmp, MAX_COUNT, 0.01, 10, mask, 3, 0, 0.04); to track new points in your new image. There is a good sample in opencv/samples/lkdemo.cpp

LBerger gravatar imageLBerger ( 2015-04-29 10:35:26 -0600 )edit

I have seen that sample, it is kinda based on that.. the sample aren't capable of restarting which what I am trying to do..

215 gravatar image215 ( 2015-04-29 11:43:27 -0600 )edit

In this sample you can reinit if you press keyboard c. Variable needToInit becomes true and vector point are clear and function goodFeaturesToTrack is called again. I don't know if it is this sort of reint you want.

LBerger gravatar imageLBerger ( 2015-04-29 11:50:42 -0600 )edit

Actually yeah.. But it has to applied to a mask. but that should be a problem.. I look into it.. thanks :)

215 gravatar image215 ( 2015-04-29 12:10:24 -0600 )edit

I am actually doing the same thing as them.. weird though my circles don't get removed..

215 gravatar image215 ( 2015-04-29 12:12:09 -0600 )edit