📄 opencvsimple.cpp
字号:
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include "cv.h"
#include "cxcore.h"
#include "VideoManControl.h"
#include "VideoManInputFormat.h"
using namespace std;
/*
This is a simple example using OpenCV. One video input is initialized and it is processed using openCV
To use this example, VideoMan must be built with the directive VM_OGLRenderer,
also you need to build the input VMDirectShow
*/
VideoManControl videoMan;
int screenLeft, screenUp, screenWidth, screenHeight;
bool fullScreened;
double videoLength;
int videoInputID; //Index of the video input
std::vector< int > userInputIDs; //Indexes of the userinputs for showing the processed images
std::string videoFile;
IplImage *inputHeader; //Header for the input image
IplImage *processedImages[3]; //The processed images
IplImage *edges;
IplImage *gray;
string dirPath;
void glutResize(int width, int height)
{
screenLeft = 0;
screenUp = 0;
screenWidth = width;
screenHeight = height;
//Notify to VideoMan the change of the screen size
videoMan.changeScreenSize( screenLeft, screenUp, screenWidth, screenHeight );
}
void clear()
{
for ( int i = 0; i< 3; ++i )
cvReleaseImage( &processedImages[i] );
cvReleaseImageHeader( &inputHeader );
cvReleaseImage( &edges );
cvReleaseImage( &gray );
}
void glutKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
{
clear();
exit(0);
}
}
}
void glutSpecialKeyboard(int value, int x, int y)
{
switch (value)
{
case GLUT_KEY_F1:
{
if ( !fullScreened )
glutFullScreen();
else
{
glutPositionWindow( 0, 20 );
glutReshapeWindow( 640, 480 );
}
fullScreened = !fullScreened;
break;
}
case GLUT_KEY_F2:
{
if ( videoMan.isActivated( userInputIDs[0] ) )
videoMan.deactivateVideoInput( userInputIDs[0] );
else
videoMan.activateVideoInput( userInputIDs[0] );
break;
}
case GLUT_KEY_F3:
{
if ( videoMan.isActivated( userInputIDs[1] ) )
videoMan.deactivateVideoInput( userInputIDs[1] );
else
videoMan.activateVideoInput( userInputIDs[1] );
break;
}
case GLUT_KEY_F4:
{
if ( videoMan.isActivated( userInputIDs[2] ) )
videoMan.deactivateVideoInput( userInputIDs[2] );
else
videoMan.activateVideoInput( userInputIDs[2] );
break;
}
case GLUT_KEY_F5:
{
static int mode = 0;
mode = (mode + 1 ) %2 ;
videoMan.changeVisualizationMode( mode );
break;
}
case GLUT_KEY_F6:
{
static int main = 0;
main = (main + 1 ) %videoMan.getNumberOfInputs() ;
videoMan.changeMainVisualizationInput( main );
break;
}
}
}
void InitializeOpenGL()
{
}
bool InitializeVideoMan()
{
VideoManInputFormat format;
inputIdentification device;
if ( !dirPath.empty() )
{
//Initialize one input from a video file
device.fileName = dirPath;
device.identifier = "DSHOW_VIDEO_FILE"; //using directshow
format.timeFormat = SECONDS; //We want the time format in seconds
//play in real-time
format.clock = true;
format.dropFrames = true;
format.renderAudio = true;
//Initialize the video file is the path
if ( ( videoInputID = videoMan.addVideoInput( device, &format ) ) != -1 )
{
printf("Loaded video file: %s\n", device.fileName.c_str() );
printf("resolution: %d %d\n", format.width, format.height );
//get the length of the video
videoLength = videoMan.getLength( videoInputID );
printf("duration: %f seconds\n\n", videoLength );
videoMan.playVideo( videoInputID );
}
}
else
{
//Initialize one input from a camera
std::vector<inputIdentification> list;
videoMan.getAvailableDevices( "DSHOW_CAPTURE_DEVICE", list ); //list all the available devices
if ( list.size()>0 )
device = list[0]; //take the first
else
printf("There is no available camera\n");
//Show dialog to select the format
format.showDlg = true;
format.SetFormat( 640, 320,30, RGB24, RGB24 );
if ( list.size()>0 && ( videoInputID = videoMan.addVideoInput( device, &format ) ) != -1 )
{
videoMan.showPropertyPage( videoInputID );
videoMan.getFormat( videoInputID, format );
printf("Initilized camera: %s\n", device.friendlyName.c_str() );
printf("resolution: %d %d\n", format.width, format.height );
printf("FPS: %f\n\n", format.fps );
}
}
if ( videoInputID != -1 )
{
inputHeader = cvCreateImageHeader( cvSize( format.width, format.height), format.depth, format.nChannels );
//Initialize the user inputs for showing the processed images
device.identifier = "USER_INPUT";
int inputID;
for ( int i = 0; i < 3; ++i )
{
if ( ( inputID = videoMan.addVideoInput( device, &format ) ) != -1 )
{
userInputIDs.push_back( inputID );
processedImages[i] = cvCreateImage( cvSize( format.width, format.height), format.depth, format.nChannels );
videoMan.setUserInputImage( inputID, processedImages[i]->imageData );
}
}
edges = cvCreateImage( cvSize( format.width, format.height), 8, 1 );
gray = cvCreateImage( cvSize( format.width, format.height), 8, 1 );
}
//We want to display all the intialized video inputs
videoMan.activateAllVideoInputs();
return ( videoInputID != -1 );
}
void glutDisplay(void)
{
//Clear the opengl window
glClear( GL_COLOR_BUFFER_BIT );
//Get a new frame
char *image = videoMan.getFrame( videoInputID );
if ( image != NULL )
{
//Update the texture of the renderer
videoMan.updateTexture( videoInputID );
cvSetImageData( inputHeader, image, inputHeader->widthStep );
// Process the images...
//Convert to grayscale
cvCvtColor( inputHeader, gray, CV_RGB2GRAY );
//process 0
cvCvtColor( gray, processedImages[0], CV_GRAY2RGB );
videoMan.updateTexture( userInputIDs[0] );
//process 1
cvThreshold( gray, gray, 100, 255, CV_THRESH_BINARY );
cvCvtColor( gray, processedImages[1], CV_GRAY2RGB );
videoMan.updateTexture( userInputIDs[1] );
//process 2
cvSobel( gray, edges, 1, 1 );
cvDilate( edges,edges );
cvCvtColor( edges, processedImages[2], CV_GRAY2RGB );
videoMan.updateTexture( userInputIDs[2] );
//Release the frame
videoMan.releaseFrame( videoInputID );
}
//render the image of input n in the screen
videoMan.renderFrame( videoInputID );
for ( int i = 0; i < 3; ++i )
videoMan.renderFrame( userInputIDs[i] );
//Check if the video file (input number 0) has reached the end
if ( videoMan.getPosition(0) == videoLength )
videoMan.goToFrame( 0, 0 ); //restart from the begining
glFlush();
glutSwapBuffers();
}
void showHelp()
{
printf("========\n");
printf("keys:\n");
printf("Esc->Exit\n");
printf("F1->Fullscreen\n");
printf("F2-F4->Show Processed Images y/n\n");
printf("F5->Change Visualization Mode\n");
printf("F6->Change Main Visualization Input\n");
printf("========\n");
}
int main(int argc, char** argv)
{
cout << "This is a simple example using OpenCV. One video input is initialized and it is processed using openCV" << endl;
cout << "Usage: VMwithDirectShow.exe filePath(string)" << endl;
cout << "Example: VMwithDirectShow.exe c:\\video.avi" << endl;
cout << "If you don't specify a filepath, a camera will be initialized" << endl;
if ( argc > 1 )
dirPath = argv[1];
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 640, 480 );
glutInit( &argc, argv );
glutCreateWindow("VideoMan with DirectShow");
glutReshapeFunc(glutResize);
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutDisplay);
glutKeyboardFunc(glutKeyboard);
glutSpecialFunc(glutSpecialKeyboard);
InitializeOpenGL();
if ( !InitializeVideoMan() )
{
return 0;
}
fullScreened = false;
showHelp();
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -