Tracking a specified target using the Camshift algorithm

Source: Internet
Author: User
Tags abs ranges scalar

Principle

The camshift algorithm is the abbreviation for continuously Adaptive Mean Shift algorithm.

It is an improved algorithm based on Meansift. It was first put forward by Gary R.bradski and other people and applied to the tracking of human face, and achieved good results. Because it is a tracking using the probability information of color. makes it much more efficient to perform. The process of the Camshift algorithm consists of the following steps:

(1) Determining the initial target and its region;

(2) Calculating the histogram of the color (hue) component of the target;

(3) Use the histogram to calculate the inverted projection of the input image (further explanation later);

(4) The Meanshift algorithm is used to iterate the collecting cable in the reverse projection graph until it converges or reaches the maximum number of iterations. and save 0 moments.

(5) Obtain the center position of the collection form from step (4) and calculate the new form size. Take this as a reference, go to the next frame target tracking. (i.e. jump to step (2));

Code
#include "stdafx.h" #include "opencv2/video/tracking.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/ Highgui/highgui.hpp "#include <iostream> #include <ctype.h>using namespace cv;using namespace std; Mat Image;bool Backprojmode = False;bool SelectObject = false;int Trackobject = 0;bool Showhist = true; Point origin; Rect selection (0,0,50,50); static void Onmouse (int event, int x, int y, int, void*) {switch (event) {case Cv_event_lbutton Down:origin = Point (x, y); selection = Rect (x,y,0,0); SelectObject = True;break;case Cv_event_lbuttonup:selectobject = False;if (selection.width > 0 && selection.height > 0) trackobject = -1;break;} if (SelectObject) {selection.x = min (x, origin.x); selection.y = min (y, origin.y); selection.width = Std::abs (x-origin.x); Selection.height = Std::abs (Y-ORIGIN.Y);}} int main (int argc, const char** argv) {Cv::videocapture capture (0); Capture.set (cv_cap_prop_frame_width,640); Capture.set (cv_cap_prop_frame_height,480); if (!capture.iSopened ()) return-1;double rate = Capture.get (cv_cap_prop_fps);//Gets the framerate int delay = 1000/rate;//calculates the inter-frame delay; Mat frame,image, Hsv,mask,hue;namedwindow ("Test", cv_window_autosize); Setmousecallback ("Test", onmouse,0); while (1) {capture> >frame;if (Trackobject = =-1) {//After setting the detected object, start tracking Frame.copyto (image); Cv::cvtcolor (IMAGE,HSV,CV_RGB2HSV); CV:: InRange (Hsv,scalar (0,130,50), Scalar (180,256,256), mask);//Remove the low-saturation point vector<cv::mat> v;cv::split (hsv,v);// The HSV three channels separate hue = V[1];cv::mat ROI = hue (selection);//Select the area of interest cv::mat Maskroi = mask (selection); Cv::matnd Hist;int histsize[1];histsize[0]= 16;float hranges[2];hranges[0] = 0;hranges[1] = 180;const float *ranges[1];ranges[0] = hranges; Cv::calchist (&roi,1,0,maskroi,hist,1,histsize,ranges);//histogram of the area of interest. There are too many cv::normalize (Hist,hist,0,180,cv_minmax);//normalization of histograms; Cv::mat Backpro;cv::calcbackproject (&hue,1,0, Hist,backpro,ranges); The inverse projection of the H-channel is placed in the Backpro backpro &= mask;cv::rotatedrect trackbox = Cv::camshift (Backpro,selection,termcriteria (CV _termcrit_eps | cv_termcrit_iter,10,1));//use mean second algorithm to find Rect;cv::ellipse (Frame,trackbox,cv::scalar (0,0,255), 2,CV_AA);} Cv::imshow ("Test", frame); if (Waitkey () >= 0) break;} Capture.release (); return 0;}
Effect
get video with a webcam
Direct Read VideoSummary:The effect is not too good. It may be that there is no preprocessing or the parameter setting is not good. people who have just started learning. Look forward to the aunt know!


Tracking a specified target using the Camshift algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.