Ask Your Question
0

meanshift sample code in C++

asked 2017-10-01 12:55:28 -0500

lamiastella gravatar image

In OpenCV tutorial code I only see the code for meanshift in Python. Can you please have the same code somewhere in C++? It will be very useful for the community.

https://github.com/opencv/opencv/tree...

import numpy as np
import cv2
cap = cv2.VideoCapture('slow.flv')
# take first frame of the video
ret,frame = cap.read()
# setup initial location of window
r,h,c,w = 250,90,400,125  # simply hardcoded the values
track_window = (c,r,w,h)
# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while(1):
    ret ,frame = cap.read()
    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
        # apply meanshift to get the new location
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)
        # Draw it on image
        x,y,w,h = track_window
        img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
        cv2.imshow('img2',img2)
        k = cv2.waitKey(60) & 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k)+".jpg",img2)
    else:
        break
cv2.destroyAllWindows()
cap.release()

http://docs.opencv.org/master/db/df8/...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-10-01 14:39:13 -0500

LBerger gravatar image

updated 2018-08-18 04:06:42 -0500

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 };
    float hranges[] = { 0,180 };
    const float* phranges[] = { hranges,hranges,hranges };
    VideoCapture cap(0);
    Mat frame;
    cap >> frame;

    imshow("Define Rect 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);
    Mat maskroi;
    inRange(hsv_roi, Scalar(0., 60., 32.), Scalar(180., 255., 255.), maskroi);
    Mat roi_hist;
    int ch[] = { 0, 1,2 };
    calcHist(&hsv_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();
}
edit flag offensive delete link more

Comments

Hi, Bro. I wonder why the first parameter in the calcHist is roi rather than hsv_roi?

HD0824 gravatar imageHD0824 ( 2018-08-18 01:51:08 -0500 )edit

^^ assume a typo.

berak gravatar imageberak ( 2018-08-18 01:56:35 -0500 )edit

but it doesn't work if i use hsv_roi...

HD0824 gravatar imageHD0824 ( 2018-08-18 01:58:36 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-01 12:55:28 -0500

Seen: 1,993 times

Last updated: Aug 18 '18