1 /*==========================================================================
3 show_video_v4l_capture.c: Functions for capturing video using V4L.
4 Written by Naoyuki Ichimura, AIST, 2001.
5 ==========================================================================*/
7 #include "show_video_v4l.h"
8 #include "extract_skin_color.h"
10 #define PREFIX_IMAGE_FILE "video_image"
12 /*=== Local global variables ===*/
13 static unsigned char disp_image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB];/* pointer to image buffer */
15 static int fd; /* file descriptor for frame buffer */
16 static int exit_flag; /* flag to exit program */
17 static int frame_no=1; /* frame number */
19 static struct video_mbuf vm; /* structure for frame buffer */
20 static struct video_mmap vmap; /* structure for memory space for frame buffer */
22 /*=== global variable ===*/
23 extern int extract_skin_color; /* global variable for enabling image processing */
25 void ShowVideoCaptureImage()
28 //static char image_prefix[1024]=PREFIX_IMAGE_FILE; /* Prefix of output image file */
29 static unsigned char image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB]; /* Captured image */
30 static unsigned char skin_map[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS]; /* Skin color map */
32 /*======= Wait capture for current frame ====================*/
33 CaptureV4LDoubleBufferingCaptureWait( fd , &vmap );
35 /*======= Set data to image array ===========================*/
36 CaptureV4LSetImageDownSamplingForOpenGL( vmap , vm , DOWN_SAMPLING_RATE , image , disp_image );
38 /*======= Begin capture for next frame ======================*/
39 if( CaptureV4LDoubleBufferingCaptureNextFrame( fd , &vmap ) == -1 ) {
40 fprintf( stderr , "COuld not capture next frame.\n" );
44 /*======= Display image =====================================*/
45 ShowVideoDisplayImage();
47 /*======= Extract skin color by simple thresholding =========*/
48 if( extract_skin_color ) {
49 ExtractSkinColorSimpleThresholding( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , skin_map );
50 ShowSkinRegion( skin_map , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS );
54 /*======= Save image ========================================*/
55 ShowVideoSavePPMImage( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , frame_no , image_prefix );
58 /*======= Swap Buffer: show graphics ========================*/
61 /*======= Increment frame number ============================*/
64 /*======= Check exit flag ===================================*/
71 int ShowVideoInitCaptureDevice( char *device_name , int channel_no )
74 struct video_capability vcap; /* structure for video capability */
75 struct video_channel vch[MAX_NO_CHANNEL]; /* structure for video channel */
76 struct video_picture vp;
78 /*======= Open video device ================================*/
79 if( ( fd = CaptureV4LOpen( device_name ) ) == -1 ) {
80 fprintf( stderr, "Could not open device %s.\n" , device_name );
84 /*======= Get information of device =========================*/
85 /*=== Get device capabilities ===*/
86 if( CaptureV4LGetDeviceCapability( fd , &vcap ) == -1 ) {
87 fprintf( stderr , "Could not get capabilities of video device.\n" );
91 /*=== Get channel information ===*/
92 if( CaptureV4LGetChannelInfo( fd , vch , vcap.channels ) == -1 ) {
93 fprintf( stderr , "Could not get channel information of video device.\n" );
97 /*=== Get memory map information ===*/
98 if( CaptureV4LGetMemoryMapInfo( fd , &vm ) == -1 ) {
99 fprintf( stderr , "Could not get memory map information.\n" );
103 /*======= Show picture information =========================*/
104 CaptureV4LGetPictureInfo( fd , &vp );
105 CaptureV4LDisplayPictureInfo( vp );
107 /*======= Select channel ===================================*/
108 if( CaptureV4LSelectChannel( fd , vch , channel_no ) == -1 ) {
109 fprintf( stderr , "Could not select channel.\n" );
113 /*======= Mapping frame buffer ==============================*/
114 if( CaptureV4LMemoryMapping( fd , vm ) == -1 ) {
115 fprintf( stdout , "Could not map frame buffer.\n" );
119 /*======= Set image size and formate ========================*/
120 vmap.width = CAPTURE_IMAGE_WIDTH;
121 vmap.height = CAPTURE_IMAGE_HEIGHT;
122 vmap.format = VIDEO_PALETTE_RGB24;
124 /*======= Begin Capture =====================================*/
125 CaptureV4LDoubleBufferingInitCapture( fd , &vmap );
131 void ShowVideoDisplayImage()
134 glRasterPos2i( 0 , 0 );
135 glDrawPixels( IMAGE_WIDTH_DS , IMAGE_HEIGHT_DS , GL_BGR , GL_UNSIGNED_BYTE , disp_image );
140 void ShowVideoMouseCheck( int button , int status , int x , int y )
143 if( button == GLUT_LEFT_BUTTON && status == GLUT_DOWN ) {
149 void ShowVideoSavePPMImage( unsigned char *image , int iheight , int iwidth , int frame_no , char *image_prefix )
152 char file_name[1024];
156 sprintf( file_name , "%s_%d.ppm" , image_prefix , frame_no );
157 if( ( fp = fopen( file_name , "w" ) ) == NULL ) {
158 fprintf( stderr , "Could not open image file: %s\n" , file_name );
161 fprintf( fp , "P6 %d %d 255\n" , iwidth , iheight );
162 fwrite( image , iheight*iwidth*RGB , 1 , fp );