Opencv video target tracking example tutorial (meanshift)

Source: Internet
Author: User
Tags visual studio 2010 vmin

Using camshift in opencv for Target Tracking in the video is a good choice. There are many examples in this regard, but most of the Code is incomplete, or the Code has problems and cannot be used normally. Here, after finishing many articles, I posted the correct code that can be used.

First download opencv, http://sourceforge.net/projects/opencvlibrary/

Install opencv, Which is exe and can be directly installed.

Specific installation process see reprinted a blog: http://blog.csdn.net/luopeiyuan1990/article/details/8775069

After the installation is complete, do not forget to add the include and Lib search directories in the project summary. Finally, add the dynamic link library as follows:

Use the development environment: vs2010.

Dynamic Link Library

Opencv_core245d.lib
Opencv_core245.lib
Opencv_highgui245.lib
Opencv_highgui245d.lib
Opencv_imgproc245.lib
Opencv_imgproc245d.lib
Opencv_video245.lib
Opencv_video245d.lib

If you do not install the following error message:
Error 1 error lnk2019: the external symbol that cannot be parsed "Void _ cdecl CV: destroywindow (class STD: basic_string <char, struct STD: char_traits <char>, class STD:: Allocator <char> const &)"(? Destroywindow @ CV @ yaxabv? $ Basic_string @ du? $ Char_traits @ d @ STD @ V? $ Allocator @ d @ 2 @ STD @ Z). This symbol is referenced in function _ main. E: \ Documents \ Visual Studio 2010 \ projects \ track \ main. OBJ

Error cause: Incorrect Library File Settings
Solution: Project-> properties-> connector-> input-> Add dependencies to add library files that the program depends on, this program uses opencv_core220d.lib and opencv_highgui220d.lib (we recommend that you add all the above dynamic libraries)

Other errors may occur during use, such:

Proxytrans. Ax cocould not be loaded
This error is caused by the default installation of a registered item in opencv1.0. you can install opencv1.0 at the following address:

Http://www.opencv.org.cn/download/OpenCV_1.0.exe

Another error:
Error: "cvsetmousecallback": you cannot convert parameter 2 from "void (_ cdecl *) (INT, Int, Int, INT)" to "cvmousecallback"

Cause: the function name does not comply with the following naming rules of opencv.
// Void on_mouse (INT event, int X, int y, int flags)
Void on_mouse (INT event, int X, int y, int flags, void * PARAM)

Summary routine:

Http://ishare.iask.sina.com.cn/f/36709094.html

A reference tutorial is as follows:

Http://ishare.iask.sina.com.cn/f/9105002.html

Below is the sample code used in this routine, which is used for simple target tracking: the camera in the notebook can be used for simple tracking of your face. Not very sensitive,To be improved, this routine is combined. If you have any questions, please leave a message or contact me.

 

# Include "CV. H "# include" highgui. H "# include <stdio. h> # include <ctype. h> iplimage * image = 0, * HSV = 0, * hue = 0, * mask = 0, * backproject = 0, * histimg = 0; cvhistogram * hist = 0; int backproject_mode = 0; int select_object = 0; int track_object = 0; int show_hist = 1; cvpoint origin; cvrect selection; cvrect track_window; cvbox2d track_box; // box of the region returned by tracking, cvconnectedcomp track_comp; int hdims = 48; // Specify the number of Hist entries. The higher the value, the more precise the float hranges_arr [] = {0,180}; float * hranges = hranges_arr; int Vmin = 10, Vmax = 256, Smin = 30; // void on_mouse (INT event, int X, int y, int flags) void on_mouse (INT event, int X, int y, int flags, void * PARAM) {If (! Image) return; If (image-> origin) y = image-> height-y; If (select_object) {selection. X = min (x, origin. x); selection. y = min (Y, origin. y); selection. width = selection. X + cv_iabs (X-origin. x); selection. height = selection. Y + cv_iabs (Y-origin. y); selection. X = max (selection. x, 0); selection. y = max (selection. y, 0); selection. width = min (selection. width, image-> width); selection. height = Min (selection. height, image-> height); selection. width-= selection. x; selection. height-= selection. y;} switch (event) {Case cv_event_lbuttondown: Origin = cvpoint (x, y); selection = cvrect (X, Y, 0, 0); select_object = 1; break; Case cv_event_lbuttonup: select_object = 0; If (selection. width> 0 & selection. height> 0) track_object =-1; # ifdef _ debug printf ("\ n # mouse selection area:"); printf ("\ n x = % d, Y = % d, width = % d, Height = % d ", selection. x, selection. y, selection. width, selection. height); # endif break ;}} cvscalar HSV 2rgb (float Hue) {int RGB [3], p, sector; static const int sector_data [] [3] = {0, 2, 1}, {, 0}, {, 2}, {, 1}, {, 0}, {, 2}; hue * = 0.0333333333333333333333333333333f; sector = cvfloor (Hue); P = cvround (255 * (hue-sector); P ^ = Sector & 1? 255: 0; RGB [sector_data [Sector] [0] = 255; RGB [sector_data [Sector] [1] = 0; RGB [sector_data [Sector] [2] = P; # ifdef _ debug printf ("\ n # convert HSV to RGB :"); printf ("\ n hue = % F", Hue); printf ("\ n r = % d, g = % d, B = % d", RGB [0], RGB [1], RGB [2]); # endif return cvscalar (RGB [2], RGB [1], RGB [0], 0 );} int main (INT argc, char ** argv) {cvcapture * capture = 0; iplimage * frame = 0; If (argc = 1 | (argc = 2 & strlen (argv [1]) = 1 & isdigit (argv [1] [0]) Capture = cvcapturefromcam (argc = 2? Argv [1] [0]-'0': 0); else if (argc = 2) Capture = cvcapturefromavi (argv [1]); If (! Capture) {fprintf (stderr, "cocould not initialize capturing... \ n "); Return-1;} printf (" hot keys: \ n "" \ tesc-quit the program \ n "" \ TC-stop the tracking \ n "" \ TB-switch to/from Backprojection view \ n "" \ th- show/hide object histogram \ n "" to initialize tracking, select the object with mouse \ n "); // cvnamedwindow (" histogram ", 1); cvnamedwindow (" camshiftdemo ", 1); cvsetmousecallback (" camshif Tdemo ", on_mouse, null); // cvcreatetrackbar (" Vmin "," camshiftdemo ", & Vmin, 256, 0); cvcreatetrackbar (" Vmax ", "camshiftdemo", & Vmax, 256, 0); cvcreatetrackbar ("Smin", "camshiftdemo", & Smin, 256, 0); For (;) {int I, bin_w, C; frame = cvqueryframe (capture); If (! Frame) break; If (! Image) {/* allocate all the buffers */image = cvcreateimage (cvgetsize (FRAME), 8, 3); image-> origin = frame-> origin; HSV = cvcreateimage (cvgetsize (FRAME), 8, 3); hue = cvcreateimage (cvgetsize (FRAME), 8, 1); mask = cvcreateimage (cvgetsize (FRAME), 8, 1); backproject = cvcreateimage (cvgetsize (FRAME), 8, 1); hist = cvcreatehist (1, & hdims, cv_hist_array, & hranges, 1 ); // calculate the histogram histimg = CV Createimage (cvsize (320,200), 8, 3); cvzero (histimg);} cvcopy (frame, image, 0); cvcvtcolor (image, HSV, cv_bgr2hsv ); // color space conversion BGR to HSV if (track_object) {int _ Vmin = Vmin, _ Vmax = vmax; cvinranges (HSV, cvscalar (0, Smin, min (_ Vmin, _ Vmax), 0), cvscalar (180,256, max (_ Vmin, _ Vmax), 0), mask); // obtain the two-value mask cvsplit (HSV, hue, 0, 0, 0); // extract only the hue component if (track_object <0) {float max_val = 0.f; Cvsetimageroi (hue, selection); // obtain the selected region for ROI cvsetimageroi (mask, selection); // obtain the selected region for mask cvcalchist (& hue, Hist, 0, mask ); // calculate the histogram cvgetminmaxhistvalue (Hist, 0, & max_val, 0, 0); // only find the maximum cvconvertscale (hist-> bins, hist-> bins, max_val? 255. /max_val: 0 ., 0); // zoom bin to the interval [0,255] cvresetimageroi (Hue); // remove ROI cvresetimageroi (mask); track_window = selection; track_object = 1; cvzero (histimg ); bin_w = histimg-> width/hdims; // hdims: number of entries, then bin_w is the width of the bar // draw a histogram for (I = 0; I 

 

 

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.