Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Reinit goodFeaturesToTrack and LukasKanade optical flow.

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
    {
        image = cv_ptr->image;
        flip(image, image, 1);
        cvtColor(image,gray,COLOR_BGR2GRAY);
        equalizeHist(gray,gray);
        circle(cv_ptr->image, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
        static uint64_t c;
        Mat mask(image.size(),CV_8UC1,Scalar(0));
        mask(face).setTo(Scalar(255));
        flip(mask,mask,1);
        if (c++ == 0)
        {
            cout << "automatic initialization" << endl;
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, mask, 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            cout << "points[1].size(): "<< points[1].size()<< endl;
            reinit = false;
            cout << "i am here" << endl;

        }


        if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            vector<uchar> status2;
            vector<float> err2;
            if(prevGray.empty())
            {
                gray.copyTo(prevGray);
            }
            //Lukas kanade optilcal flow + Forward - backward error
            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);
            Point2f center = Mean(point_final);
            Point2f center2 = Mean(points[1]);
            if(std_dev(point_final) &&    std_dev(points[1])) //tracking mode
            {
                cout << "Alowable deviation" << endl;
                goodFeatures.push_back(center);
                goodFeatures.push_back(center2);
                sendCommands = true;
                ROI,mask = gray(face);
                imwrite("/home/kratnarajah/Desktop/template_matching_images/ROI.jpg",mask);
                if(goodFeatures.empty())
                {
                    backup_center = center;
                    backup_center.y =+ 40;
                }
                else
                {
                    backup_center = Mean(goodFeatures);
                    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;

                }
                cout << "face.tl(): " << face.tl() << endl;
                cout << "backup_center: " <<backup_center << endl;
                if(std_dev_determine(point_final)>120 ||std_dev_determine(point_final)>120)
                {
                    frames++;
                    cout << "Issues may occur (frame++) => taking precaution by removing outliers" << endl;

                }
                else
                {
                    frames = 0;
                    cout << "frames reset" << endl;

                }
                rectangle(image,face,Scalar(255,255,0));

            }
            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);
            }
            for( int i = 0; i < points[1].size(); i++ )
            {
                if( status[i] && status2[i] ) // Forward - backward Error estimation => Good values are only taken.
                {
                    if(std_dev_determine(point_final) < 80)//std_dev_determine(points[1]))
                    {
                        //cout << "limit is now: " << std_dev_determine(point_final) << endl;
                        if(euclideanDistance(Mean(point_final) - point_final[i]) < 130)//std_dev_determine(point_final))
                        {
                            goodFeatures.push_back(point_final[i]);
                        }
                        else
                        {
                            //cout << "removed because distance is above!!!!!!!!!!!!!!!!!!!!!!!!: " <<  std_dev_determine(point_final)*(2*frames) <<endl;
                            circle(image,point_final[i],4, Scalar(0,0,255),-1,10,0);
                        }
                    }
                    else
                    {
                        //cout << "limit is now: " << std_dev_determine(points[1]) << endl;
                        if(euclideanDistance(Mean(points[1]) - points[1][i]) < 130)//std_dev_determine(points[1])*(2*frames))
                        {
                            goodFeatures.push_back(points[1][i]);
                        }
                        else
                        {
                            //cout << "removed because distance is above: " <<  std_dev_determine(points[1])*(2*frames) <<endl;
                            circle(image,points[1][i],4, Scalar(0,0,255),-1,10,0);
                        }
                    }
                }

                if(goodFeatures.empty())
                {
                    cout << "emppty !!!!!!!!!!!!!" << endl;
                    frames =- 30;
                    cout << " No features, widen the std.dev " << endl;
                }
                else if(goodFeatures.size() > 50)
                {
                    backup_center = center;
                }

            }
            cout << "GoodFeatures.size(): "<< goodFeatures.size() << endl;
            for(int j = 0; j<500; j++)
            {
                circle(image,goodFeatures[0],9, Scalar(0,0,255),-1,12,0);
                circle(image,goodFeatures[1],4, Scalar(0,255,0),-1,8,0);
                circle(image,Mean(goodFeatures),10,Scalar(255,0,255),-1,12,0);
                circle(image,punkt2,10,Scalar(0,255,255),-1,12,0);
                circle(image,goodFeatures[j],4, Scalar(255,0,0),-1,8,0);

            }
            displacement = make_pair(Mean(goodFeatures).x-center_of_frame.x,Mean(goodFeatures).y-center_of_frame.y);
            line(image, center , center_of_frame, CV_RGB(0,200,0),1,8);

        }


        if( goodFeatures.size() < 20 )
        {
            cout << "Adding extra because goodFeature count getting low" << endl;
            vector<Point2f> tmp;
            tmp.push_back(point);
            Rect templ = face;
            templ+= Size(30,30);
            Mat maskINC(image.size(),CV_8UC1,Scalar(0));
            maskINC(face).setTo(Scalar(255));
            goodFeaturesToTrack(gray, tmp, MAX_COUNT, 0.01, 10, maskINC, 3, 0, 0.04);
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            goodFeatures.push_back(tmp[0]);
        }
        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
        goodFeatures.clear();
        if(sendCommands)
        {
                //doing something..
        }


}

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

Reinit goodFeaturesToTrack and LukasKanade optical flow.

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
          else // performing reinit
            {
        image         Rect redetected;
                redetected = cv_ptr->image;
        flip(image, image, 1);
        cvtColor(image,gray,COLOR_BGR2GRAY);
        equalizeHist(gray,gray);
        circle(cv_ptr->image, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
        static uint64_t c;
        Mat mask(image.size(),CV_8UC1,Scalar(0));
        mask(face).setTo(Scalar(255));
        flip(mask,mask,1);
        if (c++ == 0)
        {
box(gray);
                cout << "automatic initialization" "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, points[1], tmp, MAX_COUNT, 0.01, 10, mask, 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1),     cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            cout << "points[1].size(): "<< points[1].size()<< endl;
            reinit = false;
            cout << "i am here" << endl;

        }


        if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            vector<uchar> status2;
            vector<float> err2;
            if(prevGray.empty())
            {
                gray.copyTo(prevGray);
            }
            //Lukas kanade optilcal flow + Forward - backward error
    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);
            Point2f center = Mean(point_final);
            Point2f center2 = Mean(points[1]);
            if(std_dev(point_final) &&    std_dev(points[1])) //tracking mode
            {
                cout << "Alowable deviation" << endl;
                goodFeatures.push_back(center);
                goodFeatures.push_back(center2);
                sendCommands = true;
                ROI,mask = gray(face);
                imwrite("/home/kratnarajah/Desktop/template_matching_images/ROI.jpg",mask);
                if(goodFeatures.empty())
                {
                    backup_center = center;
                    backup_center.y =+ 40;
                }
                else
                {
                    backup_center = Mean(goodFeatures);
                    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;

                }
                cout << "face.tl(): " << face.tl() << endl;
                cout << "backup_center: " <<backup_center << endl;
                if(std_dev_determine(point_final)>120 ||std_dev_determine(point_final)>120)
                {
                    frames++;
                    cout << "Issues may occur (frame++) => taking precaution by removing outliers" << endl;

                }
                else
                {
                    frames = 0;
                    cout << "frames reset" << endl;

                }
                rectangle(image,face,Scalar(255,255,0));

            }
            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);
            }
            for( int i = 0; i < points[1].size(); i++ )
            {
                if( status[i] && status2[i] ) // Forward - backward Error estimation => Good values are only taken.
                {
                    if(std_dev_determine(point_final) < 80)//std_dev_determine(points[1]))
                    {
                        //cout << "limit is now: " << std_dev_determine(point_final) << endl;
                        if(euclideanDistance(Mean(point_final) - point_final[i]) < 130)//std_dev_determine(point_final))
                        {
                            goodFeatures.push_back(point_final[i]);
                        }
                        else
                        {
                            //cout << "removed because distance is above!!!!!!!!!!!!!!!!!!!!!!!!: " <<  std_dev_determine(point_final)*(2*frames) <<endl;
                            circle(image,point_final[i],4, Scalar(0,0,255),-1,10,0);
                        }
                    }
                    else
                    {
                        //cout << "limit is now: " << std_dev_determine(points[1]) << endl;
                        if(euclideanDistance(Mean(points[1]) - points[1][i]) < 130)//std_dev_determine(points[1])*(2*frames))
                        {
                            goodFeatures.push_back(points[1][i]);
                        }
                        else
                        {
                            //cout << "removed because distance is above: " <<  std_dev_determine(points[1])*(2*frames) <<endl;
                            circle(image,points[1][i],4, Scalar(0,0,255),-1,10,0);
                        }
                    }
                }

                if(goodFeatures.empty())
                {
                    cout << "emppty !!!!!!!!!!!!!" << endl;
                    frames =- 30;
                    cout << " No features, widen the std.dev " << endl;
                }
                else if(goodFeatures.size() > 50)
                {
                    backup_center = center;
                }

            }
            cout << "GoodFeatures.size(): "<< goodFeatures.size() << endl;
            for(int j = 0; j<500; j++)
            {
                circle(image,goodFeatures[0],9, Scalar(0,0,255),-1,12,0);
                circle(image,goodFeatures[1],4, Scalar(0,255,0),-1,8,0);
                circle(image,Mean(goodFeatures),10,Scalar(255,0,255),-1,12,0);
                circle(image,punkt2,10,Scalar(0,255,255),-1,12,0);
                circle(image,goodFeatures[j],4, Scalar(255,0,0),-1,8,0);

            }
            displacement = make_pair(Mean(goodFeatures).x-center_of_frame.x,Mean(goodFeatures).y-center_of_frame.y);
            line(image, center , center_of_frame, CV_RGB(0,200,0),1,8);

        }


        if( goodFeatures.size() < 20 )
        {
            cout << "Adding extra because goodFeature count getting low" << endl;
            vector<Point2f> tmp;
            tmp.push_back(point);
            Rect templ = face;
            templ+= Size(30,30);
            Mat maskINC(image.size(),CV_8UC1,Scalar(0));
            maskINC(face).setTo(Scalar(255));
            goodFeaturesToTrack(gray, tmp, MAX_COUNT, 0.01, 10, maskINC, 3, 0, 0.04);
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            goodFeatures.push_back(tmp[0]);
        }
        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
        goodFeatures.clear();
        if(sendCommands)
        {
                //doing something..
        }


}

I tried debugging, and can see that GoodFeaturesToTrack only adds one circle, which isn't nessesarly within the mask..

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

Reinit goodFeaturesToTrack and LukasKanade optical flow.

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..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.