CameraDS类Directshow camera raw10

本文介绍了一个使用DirectShow进行视频捕获的示例程序。该程序由Shiqi Yu编写,能够获取连接到计算机上的摄像头数量及摄像头名称,并演示了如何打开摄像头、设置曝光并读取帧数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//////////////////////////////////////////////////////////////////////
// Video Capture using DirectShow
// Author: Shiqi Yu (shiqi.yu@gmail.com)
// Thanks to:
//      HardyAI@OpenCV China
//      flymanbox@OpenCV China (for his contribution to function CameraName, and frame width/height setting)
// Last modification: April 9, 2009
//////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// 使用说明:
//   1. 将CameraDS.h CameraDS.cpp以及目录DirectShow复制到你的项目中
//   2. 菜单 Project->Settings->Settings for:(All configurations)->C/C++->Category(Preprocessor)->Additional include directories
//      设置为 DirectShow/Include
//   3. 菜单 Project->Settings->Settings for:(All configurations)->Link->Category(Input)->Additional library directories
//      设置为 DirectShow/Lib
//////////////////////////////////////////////////////////////////////

#include "camerads.h"
#include <stdio.h>

int main()
{
    int cam_count;

    //仅仅获取摄像头数目
    cam_count = CCameraDS::CameraCount();
    printf("There are %d cameras.\n", cam_count);


    //获取所有摄像头的名称
    for(int i=0; i < cam_count; i++)
    {
        char camera_name[1024];  
        int retval = CCameraDS::CameraName(i, camera_name, sizeof(camera_name) );

        if(retval >0)
            printf("Camera #%d's Name is '%s'.\n", i, camera_name);
        else
            printf("Can not get Camera #%d's name.\n", i);
    }


    if(cam_count==0)
        return -1;

    CCameraDS camera;

    //打开第一个摄像头
    //if (!camera.OpenCamera(0, true)){} //弹出属性选择窗口
    if(! camera.OpenCamera(0, false, 640, 480)) //不弹出属性选择窗口,用代码制定图像宽和高
    {
        fprintf(stderr, "Can not open camera.\n");
        return -1;
    }

    int tt=0, ltt=0;
    cv::namedWindow("camera");
    int et = -13;
    while(1)
    {
        camera.SetExposure(et);
        //获取一帧
        IplImage *pFrame = camera.QueryFrame();
        //std::cout<<"size = "<<pFrame->imageSize<<std::endl;
        cv::Mat frame(480, 640, CV_16UC1, pFrame->imageData);
        cv::Mat grey(480, 640, CV_8UC1);
        uchar* p1 = frame.data;
        uchar* p2 = grey.data;
        tt = *(p1 + 638 * 2 + 1);
        //printf("%02x %02x : %02x-%02x-%02x-%02x %d\n", *(unsigned char*)(p1 + 636 * 2), *(unsigned char*)(p1 + 636 * 2 + 1), *(p1 + 637 * 2), *(p1 + 637 * 2 + 1), *(p1 + 638 * 2), *(p1 + 638 * 2 + 1), tt-ltt);
        ltt = tt;
        //std::cout << (p1 + 637 * 2) << (p1 + 637 * 2 + 1) << (p1 + 638 * 2) << (p1 + 638 * 2 + 1) << std::endl; // << " " << *(p1 + 640 * 480 * 2 - 49) << " " << *(p1 + 640 * 480 * 2 - 48) << " " << *(p1 + 640 * 480 * -47) << std::endl;
        for (size_t i = 0; i < 640*480; i++)
        {
            *p2 = (uchar)(*((ushort*)p1) >> 2);//10bit raw 去掉低2bit
            p2++;
            p1+=2;
        }
        cv::flip(grey, grey,1);
        //显示
        cv::imshow("camera", grey);
        et++;
        if (cvWaitKey(20) == 27)
            break;
    }
    camera.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头

    cvDestroyWindow("camera");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值