Ask Your Question

Revision history [back]

Just translate python to c++ (and you should test this code

// This file is part of my own effort
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution 
int main(int argc, char *argv[])

{

    int hsize[] = {16,16,16};
    float hranges[] = { 0,180 };
    const float* phranges[] = {hranges,hranges,hranges};
    VideoCapture cap(0);
    Mat frame;
    cap>>frame;
    Rect r(250, 90, 300, 125);  
    Rect track_window = r;
    Mat roi = frame(r);
    Mat hsv_roi,hsv,dst;
    cvtColor(roi,hsv_roi,COLOR_BGR2HSV);
    Mat maskroi;
    inRange(hsv_roi, Scalar(0., 60., 32.), Scalar(180., 255., 255.),maskroi);
    Mat roi_hist;
    int ch[] = { 0, 1,2 };
    calcHist(&roi, 1, ch, maskroi, roi_hist, 1, hsize, phranges);
    normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
    int k=0;
    while (true)
    {
        cap>>frame;
        if (!frame.empty())
        {
            cvtColor(frame, hsv,COLOR_BGR2HSV);
            calcBackProject(&hsv, 1, ch, roi_hist, dst, phranges);
            meanShift(dst, track_window, TermCriteria(TermCriteria::EPS | TermCriteria::COUNT, 10, 1));

            rectangle(frame, track_window, Scalar(255,128,128), 2);
            imshow("img2", frame);
            k = waitKey(60);
            if (k == 27)
                break;
            else
                imwrite("a.jpg", frame);

        }

    }
 destroyAllWindows();
 cap.release();
}

Just translate python to c++ (and you should test this codecode) hsv range values are wrong...

// This file is part of my own effort
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution 
int main(int argc, char *argv[])

{

    int hsize[] = {16,16,16};
    float hranges[] = { 0,180 };
    const float* phranges[] = {hranges,hranges,hranges};
    VideoCapture cap(0);
    Mat frame;
    cap>>frame;
    Rect r(250, 90, 300, 125);  
    Rect track_window = r;
    Mat roi = frame(r);
    Mat hsv_roi,hsv,dst;
    cvtColor(roi,hsv_roi,COLOR_BGR2HSV);
    Mat maskroi;
    inRange(hsv_roi, Scalar(0., 60., 32.), Scalar(180., 255., 255.),maskroi);
    Mat roi_hist;
    int ch[] = { 0, 1,2 };
    calcHist(&roi, 1, ch, maskroi, roi_hist, 1, hsize, phranges);
    normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
    int k=0;
    while (true)
    {
        cap>>frame;
        if (!frame.empty())
        {
            cvtColor(frame, hsv,COLOR_BGR2HSV);
            calcBackProject(&hsv, 1, ch, roi_hist, dst, phranges);
            meanShift(dst, track_window, TermCriteria(TermCriteria::EPS | TermCriteria::COUNT, 10, 1));

            rectangle(frame, track_window, Scalar(255,128,128), 2);
            imshow("img2", frame);
            k = waitKey(60);
            if (k == 27)
                break;
            else
                imwrite("a.jpg", frame);

        }

    }
 destroyAllWindows();
 cap.release();
}

Just translate python to c++ (and you should test this code) hsv range values are wrong...

// This file is part of my own effort
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution 
int main(int argc, char *argv[])

{

    int hsize[] = {16,16,16};
{ 16,16,16 };
    float hranges[] = { 0,180 };
    const float* phranges[] = {hranges,hranges,hranges};
{ hranges,hranges,hranges };
    VideoCapture cap(0);
    Mat frame;
    cap>>frame;
    cap >> frame;

    imshow("Define Rect r(250, 90, 300, 125);  
HSV", frame);
    Rect r=selectROI(frame);
    Rect track_window = r;
    Mat roi = frame(r);
    Mat hsv_roi,hsv,dst;
    cvtColor(roi,hsv_roi,COLOR_BGR2HSV);
hsv_roi, hsv, dst;
    cvtColor(roi, hsv_roi, COLOR_BGR2HSV);
    Mat maskroi;
    inRange(hsv_roi, Scalar(0., 60., 32.), Scalar(180., 255., 255.),maskroi);
255.), maskroi);
    Mat roi_hist;
    int ch[] = { 0, 1,2 };
    calcHist(&roi, calcHist(&hsv_roi, 1, ch, maskroi, roi_hist, 1, hsize, phranges);
    normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
    int k=0;
k = 0;
    while (true)
    {
        cap>>frame;
cap >> frame;
        if (!frame.empty())
        {
            cvtColor(frame, hsv,COLOR_BGR2HSV);
hsv, COLOR_BGR2HSV);
            calcBackProject(&hsv, 1, ch, roi_hist, dst, phranges);
            meanShift(dst, track_window, TermCriteria(TermCriteria::EPS | TermCriteria::COUNT, 10, 1));

            rectangle(frame, track_window, Scalar(255,128,128), Scalar(255, 128, 128), 2);
            imshow("img2", frame);
            k = waitKey(60);
            if (k == 27)
                break;
            else
                imwrite("a.jpg", frame);

        }

    }
  destroyAllWindows();
  cap.release();
}