1.0.x-learning opencv and MFC mixed programming-video motion detection

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/nuptboyzhb/3961668

New Content in version 1.0.x

Video motion detection

Ø create menu items, learning opencv --> opencvr entry --> video motion detection

The menu items are set as follows:

Create a Class Wizard

ØEdit code

Voidccvmfcview: onmytestsport ()

{

// Todo: add your command handler codehere

// Alt + F8 sort out the code

Cvcapture * capture;

Capture = cvcreatecameracapture (0); // enable the camera

If (capture = NULL)

{

MessageBox ("no camera is detected. Check whether the camera is correctly installed ");

}

Else

{

Cstring windowname1 = "camera ";

Iplimage * frame1 = 0; // the first frame.

Iplimage * frame2 = 0; // second Frame

Iplimage * frame_sub = 0; // frames after subtraction

Iplimage * pimg8u = NULL; // grayscale chart

//

Assert (capture! = NULL); // if it is not enabled, it is interrupted.

Cvnamedwindow (windowname1, cv_window_autosize );

Frame1 = cvqueryframe (capture );

Frame_sub = cvcloneimage (frame1 );

While (1)

{

Frame2 = cvqueryframe (capture );

If (! Frame2)

{

Break;

}

Cvabsdiff (frame1, frame2, frame_sub );

Frame1 = cvcloneimage (frame2 );

Cvmovewindow (FIG, 150,200 );

Cvshowimage (windowname1, frame_sub );

// Create a bitmap

Pimg8u = cvcreateimage (cvgetsize (frame_sub), ipl_depth_8u, 1 );

Cvcvtcolor (frame_sub, pimg8u, cv_bgr2gray); // color grayscale

// -------------- Calculate the histogram -----------------------------

Iplimage * SRC;

Iplimage * histimg = 0;

Cvhistogram * hist = 0;

Int hdims = 256; // Number of Hist partitions, the higher the accuracy

Src = cvcloneimage (pimg8u );

Float hranges_arr [] ={ 0,255 };

Float * hranges = hranges_arr;

Int bin_w;

Float max_val;

Int I;

Cvnamedwindow ("histogram", 0 );

Hist = cvcreatehist (1, & hdims, cv_hist_array, & hranges, 1); // calculates the Histogram

Histimg = cvcreateimage (cvsize (320,200), 8, 3 );

Cvzero (histimg );

Cvcalchist (& SRC, Hist, 0, 0); // calculate the Histogram

Cvgetminmaxhistvalue (Hist, 0, & max_val, 0, 0); // you can specify the maximum value only.

Cvconvertscale (hist-> bins, hist-> bins,

Max_val? 255./max_val: 0., 0); // scale the bin to the interval [0,255]

Cvzero (histimg );

Bin_w = histimg-> width/hdims; // hdims: number of entries, then bin_w is the width

// Draw a Histogram

For (I = 0; I

{

Double val = (cvgetreal1d (hist-> bins, I) * histimg-> height/255 );

Cvscalar color = cv_rgb (255,255, 0); // (HSV 2rgb (I * 180.f/ hdims );

Cvrectangle (histimg, cvpoint (I * bin_w, histimg-> height ),

Cvpoint (I + 1) * bin_w, (INT) (histimg-> height-Val )),

Color, 1, 8, 0 );

}

Cvmovewindow ("histogram", 550,200 );

Cvshowimage ("histogram", histimg );

//------------------------------------------------------

Char c = cvwaitkey (1 );

If (C = 27)

{

Break;

}

}

Cvreleasecapture (& capture );

Cvdestroywindow (windowname1 );

Cvdestroywindow ("histogram ");

}

}

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.