无法使用带有 Python 3 的 See3CAM 读取图像并打开 CV

Cannot read image with See3CAM with Python 3 and open CV

我正在尝试使用 Python 3.4 和 OpenCV 从 e-Con Systems 的相机读取图像。相机使用 DirectShow 驱动程序,我可以连接到相机(isOpened returns true 并且相机上的状态 LED 处于活动状态)但是当我尝试读取或抓取帧时它不起作用。

import cv2
cam = cv2.VideoCapture(cv2.CAP_DSHOW + device)
cam.isOpened()  # returns true, camera LED on
flag, frame = cam.read() # flag=false, frame=None

我也试过像其他人所说的那样捕捉多帧,但还是不行!

从相机规格可以看出,相机输出格式为Y16,即16位单色格式。默认opencv/Direct show不支持这种格式!

为 See3CAM 自定义格式构建 OpenCV

这是构建支持自定义帧格式的 OpenCV 的分步过程。

  1. 下载OpenCV
  2. 下载已修改 cap_dshow.cpp

注意:此源代码已修改,仅支持 Y16 和 BY8 格式。

  1. Select 所需配置 - 用于构建 x86 或 x64 位库和构建 OpenCV official guide.

  2. 替换已有的cap_dshow.cpp

我。对于 OpenCV 2.x.x 路径:OpenCV/sources/modules/highgui/src

二.对于 OpenCV 3.x.x 路径:OpenCV/sources/modules/videoio/src

  1. 打开 OpenCV.sln 解决方案文件并构建 OpenCV 库。

这是一个示例 C++ 应用程序,用于流式传输具有 16 位图像格式的相机。我希望将它移植到 python 非常简单!希望这对您有所帮助!

#include "CTimer.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

int main( int, char** argv )
{
    const int iWidth=1280;
    const int iHeight=720;

    CTimer t_FPS;
    t_FPS.Init();

    VideoCapture Capture(0);

    if(!Capture.isOpened())
    {
        cout<< "Cannot Open Camera!";
        return 0;
    }
    Capture.set(CV_CAP_PROP_FRAME_WIDTH,iWidth);
    Capture.set(CV_CAP_PROP_FRAME_HEIGHT,iHeight);

    namedWindow("Camera NIR Frame",WINDOW_AUTOSIZE);

    Mat mCamFrame_10,mCamFrame_8;

    char key;
    for(;;)
    {

        Capture >> mCamFrame_10; // get a new frame from camera

        if(mCamFrame_10.empty()||mCamFrame_10.rows==0||mCamFrame_10.cols==0)
        {
            cout<< "Invalid Frame Frame Empty"<<"\n";
            break;
        }

        //Convert to 8 Bit: Scale the 10 Bit (1024) Pixels into 8 Bit(256) (256/1024)= 0.25
        convertScaleAbs(mCamFrame_10,mCamFrame_8,0.25);

        imshow("Camera NIR Frame", mCamFrame_8);

        t_FPS.Update();
        cout<<"Frame Rate: "<<t_FPS.GetFPS()<<"\n";
        key = cv::waitKey(1);
        if(key == 27)
        {
            //Esc Key is pressed & Exit the Application
            break;
        }
    }
    waitKey(0);
    return 0;
    // the camera will be deinitialized automatically in VideoCapture destructor
}

很久以前,我为 OpenCV 编写了一个补丁,它提供了对 Y16 编解码器的支持,因此您不再需要经历所有这些为 OpenCV 做 custom-compile 的痛苦。当时我已经通过电子邮件向 E-con 支持人员发送了有关此问题的信息,但是(截至目前 post)我看到他们没有尝试调整他们的工作方式。

Brandon Hurr / @bhive01 已经为 See3CAM_CU40 编写了一些 python 代码,您可以查看 over here。这与您使用 OpenCV 的 VideoCapture 界面的方式非常相似,只需使用 'Y','1','6','' 进行 FOURCC 设置,并将 CAP_PROP_CONVERT_RGB 设置为 false。

干杯!