Using opencv for camera interaction games and source code

Source: Internet
Author: User
Tags vmin
Note: This example is from China's digital art online. It has not been debugged and is reprinted here. It is just for your reference. Source of the original is: http://www.cgblog.com.cn/e/DoPrint? Classid = 29 & id = 9742.

===

Recently, I saw someone using flash and the camera to make an interactive game. I thought it was very interesting. I used the library function of opencv and the VC model and made a similar haha. The effect is not very good. Next we will introduce how to connect a camera to a computer, after starting the program, there will be two beating yellow balls in the video window as your target (the ball moves to make a floating bubble, but the dynamic equation is not found, you can just design one.) There is also a white cross circle as your quasi-star. You have a red object that shakes in front of the camera to manipulate your on-board star. When the on-board star hits a yellow ball, the ball disappears. (This game requires the support of the dynamic Connection Library of opencv beta5. I want to place a x map named gun.bmp in the current directory. I use this card in the figure to decide what color of thing you can use for gymnastics to keep your star on the right)

Paste the source code.

// Auto. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include "cv. H"
# Include "highgui. H"
# Include "stdio. H"
# Include "ctype. H"
# Include "math. 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;

Int xn = 0;
Int YN = 0;
Int xn2 = 0;
Int yn2 = 0;

Cvpoint origin;
Cvpoint trackpoint;
Cvrect selection;
Cvrect track_window;
Cvbox2d track_box;
Cvconnectedcomp track_comp;
Int hdims = 16;
Float hranges_arr [] ={ 0,180 };
Float * hranges = hranges_arr;
Int Vmin = 90, Vmax = 256, Smin = 90;

Cvscalar HSV 2rgb (float Hue );

// Load the specified Image
Void loadtemplateimage ()
{
Iplimage * tempimage = cvloadimage ("gun.bmp", 1 );
Cvcvtcolor (tempimage, HSV, cv_bgr2hsv );
Int _ Vmin = Vmin, _ Vmax = vmax;

Cvinranges (HSV, cvscalar (0, Smin, min (_ Vmin, _ Vmax), 0 ),
Cvscalar (180,256, max (_ Vmin, _ Vmax), 0), mask );

Cvsplit (HSV, hue, 0, 0, 0 );

Selection. x = 1;
Selection. y = 1;
Selection. width = 320-1;
Selection. Height = 240-1;

Cvsetimageroi (hue, selection );
Cvsetimageroi (mask, selection );
Cvcalchist (& hue, Hist, 0, mask );

Float max_val = 0.f;

Cvgetminmaxhistvalue (Hist, 0, & max_val, 0, 0 );
Cvconvertscale (hist-> bins, hist-> bins, max_val? 255./max_val: 0., 0 );
Cvresetimageroi (Hue );
Cvresetimageroi (mask );
Track_window = selection;
Track_object = 1;

Cvzero (histimg );
Int bin_w = histimg-> width/hdims;
For (INT I = 0; I {
Int val = cvround (cvgetreal1d (hist-> bins, I) * histimg-> height/255 );
Cvscalar color = HSV 2rgb (I * 180.f/ hdims );
Cvrectangle (histimg, cvpoint (I * bin_w, histimg-> height ),
Cvpoint (I + 1) * bin_w, histimg-> height-Val ),
Color,-1, 8, 0 );
}

Cvreleaseimage (& tempimage );

}

// Mouse Event Callback Function
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;
Break;
}
}

// Color Space
Cvscalar HSV 2rgb (float Hue)
{
Int RGB [3], p, sector;
Static const int sector_data [] [3] =
{, 1}, {, 0}, {, 2}, {, 1}, {, 0}, {, 2 }};
Hue * = 0.033333333333333333333333333333333f;
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;

Return cvscalar (RGB [2], RGB [1], RGB [0], 0 );
}

// Drag Popo
Void drawcorssmark (iplimage * DST, cvpoint pt)
{

Const int cross_len = 10;
Cvpoint pt1, pt2, pt3, Pt4;
Pt1.x = pt. X;
Pt1.y = pt. Y-cross_len;
Pt2.x = pt. X;
Pt2.y = pt. Y + cross_len;
Pt3.x = pt. X-cross_len;
Pt3.y = pt. Y;
Pt4.x = pt. x + cross_len;
Pt4.y = pt. Y;

Cvcircle (DST, PT, 10, cv_rgb (255,255,255), 2, cv_aa, 0 );

Cvline (DST, pt1, pt2, cv_rgb (255,255,255), 2, cv_aa, 0 );
Cvline (DST, pt3, Pt4, cv_rgb (255,255,255), 2, cv_aa, 0 );

}

// Point movement
Void movepopo (iplimage * DST, Int & XN, Int & yn)
{

Int xn1 = xn + 1;
Int I =-1;
Int yn1 = (INT) (Xn) + (20 * sin (Xn )));
If (xn1> = (DST-> width ))
{
Xn1 = 0;
Yn1 = 0;
}/* Image Width in pixels */
If (yn1> = (DST-> height ))
{
Xn1 = 0;
Yn1 = 0;
}

Cvpoint Pt = cvpoint (xn1, yn1 );
Cvcircle (DST, PT, 4, cv_rgb (230,255, 0), 2, cv_aa, 0 );
Xn = xn1;
YN = yn1;
}

Void movepopo1 (iplimage * DST, Int & XN, Int & yn)
{

Int xn1 = xn + 2;
Int yn1 = (INT) (Xn) + (10 * sin (Xn )));

If (xn1> = (DST-> width ))
{
Xn1 = 0;
Yn1 = 0;
}/* Image Width in pixels */
If (yn1> = (DST-> height ))
{
Xn1 = 0;
Yn1 = 0;
}

Cvpoint Pt = cvpoint (xn1, yn1 );
Cvcircle (DST, PT, 4, cv_rgb (230,255, 0), 2, cv_aa, 0 );
Xn = xn1;
YN = yn1;
}

Int main (INT argc, char ** argv)
{
Cvcapture * capture = 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", 0 );
Cvnamedwindow ("interactivecamgame", 0 );
Cvsetmousecallback ("interactivecamgame", on_mouse, 0 );
// Cvcreatetrackbar ("Vmin", "camshiftdemo", & Vmin, 256, 0 );
// Cvcreatetrackbar ("Vmax", "camshiftdemo", & Vmax, 256, 0 );
// Cvcreatetrackbar ("Smin", "camshiftdemo", & Smin, 256, 0 );

For (;;)
{
Iplimage * frame = 0;
// Int I, bin_w, C;
Int 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 );
Histimg = cvcreateimage (cvsize (320,200), 8, 3 );
Cvzero (histimg );
// Cvsaveimage ("a.bmp", image );
Loadtemplateimage ();
}

Cvcopy (frame, image, 0 );
Cvsaveimage ("a.bmp", image );
Cvcvtcolor (image, HSV, cv_bgr2hsv );

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 );
Cvsplit (HSV, hue, 0, 0, 0 );

Cvcalcbackproject (& hue, backproject, hist );
Cvand (backproject, mask, backproject, 0 );
Cvcamshift (backproject, track_window,
Cvtermcriteria (cv_termcrit_eps | cv_termcrit_iter, 10, 1 ),
& Track_comp, & track_box );
Track_window = track_comp.rect;

If (backproject_mode)
Cvcvtcolor (backproject, image, cv_gray2bgr );
If (image-> origin)
Track_box.angle =-track_box.angle;

// Cvellipsebox (image, track_box, cv_rgb (255, 0), 3, cv_aa, 0 );
Drawcorssmark (image, cvpointfrom32f (track_box.center ));

Movepopo (image, xn, yn );

Movepopo1 (image, xn2, yn2 );
// Movepopo (image, image-> width, image-> height );
Trackpoint = cvpointfrom32f (track_box.center );
If (SQRT (trackpoint. x-XN) * (trackpoint. x-XN) + (trackpoint. y-yn) * (trackpoint. y-yn) <10)
{
Xn = 0;
YN = 0;
Movepopo (image, xn, yn );

}

}

If (select_object & selection. width> 0 & selection. Height> 0)
{
Cvsetimageroi (image, selection );
Cvxors (image, cvscalarall (255), image, 0 );
Cvresetimageroi (image );
}

Cvshowimage ("interactivecamgame", image );
// Cvshowimage ("histogram", histimg );

C = cvwaitkey (10 );
If (C = 27)
Break;
Switch (c)
{
Case 'B ':
Backproject_mode ^ = 1;
Break;
Case 'C ':
Track_object = 0;
Cvzero (histimg );
Break;
Case 'H ':
Show_hist ^ = 1;
If (! Show_hist)
Cvdestroywindow ("histogram ");
Else
Cvnamedwindow ("histogram", 1 );
Break;
Default:
;
}
}

Cvreleasecapture (& capture );
Cvdestroywindow ("interactivecamgame ");

Return 0;
}

# Ifdef _ EIC
Main (1, "camshiftdemo. c ");
# Endif

Is it very familiar? That's right. It's just the example of using opencv.

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.