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

📄 cvvideo.cpp

📁 Program use openCV to demo edge detection (algorithm Gradient).
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include <glob.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <malloc.h>
#include <errno.h>
#include <pthread.h>
#include "dialogs.h"


#include "cvcam.h"
#include "cvvidtypes.h"
#include "videodev.h"
#include "icvvideo.h"
#include "render.h"

//#define DEBUG_CVCAM 1
//#define DEBUG_THREADS 1

#ifdef DEBUG_THREADS
#define pthread_mutex_lock(x) printf("locking mutex "#x" from thread %p\n",pthread_self()),pthread_mutex_lock(x)
#define pthread_mutex_unlock(x) printf("unlocking mutex "#x" from thread %p\n",pthread_self()),pthread_mutex_unlock(x)
#define pthread_rwlock_rdlock(x) printf("rdlocking rwlock "#x" from thread %p\n",pthread_self()),pthread_rwlock_rdlock(x)
#define pthread_rwlock_wrlock(x) printf("wrlocking rwlock "#x" from thread %p\n",pthread_self()),pthread_rwlock_wrlock(x)
#define pthread_rwlock_unlock(x) printf("unlocking rwlock "#x" from thread %p\n",pthread_self()),pthread_rwlock_unlock(x)
#define pthread_create(a,b,c,d) printf("starting new thread from thread %p\n",pthread_self()),pthread_create(a,b,c,d)
#endif

const int FRAMES_FORMAT=0;
const int TIME_FORMAT = 1;

CvVideoCamera* cameras = NULL;
static int ncameras = 0;

static void icvVideoPicture2ImageBuffer(int camera);
static IplImage * icvVideoGetImage(int cameraid);

