// Pedetriandetect. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdafx. H "<br/> # include <cv. h> <br/> # include <pighgui. h> <br/> # include "cxcore. H "<br/> # include <stdio. h> </P> <p> int otsuthreshold (iplimage * frame); </P> <p> int selectvideo = 1; <br/> # define grayscale 256 </P> <p> int main (INT argc, char ** argv) <br/>{< br/> // declare iplimage pointer <br/> iplimage * pframe = NULL; <br/> iplimage * pfrimg = NULL; <br/> iplimage * pbkimg = NULL; <br/> iplimage * pi Mg = NULL; </P> <p> cvmat * pframemat = NULL; <br/> cvmat * pfrmat = NULL; <br/> cvmat * pbkmat = NULL; </P> <p> cvmemstorage * storage = NULL; <br/> cvseq * contour = 0; <br/> int mode = cv_retr_external; </P> <p> cvcapture * pcapture = NULL; </P> <p> int nfrmnum = 0; </P> <p> // create a window <br/> cvnamedwindow ("video", 1); <br/> cvnamedwindow ("background", 1 ); <br/> cvnamedwindow ("foreground", 1); <br/> cvnamedwindow ("Contour", 1); </P> <p> // arrange the window in sequence <br/> cvmovewindow ("video", 30, 0); <br/> cvmovewindow ("background ", 382, 0); <br/> cvmovewindow ("foreground", 734, 0); <br/> cvmovewindow ("Contour", 720,288 ); </P> <p> // enable the camera <br/> If (selectvideo = 0) <br/> If (! (Pcapture = cvcapturefromcam (-1) <br/>{< br/> fprintf (stderr, "can not open camera. /n "); <br/> return-2; <br/>}</P> <p> // open the video file <br/> If (selectvideo = 1) <br/> If (! (Pcapture = cvcapturefromfile ("D: // infrared // new // 20151114-191839.mpg") <br/>{< br/> fprintf (stderr, "can not open video file % s/n", argv [1]); <br/> return-2; <br/>}</P> <p> // read the video frame by frame <br/> while (pframe = cvqueryframe (pcapture )) <br/>{< br/> nfrmnum ++; </P> <p> // if it is the first frame, you need to apply for memory, <br/> If (nfrmnum = 1) <br/> {<br/> pbkimg = cvcreateimage (cvsize (pframe-> width, pframe-> height ), ipl_depth_8u, 1); <br/> pfrimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 ); <br/> pimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 ); </P> <p> pbkmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1); <br/> pfrmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1); <br/> pframemat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1); <br/> storage = cvcreatemstorage (0 ); </P> <p> // convert to a single-channel image for reprocessing <br/> cvcvtcolor (pframe, pbkimg, cv_bgr2gray); <br/> cvcvtcolor (pframe, pfrimg, cv_bgr2gray); </P> <p> cvconvert (pfrimg, pframemat); <br/> cvconvert (pfrimg, pfrmat); <br/> cvconvert (pfrimg, pbkmat ); <br/>}< br/> else <br/> {<br/> cvcvtcolor (pframe, pfrimg, cv_bgr2gray); <br/> cvconvert (pfrimg, pframemat ); <br/> // Gaussian filtering first to smooth the image <br/> // cvsmooth (pframemat, pframemat, cv_gaussian, 3, 0, 0 ); </P> <p> // subtract the current frame from the background image <br/> cvabsdiff (pframemat, pbkmat, pfrmat); <br/> cvdilate (pfrmat, pfrmat, 0, 5); <br/> cvgetimage (pfrmat, pimg); <br/> int thredhold = otsuthreshold (pimg)-30; <br/> printf ("Otsu: % d/N ", thredhold); <br/> // binarization foreground diagram <br/> cvthreshold (pfrmat, pfrimg, 70,255.0, cv_thresh_binary ); <br/> cvfindcontours (pfrimg, storage, & contour, sizeof (cvcontour), <br/> mode, cv_chain_approx_simple, cvpoint (0, 0); <br/> cvrect rect; </P> <p> double imgarea = pfrimg-> width * pfrimg-> height; <br/> double contarea = 0.0; <br/> float contlenth = 0; </P> <p> cvpoint pt1, pt2; <br/> cvpoint pt3, Pt4; </P> <p> char x_char = cvwaitkey (10 ); </P> <p> static float hx_create; <br/> static float hy_create; </P> <p> static float lx_create; <br/> static float ly_create; </P> <p> If (x_char = 'A') lx_create-= 0.01; <br/> If (x_char = 'D') lx_create + = 0.01; <br/> If (x_char = 'W') hy_create-= 0.01; <br/> If (x_char = 's') hy_create + = 0.01; </P> <p> If (x_char = 'R') ly_create + = 0.01; <br/> If (x_char = 'T') ly_create-= 0.01; <br/> If (x_char = 'F') hx_create + = 0.01; <br/> If (x_char = 'G') hx_create-= 0.01; </P> <p> pt3.x = pframe-> width * (0.15 + Lx_create-Hx_create); <br/> pt4.x = pframe-> width * (0.85 + lx_create + hx_create ); <br/> pt3.y = pframe-> height * (0.5 + Hy_create-Ly_create); <br/> pt4.y = pframe-> height * (0.85 + hy_create + ly_create ); // */</P> <p> cvrectangle (pframe, pt3, Pt4, cv_rgb (255,), 0 ); </P> <p> for (; contour = contour-> h_next) <br/>{< br/> cvrect rect; <br/> rect = cvboundingrect (contour, 0); <br/> pt1.x = rect. x; <br/> pt1.y = rect. y; <br/> pt2.x = rect. width + rect. x; <br/> pt2.y = rect. height + rect. y; <br/> contarea = FABS (cv1_area (contour, cv_whole_seq); <br/> contlenth = cvarclength (contour, cv_whole_seq,-1 ); <br/> printf ("contarea: % F/N", contarea); <br/> printf ("contlength: % F/N", contlenth ); <br/> If (contarea> = 30 & contlenth> = 20 & rect. width> 2 & rect. width <50 & (rect. height-rect. width)> 0) <br/> {<br/> // cvdrawcontours (pframe, contour, cv_rgb (255,255, 0), cv_rgb (0,255, 0 ), <br/> // 1, 1, cv_aa, cvpoint (0, 0); <br/> cvrectangle (pframe, pt1, pt2, cv_rgb (255, 0, 0 ), 1, 8, 0); </P> <p >}< br/>}</P> <p> // cvsmooth (pfrimg, pfrimg, cv_median, 3, 0, 0); // cv_gaussian <br/> // perform morphological filtering to remove noise <br/> // cverode (pfrimg, pfrimg, 0, 1 ); <br/> // cvdilate (pfrimg, pfrimg, 0, 2); </P> <p> // update the background <br/> cvrunningavg (pframemat, pbkmat, 0.0005, 0); <br/> // convert the background to the image format to display <br/> cvconvert (pbkmat, pbkimg ); </P> <p> // display the image <br/> cvshowimage ("video", pframe); <br/> cvshowimage ("background", pbkimg ); <br/> cvshowimage ("foreground", pfrimg); </P> <p> // If a key event exists, then jump out of the loop <br/> // This wait time also provides the cvshowimage function with time to complete display <br/> // The wait time can be adjusted according to the CPU speed <br/> char c = cvwaitkey (30 ); <br/> If (C = 27) break; </P> <p >}</P> <p> // destruction window <br/> cvdestroywindow ("video "); <br/> cvdestroywindow ("background"); <br/> cvdestroywindow ("foreground "); </P> <p> // release images and matrices <br/> cvreleaseimage (& pfrimg); <br/> cvreleaseimage (& pbkimg ); </P> <p> cvreleasemat (& pframemat); <br/> cvreleasemat (& pfrmat); <br/> cvreleasemat (& pbkmat ); </P> <p> cvreleasecapture (& pcapture); </P> <p> return 0; <br/>}</P> <p> int otsuthreshold (iplimage * frame) <br/>{< br/> int width = frame-> width; <br/> int Height = frame-> height; <br/> int pixelcount [grayscale]; <br/> float pixelpro [grayscale]; <br/> int I, j, pixelsum = width * height, Threshold = 0; <br/> uchar * Data = (uchar *) frame-> imagedata; </P> <p> for (I = 0; I <grayscale; I ++) <br/> {<br/> pixelcount [I] = 0; <br/> pixelpro [I] = 0; <br/>}</P> <p> // calculate the number of pixels in the entire image in the gray scale. <br/> for (I = 0; I <peight; I ++) <br/>{< br/> for (j = 0; j <width; j ++) <br/> {<br/> pixelcount [(INT) data [I * width + J] ++; <br/>}</P> <p> // calculate the ratio of each pixel to the entire image. <br/> for (I = 0; I <grayscale; I ++) <br/>{< br/> pixelpro [I] = (float) pixelcount [I]/pixelsum; <br/>}</P> <p> // grayscale traversal [0,255] <br/> float w0, W1, u0tmp, u1tmp, U0, u1, U, <br/> deltatmp, deltamax = 0; <br/> for (I = 0; I <grayscale; I ++) <br/> {<br/> W0 = W1 = u0tmp = u1tmp = U0 = U1 = u = deltatmp = 0; <br/> for (j = 0; j <grayscale; j ++) <br/>{< br/> If (j <= I) // background <br/>{< br/> w0 + = pixelpro [J]; <br/> u0tmp + = J * pixelpro [J]; <br/>}< br/> else // foreground <br/> {<br/> W1 + = pixelpro [J]; <br/> u1tmp + = J * pixelpro [J]; <br/>}< br/> U0 = u0tmp/W0; <br/> U1 = u1tmp/W1; <br/> U = u0tmp + u1tmp; <br/> deltatmp = <br/> W0 * POW (U0-U ), 2) + W1 * POW (U1-U), 2); <br/> If (deltatmp> deltamax) <br/>{< br/> deltamax = deltatmp; <br/> Threshold = I; <br/>}< br/> return threshold; <br/>}< br/>