📄 show_video_v4l_capture.c
字号:
/*========================================================================== $Id$ show_video_v4l_capture.c: Functions for capturing video using V4L. Written by Naoyuki Ichimura, AIST, 2001.==========================================================================*/#include "show_video_v4l.h"#include "extract_skin_color.h"#define PREFIX_IMAGE_FILE "video_image" /*=== Local global variables ===*/static unsigned char disp_image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB];/* pointer to image buffer */static int fd; /* file descriptor for frame buffer */static int exit_flag; /* flag to exit program */static int frame_no=1; /* frame number */static struct video_mbuf vm; /* structure for frame buffer */static struct video_mmap vmap; /* structure for memory space for frame buffer *//*=== global variable ===*/extern int extract_skin_color; /* global variable for enabling image processing */void ShowVideoCaptureImage(){ static char image_prefix[1024]=PREFIX_IMAGE_FILE; /* Prefix of output image file */ static unsigned char image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB]; /* Captured image */ static unsigned char skin_map[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS]; /* Skin color map *//*======= Wait capture for current frame ====================*/ CaptureV4LDoubleBufferingCaptureWait( fd , &vmap );/*======= Set data to image array ===========================*/ CaptureV4LSetImageDownSamplingForOpenGL( vmap , vm , DOWN_SAMPLING_RATE , image , disp_image );/*======= Begin capture for next frame ======================*/ if( CaptureV4LDoubleBufferingCaptureNextFrame( fd , &vmap ) == -1 ) { fprintf( stderr , "COuld not capture next frame.\n" ); exit(-1); }/*======= Display image =====================================*/ ShowVideoDisplayImage();/*======= Extract skin color by simple thresholding =========*/ if( extract_skin_color ) { ExtractSkinColorSimpleThresholding( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , skin_map ); ShowSkinRegion( skin_map , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS ); }#if 0/*======= Save image ========================================*/ ShowVideoSavePPMImage( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , frame_no , image_prefix );#endif/*======= Swap Buffer: show graphics ========================*/ glutSwapBuffers();/*======= Increment frame number ============================*/ frame_no++;/*======= Check exit flag ===================================*/ if( exit_flag ) { exit(0); }}int ShowVideoInitCaptureDevice( char *device_name , int channel_no ){ struct video_capability vcap; /* structure for video capability */ struct video_channel vch[MAX_NO_CHANNEL]; /* structure for video channel */ struct video_picture vp;/*======= Open video device ================================*/ if( ( fd = CaptureV4LOpen( device_name ) ) == -1 ) { fprintf( stderr, "Could not open device %s.\n" , device_name ); exit(-1); }/*======= Get information of device =========================*/ /*=== Get device capabilities ===*/ if( CaptureV4LGetDeviceCapability( fd , &vcap ) == -1 ) { fprintf( stderr , "Could not get capabilities of video device.\n" ); exit(-1); } /*=== Get channel information ===*/ if( CaptureV4LGetChannelInfo( fd , vch , vcap.channels ) == -1 ) { fprintf( stderr , "Could not get channel information of video device.\n" ); exit(-1); } /*=== Get memory map information ===*/ if( CaptureV4LGetMemoryMapInfo( fd , &vm ) == -1 ) { fprintf( stderr , "Could not get memory map information.\n" ); exit(-1); }/*======= Show picture information =========================*/ CaptureV4LGetPictureInfo( fd , &vp ); CaptureV4LDisplayPictureInfo( vp );/*======= Select channel ===================================*/ if( CaptureV4LSelectChannel( fd , vch , channel_no ) == -1 ) { fprintf( stderr , "Could not select channel.\n" ); exit(-1); }/*======= Mapping frame buffer ==============================*/ if( CaptureV4LMemoryMapping( fd , vm ) == -1 ) { fprintf( stdout , "Could not map frame buffer.\n" ); exit(-1); }/*======= Set image size and formate ========================*/ vmap.width = CAPTURE_IMAGE_WIDTH; vmap.height = CAPTURE_IMAGE_HEIGHT; vmap.format = VIDEO_PALETTE_RGB24; /*======= Begin Capture =====================================*/ CaptureV4LDoubleBufferingInitCapture( fd , &vmap ); return 0;}void ShowVideoDisplayImage(){ glRasterPos2i( 0 , 0 ); glDrawPixels( IMAGE_WIDTH_DS , IMAGE_HEIGHT_DS , GL_BGR , GL_UNSIGNED_BYTE , disp_image ); glFlush();}void ShowVideoMouseCheck( int button , int status , int x , int y ){ if( button == GLUT_LEFT_BUTTON && status == GLUT_DOWN ) { exit_flag = 1; }}void ShowVideoSavePPMImage( unsigned char *image , int iheight , int iwidth , int frame_no , char *image_prefix ){ char file_name[1024]; FILE *fp; sprintf( file_name , "%s_%d.ppm" , image_prefix , frame_no ); if( ( fp = fopen( file_name , "w" ) ) == NULL ) { fprintf( stderr , "Could not open image file: %s\n" , file_name ); exit(1); } fprintf( fp , "P6 %d %d 255\n" , iwidth , iheight ); fwrite( image , iheight*iwidth*RGB , 1 , fp ); fclose(fp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -