MA.cpp
上传用户:shopgift
上传日期:2022-07-27
资源大小:3k
文件大小:8k
- #include <iostream>
- #include <stdlib.h>
-
- // OpenCV includes.
- #include <cv.h>
- #include <highgui.h>
-
- using namespace std;
-
- int main()
- {
- //Create a new window.
- cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
-
- //Create a new movie capture object.
- CvCapture* inputMovie;
-
- //Assign the movie to capture.
- inputMovie = cvCaptureFromAVI("SAM_0830.AVI");
- double width = cvGetCaptureProperty( inputMovie, CV_CAP_PROP_FRAME_WIDTH );
- //Size of the image.
- // CvSize imgSize;
- // imgSize.width = 320;
- // imgSize.height = 240;
-
- //Images to use in the program.
- IplImage* colourImage;
- colourImage = cvQueryFrame(inputMovie);
- IplImage* greyImage = cvCreateImage( cvSize(colourImage->width, colourImage->height), IPL_DEPTH_8U, 1);
- IplImage* movingAverage = cvCreateImage( cvSize(colourImage->width, colourImage->height), IPL_DEPTH_32F, 3);
- IplImage* difference;
- IplImage* temp;
- IplImage* motionHistory = cvCreateImage( cvSize(colourImage->width, colourImage->height), IPL_DEPTH_8U, 3);
-
- //Rectangle for bounding box of a objects.
- CvRect bndRect = cvRect(0,0,0,0);
-
- //Points for the edges of the rectangle.
- CvPoint pt1, pt2;
-
- //Create a font object.
- CvFont font;
-
- //Create video to output to.
- CvVideoWriter* outputMovie = cvCreateVideoWriter("hw3_1(b).avi",
- CV_FOURCC('D', 'I', 'V', 'X'), 29.97, cvSize(320, 240));
-
- //Capture the movie frame by frame.
- int prevX = 0;
- int numCar = 0;
- int numBus = 0;
- int numPed = 0;
-
- //Buffer to save the number of vehicles when converting the integer
- //to a string.
- char Scores[60];
-
- //The center x of the rectangle.
- int avgX = 0;
-
- //Indicates whether this is the first time in the loop of frames.
- bool first = true;
-
- //Indicates the contour for left direction
- int Left = 0;
- // for the right.
- int Right = width;
-
- //Keep processing frames...
- while(true)
- {
- //Get a frame from the input video.
- colourImage = cvQueryFrame(inputMovie);
-
- //If there are no more frames, jump out of the for.
- if( !colourImage )
- {
- break;
- }
-
- //If this is the first time, initialize the images.
- if(first)
- {
- difference = cvCloneImage(colourImage);
- temp = cvCloneImage(colourImage);
- cvConvertScale(colourImage, movingAverage, 1.0, 0.0);
-
- first = false;
- }
- //else, make a running average of the motion.
- else
- {
- cvRunningAvg(colourImage, movingAverage, 0.020, NULL);
- cvNamedWindow("Run Avg",1);
- cvShowImage( "Run Avg", greyImage );
- }
-
- //Convert the scale of the moving average.
- cvConvertScale(movingAverage, temp, 1.0, 0.0);
- cvNamedWindow("Scale",1);
- cvShowImage( "Scale", greyImage );
- //Minus the current frame from the moving average.
- cvAbsDiff(colourImage,temp,difference);
- cvNamedWindow("Difference",1);
- cvShowImage( "Difference", greyImage );
- //Convert the image to grayscale.
- cvCvtColor(difference, greyImage, CV_RGB2GRAY);
-
- //Convert the image to black and white.
- cvThreshold(greyImage, greyImage, 97, 255, CV_THRESH_BINARY);
- cvNamedWindow("Thresh",1);
- cvShowImage( "Thresh", greyImage );
- cvDilate(greyImage, greyImage, 0, 18);
- cvNamedWindow("Dilate",1);
- cvShowImage( "Dilate", greyImage );
- cvErode(greyImage, greyImage, 0, 10);
- cvNamedWindow("Motion",1);
- cvShowImage( "Motion", greyImage );
- //Find the contours of the moving images in the frame.
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* contour = 0;
- cvFindContours( greyImage, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
-
- //Process each moving contour in the current frame...
- for( ; contour != 0; contour = contour->h_next )
- {
- //Get a bounding box around the moving object.
- bndRect = cvBoundingRect(contour, 0);
-
- pt1.x = bndRect.x;
- pt1.y = bndRect.y;
- pt2.x = bndRect.x + bndRect.width;
- pt2.y = bndRect.y + bndRect.height;
- printf("============================n");
- printf("%d, %dn", pt1.x, pt2.x);
- printf("%d, %dn", pt1.y, pt2.y);
- printf("%dn",bndRect.width);
-
- //Get an average X position of the moving contour.
- avgX = (pt1.x + pt2.x) / 2;
- printf("avgX----%dn", avgX);
-
- //If the contour is within the edges of the building...
- if(avgX > 90 && avgX < 250)
- {
- printf("Left --- %dn", Left);
- //If the the previous contour was within 2 of the left boundary...
- if(Left >= 87 && Left <= 90)
- {
- //If the current X position is greater than the previous...
- if(avgX > prevX)
- {
-
- //Increase the number of objects.
- if( bndRect.width > 180 && pt1.y < 70 ){
- numBus++;
- printf("num-%d, width-%dn", numBus,bndRect.width );}
- else if( bndRect.width > 120 && bndRect.width < 200){
- numCar++;
- printf("num-%d, width-%dn", numCar, bndRect.width); }
- else if( bndRect.width < 100 ){
- numPed ++;
- printf("num-%d,width-%dn", numPed, bndRect.width);}
- //Reset the closest object to the left indicator.
- Left = 0;
- }
- }
- //else if the previous contour was within 2 of the right boundary...
- else if(Right >= 250 && Right <= 252)
- {
- printf("Right --- %dn", Right);
- //If the current X position is less than the previous...
- if(avgX < prevX)
- {
-
- //Increase the number of objects.
- if(bndRect.width > 180 && pt1.y < 70){
- numBus++;
- printf("R - %dn", numBus);}
- else if(bndRect.width > 120 && bndRect.width < 200){
- numCar++;
- printf("R - %d, width-%dn", numCar, bndRect.width);}
- else if( bndRect.width < 100 && pt2.y < 75){
- numPed++;
- printf("R - %d, width-%dn", numPed, bndRect.width);}
-
- //Reset the closest object to the right counter.
- Right = 320;
- }
-
- }
-
- //Draw the bounding rectangle around the moving object.
- cvRectangle(colourImage, pt1, pt2, CV_RGB(255,0,0), 1);
- }
- else if(pt1.x == 1 && pt2.x == 15 && numCar != 5 && numCar != 2){
- numCar ++;
- printf("exception : %d, width-%dn", numCar, bndRect.width);}
- //If the current object is closer to the left boundary but still not across
- //it, then change the closest to the left counter to this value.
- if(avgX > Left && avgX <= 90)
- {
- Left = avgX;
- }
-
- //If the current object is closer to the right boundary but still not across
- //it, then change the closest to the right counter to this value.
- if(avgX < Right && avgX >= 250)
- {
- Right = avgX;
- }
-
- //Save the current X value to use as the previous in the next iteration.
- prevX = avgX;
- printf("prev-----%dn",prevX );
- }
-
- sprintf(Scores, " %d/ %d/ %d", numBus, numCar, numPed);
- printf("%d, %d, %dn", numBus, numCar, numPed);
- //Write the number vehicle counted at the top of the output frame.
- cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
- cvPutText(colourImage, Scores, cvPoint(60, 200), &font, cvScalar(0, 0, 300));
-
- //Show the frame.
- cvShowImage("My Window", colourImage);
-
- //Wait for the user to see it.
- cvWaitKey(1);
- printf("one framen");
- //Write the frame to the output movie.
- cvWriteFrame(outputMovie, colourImage);
- }
-
- // Destroy the image, movies, and window.
- cvReleaseImage(&temp);
- cvReleaseImage(&difference);
- cvReleaseImage(&greyImage);
- cvReleaseImage(&movingAverage);
- cvDestroyWindow("My Window");
-
- cvReleaseCapture(&inputMovie);
- cvReleaseVideoWriter(&outputMovie);
-
- return(0);
- }