📄 stereocam.cpp
字号:
// StereoCam.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <cv.h>
#include <cvcam.h>
//傈开函荐
CvSize imgSize;
IplImage* greyImage;// = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
IplImage* colourImage;
IplImage* movingAverage;// = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
IplImage* difference;
IplImage* temp;
IplImage* motionHistory;// = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
//Rectangle to use to put around the people.
CvRect bndRect= cvRect(0,0,0,0);
//Points for the edges of the rectangle.
CvPoint pt1, pt2;
//Create a font object.
CvFont font;
//Buffer to save the number of people when converting the integer
//to a string.
char wow[65];
//The midpoint X position of the rectangle surrounding the moving objects.
int avgX = 0;
//Indicates whether this is the first time in the loop of frames.
bool first = true;
void stereocallback(IplImage* image1, IplImage* image2)
{
//convert video#1 from RGB to HSV
//cvCvtColor(image1, image1, CV_RGB2HSV);
////////////////////////////////////////////////////////////////////////////////////////////////
//Get a frame from the input video.
IplImage * colourImage= cvCreateImage(cvGetSize(image1),IPL_DEPTH_8U,3);
colourImage = cvCloneImage(image1);
//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);
}
//Convert the scale of the moving average.
cvConvertScale(movingAverage,temp, 1.0, 0.0);
//Minus the current frame from the moving average.
cvAbsDiff(colourImage,temp,difference);
//Convert the image to grayscale.
cvCvtColor(difference,greyImage,CV_RGB2GRAY);
//Convert the image to black and white.
cvThreshold(greyImage, greyImage, 90, 255, CV_THRESH_BINARY);
//Dilate and erode to get people blobs
cvDilate(greyImage, greyImage, 0, 18);
cvErode(greyImage, greyImage, 0, 10);
//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 rectangle 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;
//Get an average X position of the moving contour.
avgX = (pt1.x + pt2.x) / 2;
/////////////////////////////////////////////////
if ((int)((pt2.x-pt1.x)*1.5)<(pt2.y-pt1.y))
{
cvRectangle(colourImage, pt1, pt2, CV_RGB(0,255,0), 8);
}
//Draw the bounding rectangle around the moving object.
else
{
;
}
}
//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.
// image1=cvCloneImage(colourImage);
IplImage * GRAY1 = cvCreateImage(cvGetSize(colourImage),IPL_DEPTH_8U,1);
cvCvtColor(colourImage, GRAY1, CV_RGB2GRAY);
cvCvtColor(GRAY1, image1, CV_GRAY2RGB);
//Write the number of people counted at the top of the output frame.
//cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
// cvPutText(colourImage, _itoa(numPeople, wow, 10), cvPoint(60, 200), &font, cvScalar(0, 0, 300));
////////////////////////////////////////////////////////////////////////////////////////////////
//convert video#2 from RGB to Gray
IplImage * GRAY = cvCreateImage(cvGetSize(image2),IPL_DEPTH_8U,1);
cvCvtColor(image2, GRAY, CV_RGB2GRAY);
cvCvtColor(GRAY, image2, CV_GRAY2RGB);
cvReleaseImage(&GRAY);
}
void ShowCamVideo(HWND hwnd1, HWND hwnd2)
{
int* out;
int ncams = cvcamGetCamerasCount( );
int nselected = cvcamSelectCamera(&out);
cvcamSetProperty(out[0], CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(out[0], CVCAM_PROP_RENDER, CVCAMTRUE);
cvcamSetProperty(out[0], CVCAM_STEREO_CALLBACK,stereocallback);
cvcamSetProperty(out[0], CVCAM_PROP_WINDOW, &hwnd1);
if ( nselected == 2 ) //if the 2nd camera has been selected
{
cvcamSetProperty(out[1], CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(out[1], CVCAM_PROP_RENDER, CVCAMTRUE);
cvcamSetProperty(out[1], CVCAM_PROP_WINDOW, &hwnd2);
cvcamSetProperty(out[1], CVCAM_STEREO_CALLBACK,stereocallback);
}
cvcamInit( );
cvcamStart( );
}
void ExitCamera()
{
cvcamStop( );
cvcamExit( );
}
long FAR PASCAL WindowProc( HWND hwnd, UINT msg, UINT wParam, LONG lParam)
{
switch (msg)
{
case WM_DESTROY:
ExitCamera();
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
return(NULL);
}
int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow )
{
MSG msg;
WNDCLASS wc;
ZeroMemory(&wc, sizeof wc);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInst;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( DKGRAY_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "CAMERAONE";
RegisterClass( &wc );
HWND hwnd1 = CreateWindow(
"CAMERAONE",
"CVCAM Sample1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);
wc.lpszClassName = "CAMERATWO";
RegisterClass( &wc );
HWND hwnd2 = CreateWindow(
"CAMERATWO",
"CVCAM Sample2",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);
//////////////////////////////////////////////////////////////////////////////////
imgSize.width = 320;
imgSize.height = 240;
IplImage* greyImage= cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
// IplImage* colourImage;
IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
// IplImage* difference;
// IplImage* temp;
IplImage* motionHistory = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
//Rectangle to use to put around the people.
//CvRect bndRect = cvRect(0,0,0,0);
ShowWindow( hwnd1, nCmdShow );
UpdateWindow( hwnd1 );
ShowWindow( hwnd2, nCmdShow );
UpdateWindow( hwnd2 );
//Show me the Stereo CvCam in Action!
ShowCamVideo(hwnd1, hwnd2);
//release image
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -