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

📄 cvvideo.cpp

📁 Program use openCV to demo edge detection (algorithm Gradient).
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    int k;
    CvVideoCamera *const camera = &(cameras[cameraid]);
    
#ifdef DEBUG_CVCAM
    printf("cvcamSetProperty(camera=%d,property=%s)\n",cameraid, property);
#endif
    if(!cameras||cameraid>ncameras-1||cameraid<0)
    {
        fprintf(stderr,"cvcamSetProperty: no such camera\n");
        return 0;
    }//if(!cameras||camera>ncameras-1)
    
    IF_SETTING_PROPERTY("description")
    {
        fprintf(stdout, "cvcamSetProperty: The property \"description\" cannot be "
                        "set\n");
        return 0;
    }//IF_SETTING_PROPERTY("description")
    
    IF_SETTING_PROPERTY("enable")
    {
        camera->enabled = (int)value;
        return 1;
    }//IF_SETTING_PROPERTY("enable")
    
    IF_SETTING_PROPERTY("render")
    {
        camera->rendered = (int)value;
        return 1;
    }//IF_SETTING_PROPERTY("render")
    
    IF_SETTING_PROPERTY("window")
    {
        camera->window = *(Window*)value;
        
        //fprintf(stdout,"TODO get prop window\n");
        return 1;
    }//IF_SETTING_PROPERTY("window")
    
    IF_SETTING_PROPERTY("callback")
    {
        camera->callback = ( void (*)(IplImage*) )value;
        return 1;
    }//IF_SETTING_PROPERTY("callback")

    IF_SETTING_PROPERTY("camera_pp")
    {
        fprintf(stdout,"TODO get prop camera_pp\n");
        return 1;
    }//IF_SETTING_PROPERTY("camera_pp")
    
    IF_SETTING_PROPERTY("description")
    {
        fprintf(stdout,"TODO get prop camera_pp\n");
        return 1;
    }//IF_SETTING_PROPERTY("camera_pp")
    
    IF_SETTING_PROPERTY("set_video_format")
    {
        k = icvVideoStop(cameraid);
        if(
            (((VideoFormat*)value)->picture.brightness>0xffff)||
            (((VideoFormat*)value)->picture.hue>0xffff)||
            (((VideoFormat*)value)->picture.colour>0xffff)||
            (((VideoFormat*)value)->picture.contrast>0xffff)||
            (((VideoFormat*)value)->picture.whiteness>0xffff)||
            (((VideoFormat*)value)->width > camera->description.maxwidth)||
            (((VideoFormat*)value)->height > camera->description.maxheight)||
            (((VideoFormat*)value)->width < camera->description.minwidth)||
            (((VideoFormat*)value)->height < camera->description.minheight)
            )
        {
#ifdef DEBUG_CVCAM
            printf("cvcamSetProperty(camera=%d,property=%s): values out of bounds\n",cameraid, property);
#endif
            if(k)
                icvVideoStart(cameraid);
            return 0;
        }
        
        if(
            ((((VideoFormat*)value)->width == camera->description.maxwidth)&&
            (((VideoFormat*)value)->height == camera->description.maxheight))||
            ((((VideoFormat*)value)->width == camera->description.minwidth)&&
            (((VideoFormat*)value)->height == camera->description.minheight))||
            ((((VideoFormat*)value)->width == 320)&&                
            (((VideoFormat*)value)->height == 240))
            )
        {
            camera->videopp.width = ((VideoFormat*)value)->width;
            camera->videopp.height= ((VideoFormat*)value)->height;
        }
        else
        {
            if(
                ((((VideoFormat*)value)->width > 320)&&
                ((VideoFormat*)value)->height > 240)
                )
            {
                camera->videopp.width = 320;
                camera->videopp.height= 240;
            }else
            {
                camera->videopp.width=
                    camera->description.minwidth;
                
                camera->videopp.height=
                    camera->description.minheight;
            }
        }
        
        camera->videopp.picture.brightness=
            ((VideoFormat*)value)->picture.brightness;
        
        camera->videopp.picture.hue=
            ((VideoFormat*)value)->picture.hue;
        
        camera->videopp.picture.colour=
            ((VideoFormat*)value)->picture.colour;
        
        camera->videopp.picture.contrast=
            ((VideoFormat*)value)->picture.contrast ;
        
        camera->videopp.picture.whiteness=
            ((VideoFormat*)value)->picture.whiteness;
        
        camera->videopp.picture.palette=
            ((VideoFormat*)value)->picture.palette;
        if(camera->videopp.picture.palette == VIDEO_PALETTE_YUV420P)
            camera->imagepalette = VIDEO_PALETTE_RGB24;

        camera->videopp.picture.depth = icvVideoFormat2Depth(camera->imagepalette);
        
        if(!camera->videopp.picture.depth)
            return 0;


        //cvcamInit();

        if(k)
            icvVideoStart(cameraid);
        //fprintf(stdout,"TODO get prop video_pp\n");
        return 1;
    }//IF_SETTING_PROPERTY("set_video_format")
    
    IF_SETTING_PROPERTY("rectify_parameters")
    {
        fprintf(stdout,"TODO get prop rectify_parameters\n");
        return 1;
    }//IF_SETTING_PROPERTY("rectify_parameters")
    
    IF_SETTING_PROPERTY("stereo_parameters")
    {
        fprintf(stdout,"TODO get prop stereo_parameters\n");
        return 1;
    }//IF_SETTING_PROPERTY("stereo_parameters")
    
    IF_SETTING_PROPERTY("raw_image")
    {
        fprintf(stdout,"TODO get prop raw_image\n");
        return 1;
    }//IF_SETTING_PROPERTY("raw_image")
    
    IF_SETTING_PROPERTY("disparity")
    {
        fprintf(stdout,"TODO get prop disparity\n");
        return 1;
    }//IF_SETTING_PROPERTY("disparity")
    
    IF_SETTING_PROPERTY("stereo_image")
    {
        fprintf(stdout,"TODO get prop stereo_image\n");
        return 1;
    }//IF_SETTING_PROPERTY("stereo_image")

    IF_SETTING_PROPERTY("video_pp")
    {
        fprintf(stdout,"TODO get prop set_video_format\n");
        return 1;
    }//IF_SETTING_PROPERTY( "video_pp" )


    IF_SETTING_PROPERTY(CVCAM_RNDWIDTH)
    {
        k=icvVideoStop(cameraid);
        camera->renderwidth = *(int*)value;
        if(k)
            icvVideoStart(cameraid);
        return 1;

    }//IF_SETTING_PROPERTY(CVCAM_RNDWIDTH)

    IF_SETTING_PROPERTY(CVCAM_RNDHEIGHT)
    {
        k=icvVideoStop(cameraid);
        camera->renderheight = *(int*)value;
        if(k)
            icvVideoStart(cameraid);
        return 1;

    }//IF_SETTING_PROPERTY(CVCAM_RNDHEIGHT)

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

/* Prepares the currently enabled cameras for work */
int cvcamInit()
{
    int camera;
#ifdef DEBUG_CVCAM
    printf("cvcamInit()\n");
#endif
    for(camera=0; camera<ncameras; camera++)
    {
        icvVideoInit(camera);
    }//for(camera=0; camera<ncameras; camera++)

    return 1;
}

///////////////////////////////////////////////////////////////////////////////
/* Start the video */
int cvcamStart()
{
    int cameraid;
    
#ifdef DEBUG_CVCAM
    printf("cvcamStart()\n");
#endif
    for(cameraid=0; cameraid<ncameras; cameraid++)
    {
        if(cameras[cameraid].enabled)
        {
            if(!icvVideoStart(cameraid))
            {
                return 0;
            }
        }
    }//for(cameraid=0; cameraid<ncameras; cameraid++)

    return 1;
}

int icvVideoStart(int cameraid)
{
    CvVideoCamera *const camera = &(cameras[cameraid]);
    int frame;

    pthread_mutex_lock(&(camera->capturestatemutex));
    if(camera->capturestate != READY)
    {
        fprintf(stderr,"icvVideoStart: camera %d not ready\n",camera);
        pthread_mutex_unlock(&(camera->capturestatemutex));
        return 0;
    }
    camera->capturestate = STARTING;
    pthread_cond_signal(&(camera->capturestatecond));
    pthread_mutex_unlock(&(camera->capturestatemutex));

    pthread_mutex_lock(&(camera->ioctlmutex));
    if(ioctl(camera->device, VIDIOCGMBUF,
             &(camera->mbuf)) == -1)
    {
        pthread_mutex_unlock(&(camera->ioctlmutex));
        return 0;
    }
    pthread_mutex_unlock(&(camera->ioctlmutex));
#ifdef DEBUG_CVCAM
    printf("mbuf: size=%d, frames=%d, offsets=",camera->mbuf.size,
           camera->mbuf.frames);
#endif
    camera->memorymap = (char*)mmap(0,
                    camera->mbuf.size,
                    PROT_READ|PROT_WRITE, MAP_SHARED,
                    camera->device,
                    0);
        
    if((int)camera->memorymap == -1)
        {
        fprintf(stderr, "icvVideoStart: mmap failed");
        return 0;
    }

    // acquire first frame
    camera->vid_mmap.frame  = 0;
    camera->vid_mmap.width  = camera->videopp.width;
    camera->vid_mmap.height = camera->videopp.height;
    camera->vid_mmap.format = camera->videopp.picture.palette;
    camera->lastframe = 0;

    pthread_mutex_lock(&(camera->ioctlmutex));
    
    if(ioctl(camera->device, VIDIOCMCAPTURE, &(camera->vid_mmap)) < 0)
    {
        fprintf(stderr, "icvVideoInit: ioctl VIDIOCMCAPTURE failed \n");
        return 0;
    }
    if(ioctl(camera->device, VIDIOCSYNC, &(camera->vid_mmap.frame) ) < 0)
    {
        fprintf(stderr, "icvVideoInit: ioctl VIDIOCSYNC failed \n");
        return 0;
    }//if ioctl(..., VIDIOCSYNK, ...)
    pthread_mutex_unlock(&(camera->ioctlmutex));
        
    assert(camera->videopp.picture.palette);
    assert(icvVideoFormat2Depth(camera->imagepalette));
    
    pthread_mutex_lock(&(camera->capturestatemutex));
    camera->capturestate = CAPTURING;
    pthread_cond_signal(&(camera->capturestatecond));
    pthread_mutex_unlock(&(camera->capturestatemutex));

    // initialize frame locks.
    pthread_mutex_init(&(camera->lastframemutex), NULL);
    camera->framelock = (pthread_rwlock_t *)malloc(camera->mbuf.frames *
                                                   sizeof(pthread_rwlock_t));
    for(frame = 0; frame < camera->mbuf.frames; frame++)
    {
        pthread_rwlock_init(&(camera->framelock[frame]),NULL);
    }

    camera->updated = 1;

    // start capture thread
    if(pthread_create((&(camera->thread)),NULL,
                      icvVideoCaptureProc, (void*)cameraid))
    {
        fprintf(stderr, "icvVideoStart: couldn't start thread for "
                "camera %d\n", camera);
        return 0;
    }//if pthread_create ...
#ifdef DEBUG_THREADS
    printf("started icvVideoCaptureProc thread %p from thread %p\n",
           camera->thread, pthread_self());
#endif
    
    if(camera->rendered)
    {
#ifdef DEBUG_CVCAM
        printf("icvVideoStart: start rendering\n");
#endif
        icvVideoRenderStart(cameraid);
    }
    return 1;
}


////////////////////////////////////////////////////////////////////////////////
void* icvVideoCaptureProc(void* arg)
{
    int cameraid = (int) arg;
    CvVideoCamera* const camera = &(cameras[cameraid]);
    
    assert(cameras != NULL);
    assert(cameraid>=0 && cameraid <ncameras);
    assert(camera->mbuf.frames>0);
    pthread_mutex_lock(&(camera->capturestatemutex));
    while(camera->capturestate == CAPTURING)
    {
        pthread_mutex_unlock(&(camera->capturestatemutex));
#ifdef DEBUG_CVCAM
        printf("*");
        fflush(stdout);

⌨️ 快捷键说明

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