/* Returns the actual number of currently available cameras */
int cvcamGetCamerasCount()
{
    glob_t               pglob;
    int                     count, count1;
    int                     CamerasCount =  0;
    int*                    descriptors;
    
    struct video_capability VidCap;
    struct video_channel    Channel;
    
#ifdef DEBUG_CVCAM
    printf("cvcamGetCamerasCount(), ncameras=%d\n",ncameras);
#endif
    if(ncameras)
        return ncameras;
    
    if(glob("/dev/video*",GLOB_MARK,NULL, &pglob))
    {
        fprintf(stderr, "cvcamGetCamerasCount:no video devices /dev/video(n)"
                        "found\n");
        return 0;
    }

    descriptors = (int*)malloc(pglob.gl_pathc*sizeof(int));
    
    for(count = 0; count < pglob.gl_pathc; count++)
    {
        descriptors[count] = open(pglob.gl_pathv[count],O_RDWR);
        if (descriptors[count]  != -1 )
        {
            if(ioctl(descriptors[count], VIDIOCGCAP, &VidCap)==-1)
            {
                close(descriptors[count]);
                break;
            }
            
            for(count1=0; count1<VidCap.channels; count1++)
                
            {
                Channel.channel = count1;
                
                if((ioctl(descriptors[count], VIDIOCGCHAN, &Channel) != -1))
                {
                    
                    Channel.norm = 1;
                    Channel.channel = count1;
                    
                    if((ioctl(descriptors[count],VIDIOCSCHAN, &Channel) != -1)&&
                        (ioctl(descriptors[count], VIDIOCGCAP, &VidCap)    != -1))
                    {
                        if(Channel.type&VIDEO_TYPE_CAMERA)
                        {
                            
                            cameras =
                                (CvVideoCamera*)realloc(cameras,
                                (CamerasCount+1)*sizeof(CvVideoCamera));
                            if(!icvVideoSetDefaultValues(CamerasCount, 
                                descriptors[count],
                                count1, VidCap))
                            {
                                printf("icvVideoSetDefaultValues failed for camera %d, channel %d\n",count,count1);
                                break; 
                            }
                            
                            strcpy(cameras[CamerasCount].description.device,
                                pglob.gl_pathv[count]);
                            
                            strcpy(cameras[CamerasCount].description.DeviceDescription,
                                VidCap.name);
                            
                            strcpy(cameras[CamerasCount].description.ChannelDescription,
                                Channel.name);
                            
                            CamerasCount++;
                            break;
                            
                        }//if(Channel.type&VIDEO_TYPE_CAMERA)
                    }//if((ioctl(descriptors[count],VIDIOCSCHAN, &Channel) != -1)&&
                    //(ioctl(descriptors[count], VIDIOCGCAP, &VidCap)!=-1))
                }//if(ioctl(fd, VIDIOCSCHAN, &Channel) != -1)
            }//for(count1=0; count1<VidCap.channels; count1++)
            if(count1 == VidCap.channels) // no valid channel found
            {
                printf("closing  descriptors[%d]=%d\n",count,descriptors[count]);
                close(descriptors[count]);
            }
        }//if (descriptors[count] = open(pglob.gl_pathv[count],O_RDWR)  != -1 )
    }//for (count = 0; count < pglob.gl_pathc; count++)
    globfree(&pglob);
    free(descriptors);
    ncameras = CamerasCount;
    XInitThreads();
#ifdef DEBUG_CVCAM
    printf("cvcamGetCamerasCount() found %d cameras\n",ncameras);
#endif
    return CamerasCount;
}
///////////////////////////////////////////////////////////////////////////////
/* get/set the property of the camera. returns 0 if the property is not supported */
int cvcamGetProperty(int cameraid, const char* property, void* value)
{
    CvVideoCamera *const camera = &(cameras[cameraid]);

#ifdef DEBUG_CVCAM
    printf("cvcamGetProperty(camera=%d,property=%s)\n",cameraid, property);
#endif
    if(!cameras||cameraid>ncameras-1||cameraid<0)
    {
        fprintf(stderr,"cvcamGetProperty:no such camera\n");
        return 0;
    }//if(!cameras||camera>ncameras-1||camera<0)

    IF_GETTING_PROPERTY("description")
    {
        strcpy(((CameraDescription*)value)->DeviceDescription,camera->description.DeviceDescription);
        strcpy(((CameraDescription*)value)->device, camera->description.device);
        strcpy(((CameraDescription*)value)->ChannelDescription,camera->description.ChannelDescription);
        
        ((CameraDescription*)value)->channel = camera->description.channel;
        
        ((CameraDescription*)value)->maxwidth = camera->description.maxwidth;
        ((CameraDescription*)value)->maxheight = camera->description.maxheight;
        ((CameraDescription*)value)->minwidth = camera->description.minwidth;
        ((CameraDescription*)value)->minheight = camera->description.minheight;
        
        return 1;
    }//IF_GETTING_PROPERTY("description")
    
    IF_GETTING_PROPERTY("enable")
    {
        *(int*)value = camera->enabled;
        return 1;
    }//IF_GETTING_PROPERTY("enable")
    
    IF_GETTING_PROPERTY("render")
    {
        *(int*)value = camera->rendered;
        //TODO: real call to rendering ioctl(if after initialisation)
        
        return 1;
    }//IF_GETTING_PROPERTY("render")
    
    IF_GETTING_PROPERTY("window")
    {
        *(Window*)value = camera->window;
        return 1;
    }//IF_GETTING_PROPERTY("window")
    
    IF_GETTING_PROPERTY("callback")
    {
        fprintf(stdout,"The property \"callback\" is set-only\n");
        return 0;
    }//IF_GETTING_PROPERTY("callback")
    
    IF_GETTING_PROPERTY("camera_pp")
    {
        icvVideoCameraPPDialog(cameraid);
        return 1;
    }//IF_GETTING_PROPERTY("camera_pp")
    
    IF_GETTING_PROPERTY("set_video_format")
    {
        /*
        if(!camera->initialised)
        {
        fprintf(stderr, "cvcamGetProperty, property = video_pp, camera\
        isn't initialised");
        return 0;
        }
        */
        ((VideoFormat*)value)->width = camera->videopp.width;
        ((VideoFormat*)value)->height = camera->videopp.height;
        
        ((VideoFormat*)value)->picture.brightness =
            camera->videopp.picture.brightness;
        
        ((VideoFormat*)value)->picture.hue =
            camera->videopp.picture.hue;
        
        ((VideoFormat*)value)->picture.colour =
            camera->videopp.picture.colour;
        
        ((VideoFormat*)value)->picture.contrast =
            camera->videopp.picture.contrast;
        
        ((VideoFormat*)value)->picture.whiteness   =
            camera->videopp.picture.whiteness;
        
        ((VideoFormat*)value)->picture.depth =
            camera->videopp.picture.depth;
        
        ((VideoFormat*)value)->picture.palette =
            camera->videopp.picture.palette;
        
        return 1;
    }//IF_GETTING_PROPERTY("set_video_format")
    
    IF_GETTING_PROPERTY("rectify_parameters")
    {
        fprintf(stdout,"TODO get prop rectify_parameters\n");
        return 1;
    }//IF_GETTING_PROPERTY("rectify_parameters")
    
    IF_GETTING_PROPERTY("stereo_parameters")
    {
        fprintf(stdout,"TODO get prop stereo_parameters\n");
        return 1;
    }//IF_GETTING_PROPERTY("stereo_parameters")
    
    IF_GETTING_PROPERTY("raw_image")
    {
        *((IplImage**)value) = icvVideoGetImage(cameraid);
        if(*((IplImage**)value) != NULL)
            return 1;
        else
            return 0;
    }//IF_GETTING_PROPERTY("raw_image")
    
    IF_GETTING_PROPERTY("disparity")
    {
        fprintf(stdout,"TODO get prop disparity\n");
        return 1;
    }//IF_GETTING_PROPERTY("disparity")
    
    IF_GETTING_PROPERTY("stereo_image")
    {
        fprintf(stdout,"TODO get prop stereo_image\n");
        return 1;
    }//IF_GETTING_PROPERTY("stereo_image")
    
    IF_GETTING_PROPERTY("video_pp")
    {
        icvVideoVideoPPDialog(cameraid);
        return 1;
    }//IF_GETTING_PROPERTY("video_pp")

    IF_GETTING_PROPERTY(CVCAM_RNDWIDTH)
    {
        *(int*)value = camera->renderwidth;
        return 1;
    }//IF_GETTING_PROPERTY (CVCAM_RNDWIDTH)

    IF_GETTING_PROPERTY(CVCAM_RNDHEIGHT)
    {
     *(int*)value = camera->renderheight;
        return 1;
    }//IF_GETTING_PROPERTY("CVCAM_RNDHEIGHT")


    return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////


/* gets all property names. the actual number of properties is returned. */
int cvcamGetPropertiesList(int camera, const char** properties, int count)
{
#ifdef DEBUG_CVCAM
    printf("cvcamGetPropertiesList()\n");
#endif
    assert(properties);
    
    properties[0] = "description" ;
    properties[1] = "enable";
    properties[2] = "render";
    properties[3] = "window";
    properties[4] = "callback";
    properties[5] = "camera_pp";
    properties[6] = "video_pp" ;
    properties[7] = "raw_image";
    properties[8] = "set_video_format" ;
    
    return 9;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////


int cvcamSetProperty(int cameraid, const char* property, void* value)
{

⌨️ 快捷键说明

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