Tracking with CamShift

asked 2016-06-14 15:34:09 -0500

marcob88 gravatar image

Hi, I have a problem with CamShift Tracking, At each iteration the region grows, reaching quickly the image size, and thus losing the person who is tracking. Where is the problem? This is my code

int main()
{
    string Directory = "C:\\Users\\Marco\\Desktop\\Scenario_1"; //directory dei file
    string StartFile = "gray-%06d.jpg";
    VideoCapture cap(Directory+"\\"+StartFile);
    int frameRef = 122;
    int countFrame = 1;
    Rect first(316, 94, 179, 380);
    Mat frame; Mat hsv;
    Mat mask, hue, roi_hist;
    float hranges[] = { 0, 180 };
    float sranges[] = { 0, 256 };
    const float* phranges = hranges;
    int hsize = 16;
    int hbins = 30, sbins = 32;
    //int histSize[] = { hbins, sbins };
    int histSize[] = { 180 };
    const float* ranges[] = { hranges };
    int channels[] = {1};
    TermCriteria term_crit(TermCriteria::EPS | TermCriteria::COUNT, 10, 1);
    Mat sec;
    Mat dest;
    bool isFirst = false;
    for (;;)
    {
        cap >> frame;

        if (frame.empty()) break;

        if (countFrame == frameRef)
        { 
            Mat firstFrame=frame(first);
            cvtColor(firstFrame, hsv, COLOR_BGR2HSV);
            inRange(hsv, Scalar(0, 60., 32.),Scalar(180., 255., 255.), mask);
            calcHist(&hsv, 1, channels, mask, roi_hist, 1, histSize, &phranges);
            //calcHist(&hsv, 1, channels, mask, roi_hist,2, histSize,ranges,true,false);
            //normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
            isFirst = true;
        }
        else if (isFirst)
        {
            cvtColor(frame, hsv, COLOR_BGR2HSV);
            calcBackProject(&hsv,1, channels, roi_hist,dest, &phranges);

            //calcBackProject(hsv, channels, roi_hist, dest, &phranges, 1.0);
            RotatedRect r=CamShift(dest, first, term_crit);


            //ellipse(frame, r, Scalar(0, 255, 0),3,LINE_AA);
            rectangle(frame, first, Scalar(0, 255, 0));
            namedWindow("ORIGINAL", WINDOW_AUTOSIZE);
            imshow("ORIGINAL", frame);

        }

        waitKey(2);
        countFrame++;
    }

}
edit retag flag offensive close merge delete