⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carcounting.cpp

📁 本程序可以对输入.avi 序列中的车辆数进行计算
💻 CPP
字号:
#include <iostream>
#include <sys/timeb.h>
#include <time.h>
#include <string>
#include <sstream>
#include <string.h>
 
// OpenCV includes.
#include <cv.h>
#include <highgui.h>
 
using namespace std;
int main(int argc, char* argv[])
{ 
	// Tell OpenCV to capture data from a camera.
	CvCapture* myMovie = cvCaptureFromAVI("cars.avi");
 	// Create a window with the given name (displayed at the top of the window).
	// Also, we'll make it automatically resize to whatever content it displays.
	cvNamedWindow("Movie", CV_WINDOW_AUTOSIZE);
	CvVideoWriter*  videoWriter = cvCreateVideoWriter("carCount.avi", -1, 
		cvGetCaptureProperty(myMovie, CV_CAP_PROP_FPS), 
cvSize(cvGetCaptureProperty(myMovie, CV_CAP_PROP_FRAME_WIDTH ),cvGetCaptureProperty(myMovie, CV_CAP_PROP_FRAME_HEIGHT))
					);

	cvNamedWindow("MHI", CV_WINDOW_AUTOSIZE);
 
	IplImage* image = cvQueryFrame(myMovie);
	IplImage* nextImage = cvQueryFrame(myMovie);
 
	CvSize size = cvSize(image->width,image->height); 
	IplImage* diff = cvCreateImage( size, IPL_DEPTH_8U, 1 );
	//Motion History Image
	IplImage *mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
	cvZero(mhi);
	IplImage* segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
	IplImage* mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
	IplImage* blueMotion = cvCloneImage(image);
 
	int frameCounter = 0;
	double timeCounter = 0;
	// Loop until end of avi file
	int carCount = 0;
	double totalTime = 0;
 
	IplImage* temp = cvCreateImage(size, IPL_DEPTH_8U, 1);
	IplImage* temp2 = cvCreateImage(size, IPL_DEPTH_8U, 1);
 	CvScalar color;
	CvRect comp_rect;
 	cvCvtPixToPlane(image, temp, NULL, NULL, NULL);
	
	//cvFlip(temp, NULL, 0);
	CvMemStorage* storage = cvCreateMemStorage(0);
	double magnitude;   
	//CvPoint center;
	double count;
	IplImage* orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
	double angle;
	CvRect lastComp = cvRect(0,0,image->width, image->height);;
	cvSaveImage("Start.jpg", image);
	while(1)
	{
 	// Make the image show up in the named window.
	//Add text to the image char myName("Emily Ericson");
		if(nextImage != NULL)
		{
			CvRect myRoi = cvRect(97,0,100, image->height);
			cvSetImageROI(image, myRoi);
			cvSetImageROI(temp2, myRoi);
			cvSetImageROI(nextImage, myRoi);
			cvSetImageROI(temp, myRoi);
			cvCvtPixToPlane(nextImage, temp2, NULL, NULL, NULL);
			//cvFlip(temp2, NULL, 0);
			double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds
			//cvCvtColor( temp, temp, CV_BGR2GRAY ); // convert frame to grayscale
			//cvCvtColor( temp2, temp2, CV_BGR2GRAY ); // convert frame to grayscale
			cvSetImageROI(diff, myRoi);
			cvAbsDiff( temp, temp2, diff ); // get difference between frames
			cvSetImageROI(mhi, myRoi);
			cvThreshold( diff, diff, 150, 1, CV_THRESH_BINARY ); // and threshold it
			cvUpdateMotionHistory( diff, mhi, timestamp, 1 ); // update MHI
			//cvFlip(mhi, NULL, 1);
			// convert MHI to blue 8u image
			cvSetImageROI(mask, myRoi);
			cvCvtScale( mhi, mask, 255./1,(2 - timestamp)*255./1 );
			//cvZero( blueMotion );
			blueMotion = cvCloneImage(image);
			cvSetImageROI(blueMotion, myRoi);
			cvCvtPlaneToPix( mask, 0, 0, 0, blueMotion );
			cvSetImageROI(orient, myRoi);
			// calculate motion gradient orientation and valid orientation mask
			cvCalcMotionGradient( mhi, mask, orient, 1, .05, 3 );
			cvSetImageROI(segmask, myRoi);
			CvSeq* seq = cvSegmentMotion( mhi, segmask, storage, timestamp, 1 );
			for( int i = -1; i < seq->total; i++ ) 
				{
					if( i < 0 ) { // case of the whole image
					comp_rect = cvRect( 0, 0, size.width, size.height );
					color = CV_RGB(255,255,255);
					magnitude = 100;
					}
					else
					 { // i-th motion component
					comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect;
					if( comp_rect.width + comp_rect.height < 1.5 ) // reject very small components
						continue;
					color = CV_RGB(255,0,0);
					magnitude = 30;
					cvRectangle(image, cvPoint(comp_rect.x - 18, comp_rect.y-18),
					cvPoint(comp_rect.x + 18, comp_rect.y+18), color, 3);
					cvRectangle(blueMotion, cvPoint(comp_rect.x-18, comp_rect.y-18),
					cvPoint(comp_rect.x + 18, comp_rect.y+18), color, 3);
					if((lastComp.y > comp_rect.y+30) || (lastComp.y < comp_rect.y-30) 
						|| (lastComp.x > comp_rect.x+27) || (lastComp.x < comp_rect.x-27))
						{
							 carCount++;
							cout << carCount << endl;
						}
					lastComp = comp_rect;
						}
					}
			//cvSetImageROI( image, comp_rect );
			//cvSetImageROI( mhi, comp_rect );
			//cvSetImageROI(blueMotion, comp_rect);
			count = cvNorm( blueMotion, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI
			// calculate orientation
			angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp,1);
			cvResetImageROI( mhi );
			cvResetImageROI(blueMotion);
			cvResetImageROI( image );
			cvResetImageROI( temp2 );
			cvResetImageROI( nextImage );
			cvResetImageROI( diff );
			cvResetImageROI( mask );
			cvShowImage("MHI",blueMotion);
			cvShowImage("Movie", image);
			// Write images to the output video.
			cvWriteFrame(videoWriter, blueMotion);
			cvClearMemStorage(storage);
			}
		else
		{
			char* myCount = new char[3];
			stringstream number;
			string count;
			number >> carCount;
			number << count;	
			cout << number.str() << endl;
			string myString = "Approx. ";
			myString += itoa(carCount, myCount, 10);
			myString += " cars.";
			//strcat(myCount, "Approx. ");
			//strcat(myCount, carCount);
			//strcat(myCount, " cars");
			CvFont font;
			cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
			cvPutText(image, myString.c_str(), cvPoint(60, 200), &font, cvScalar(0, 0, 300));
			cvWriteFrame(videoWriter, image);
			cvShowImage("MHI",image);
			cvWaitKey(10);
			//cvWriteFrame(videoWriter, image);
			//cvShowImage("MHI",image);
			//cvWaitKey(10);
			//cvWriteFrame(videoWriter, image);
			//cvShowImage("MHI",image);
			//cvWaitKey(10);
			//cvWriteFrame(videoWriter, image);
			//cvShowImage("MHI",image);
			//cvWaitKey(10);
			break;
			}
		// This will return the code of the pressed key or -1 if
		// nothing was pressed before 10 ms elapsed.
		int keyCode = cvWaitKey(10);
		// Get the latest image from the camera.  This
		// image should NOT be released manually.
		image = nextImage;
		nextImage = cvQueryFrame(myMovie);
		}
 
	// Destroy the capture object and the window.
	cvReleaseCapture(&myMovie);
	cvDestroyWindow("Movie");
	cvDestroyWindow("MHI");
	cvReleaseVideoWriter(&videoWriter);
  	cvReleaseImage(&nextImage);
	cvReleaseImage(&temp);
	cvReleaseImage(&temp2);
	cvReleaseImage(&orient);
	cvReleaseImage(&blueMotion);
	cvReleaseImage(&mhi);
 
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -