C++ PvAPI OpenCV内置代码:FPS不匹配以及如何使用多个摄像头?

C++ PvAPI OpenCV内置代码:FPS不匹配以及如何使用多个摄像头?,c++,opencv,camera,computer-vision,visual-studio-2015,C++,Opencv,Camera,Computer Vision,Visual Studio 2015,我有两个曼塔G125B相机(B代表黑色,意思是单色)。这些是 Gige接口相机,我使用 PvAUI C++程序员接口使用微软Visual Studio社区2015 ID/读取相机数据到Windows操作系统笔记本电脑。 最近,我遇到了Steven Puttemans的github帐户,他在这里分享了代码AVT_Manta_opencv_builtin.cpp,链接如下: 我从以下站点下载了OpenCV 3.0.0源文件 itsez-github页面并使用CMake构建所有必需的文件(我在使用6

我有两个曼塔G125B相机(B代表黑色,意思是单色)。这些是<强> Gige接口<强>相机,我使用<强> PvAUI C++程序员接口使用微软Visual Studio社区2015 ID/<强>读取相机数据到Windows操作系统笔记本电脑。 最近,我遇到了Steven Puttemans的github帐户,他在这里分享了代码AVT_Manta_opencv_builtin.cpp,链接如下:

我从以下站点下载了OpenCV 3.0.0源文件 itsez-github页面并使用CMake构建所有必需的文件(我在使用64位CPU和MVS Community 2015时,在单击配置选项后,选择了使用默认本机编译器Visual Studio 14 2015 64选项)。在第一次配置之后,我选择了,并使用_PVAPI选项(实际上它已经被选中)我注意到,PVAPI_INCLUDE_PATHPVAPI_LIBRARY选项分别被自动正确识别为C:/Program Files/Allied Vision Technologies/GigESDK/inc pcC:/Program Files/Allied Vision Technologies/GigESDK/lib pc/x64/PVAPI.lib。我再次单击了配置选项,然后单击了生成选项(同时,如果我没有取消选择配置后已选择的BUILD\u PERF\u TESTSBUILD\u TESTS选项,当我打开OpenCV.sln和BUILDALL\u BUILDINSTALL时,Visual Studio会显示三个错误。我在BUILD\u PERF\TES删除了勾号。)T构建测试选项,错误消失)

在从头开始构建OpenCV之后,我做了一个Visual Studio项目,并稍微修改了Steven Puttemans的代码,以便在控制台上打印帧数和帧速率时看到从我的一台相机中实时采集的相机。以下是我的代码:

int main()
{
    Mat frame, imgResized;
    double f = 0.4; /* f is a scalar in [0-1] range that scales the raw image. Output image is displayed in the screen. */
    DWORD timeStart, timeEnd; // these variables are used for computing fps and avg_fps.
    double fps = 1.0; // frame per second
    double sum_fps(0.);
    double avg_fps(0.); // average fps
    int frameCount = 0;
    VideoCapture camera(0 + CV_CAP_PVAPI); /* open the default camera; VideoCapture is class, camera is object. */
    if (!camera.isOpened())
    {
        cerr << "Cannot open the camera." << endl;
        return EXIT_FAILURE;
    }
    double rows = camera.get(CV_CAP_PROP_FRAME_HEIGHT); /* Height of the frames in the video stream. */
    double cols = camera.get(CV_CAP_PROP_FRAME_WIDTH); /* Width of the frames in the video stream. */
    double exposure = camera.get(CV_CAP_PROP_EXPOSURE);
    cout << "Exposure value of the camera at the beginning is " << exposure << endl;
    double exposureTimeInSecond = 0.02; /* As exposureTimeInSecond variable decreases, fps should increase */
    exposure = exposureTimeInSecond * 1000000; /* esposure time in us */
    camera.set(CV_CAP_PROP_EXPOSURE, exposure);
    double frameRate; /* built-in fps */
    cout << "Frame size of the camera is " << cols << "x" << rows << "." << endl;
    cout << "Exposure value of the camera is set to " << exposure << endl;
    char* winname = "Manta Camera";
    namedWindow(winname, WINDOW_AUTOSIZE);
    cout << "Press ESC to terminate default camera." << endl;

    while (true)
    {
        timeStart = GetTickCount();
        camera >> frame;
        frameCount++;
        frameRate = camera.get(CV_CAP_PROP_FPS); /* Built-in frame rate in Hz. */
        /* resize() built-in function is in imgproc main module, in Geometric Image Transformations module. I resize the image by f (where f is a scalar in [0-1] range) for display. */
        resize(frame, imgResized, Size(), f, f, INTER_LINEAR); /* void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR) */
        imshow(winname, imgResized);
        moveWindow(winname, 980, 50);
        int key = waitKey(10);
        if (key == VK_ESCAPE)
        {
            destroyWindow(winname);
            break;
        }
        /* Calculating FPS in my own way. */
        timeEnd = GetTickCount();
        fps = 1000.0 / (double)(timeEnd - timeStart); /* 1s = 1000ms */
        sum_fps += fps;
        avg_fps = sum_fps / frameCount;
        cout << "FPS = " << frameRate << ", frame #" << frameCount << ", my_fps = " << fps << ", avg_fps = "<< avg_fps << endl;
    }

    cout << "Compiled with OpenCV version " << CV_VERSION << endl; /* thanks to Shervin Emami */
    system("pause");
    return EXIT_SUCCESS;
}
intmain()
{
垫架,尺寸不合适;
双f=0.4;/*f是[0-1]范围内的标量,用于缩放原始图像。输出图像显示在屏幕上*/
DWORD timeStart,TIMEND;//这些变量用于计算fps和avg_fps。
双fps=1.0;//每秒帧数
双和fps(0);
双平均fps(0);//平均fps
int frameCount=0;
VideoCapture摄像机(0+CV_CAP_PVAPI);/*打开默认摄像机;VideoCapture是类,摄像机是对象*/
如果(!camera.isOpened())
{

cerr我能够打开两个摄像头:

#包括
#include“Windows.h”/*在Windows.h中定义了数据类型DWORD和函数GetTickCount()*/
#包括“opencv2/opencv.hpp”
#包括
#包括//Parallel patterns library、并发名称空间。Young Jin和我都使用它。这非常重要;如果不使用,在处理图像时,fps会急剧下降。
#if!定义的VK_ESCAPE/*VK代表虚拟密钥*/
#为ESC字符定义VK_转义0x1B/*ASCII码为27*/
#恩迪夫
使用名称空间std;
使用名称空间cv;
const unsigned long numberOfCameras=2;
bool displayImages=true;
int main()
{
垫框[numberOfCameras],尺寸不合适[numberOfCameras];
双f=0.4;/*f是[0-1]范围内的标量,用于缩放原始图像。输出图像显示在屏幕上*/
DWORD timeStart,TIMEND;//这些变量用于计算fps和avg_fps。
双fps=1.0;//每秒帧数
双和fps(0);
双平均fps(0);//平均fps
int frameCount=0;
VideoCapture camera[numberOfCameras];/(0+CV_CAP_PVAPI);/*打开默认的摄像头;VideoCapture是类,摄像头是对象*/
对于(int i=0;i#include <iostream>
#include "Windows.h" /* data type DWORD and function GetTickCount() are defined in Windows.h */
#include "opencv2/opencv.hpp"
#include <cassert>
#include <ppl.h> // Parallel patterns library, concurrency namespace. Young-Jin uses this, so do I. It's very important; if not used, when images are processed, fps drops dramatically.
#if !defined VK_ESCAPE /* VK stands for virtual key*/
#define VK_ESCAPE 0x1B /* ASCII code for ESC character is 27 */
#endif

using namespace std;
using namespace cv;

const unsigned long numberOfCameras = 2;
bool displayImages = true;

int main()
{
    Mat frame[numberOfCameras], imgResized[numberOfCameras];
    double f = 0.4; /* f is a scalar in [0-1] range that scales the raw image. Output image is displayed in the screen. */
    DWORD timeStart, timeEnd; // these variables are used for computing fps and avg_fps.
    double fps = 1.0; // frame per second
    double sum_fps(0.);
    double avg_fps(0.); // average fps
    int frameCount = 0;
    VideoCapture camera[numberOfCameras]; // (0 + CV_CAP_PVAPI); /* open the default camera; VideoCapture is class, camera is object. */
    for (int i = 0; i < numberOfCameras; i++)
    {
        camera[i].open(i + CV_CAP_PVAPI);
        if (!camera[i].isOpened())
        {
            cerr << "Cannot open camera " << i << "." << endl;
            return EXIT_FAILURE;
        }
    }
    double rows = camera[0].get(CV_CAP_PROP_FRAME_HEIGHT); /* Height of the frames in the video stream. */
    double cols = camera[0].get(CV_CAP_PROP_FRAME_WIDTH); /* Width of the frames in the video stream. */
    if (numberOfCameras == 2)
        assert(rows == camera[1].get(CV_CAP_PROP_FRAME_HEIGHT) && cols == camera[0].get(CV_CAP_PROP_FRAME_WIDTH));
    for (int i = 0; i<numberOfCameras; i++) // initializing monochrome images.
    {
        frame[i] = Mat(Size(cols, rows), CV_8UC1); /* Mat(Size size, int type) */
        resize(frame[i], imgResized[i], Size(0, 0), f, f, INTER_LINEAR);
    }
    /* combo is a combined image consisting of left and right resized images. images are resized in order to be displayed at a smaller region on the screen. */
    Mat combo(Size(imgResized[0].size().width * 2, imgResized[0].size().height), imgResized[0].type()); /* This is the merged image (i.e., side by side) for real-time display. */
    Rect roi[numberOfCameras]; /* roi stands for region of interest. */
    for (int i = 0; i < numberOfCameras; i++)
        roi[i] = Rect(0, 0, imgResized[0].cols, imgResized[0].rows);
    /* Setting locations of images coming from different cameras in the combo image. */
    if (numberOfCameras > 1) /* I assume max camera number is 2. */
    {
        roi[1].x = imgResized[0].cols;
        roi[1].y = 0;
    }

    double exposure, exposureTimeInSecond = 0.06; /* As exposureTimeInSecond variable decreases, fps should increase */
    for (int i = 0; i < numberOfCameras; i++)
    {
        exposure = camera[i].get(CV_CAP_PROP_EXPOSURE);
        cout << "Exposure value of the camera " << i << " at the beginning is " << exposure << endl;
        exposure = exposureTimeInSecond * 1000000; /* esposure time in us */
        camera[i].set(CV_CAP_PROP_EXPOSURE, exposure);
    } 
    double frameRate[numberOfCameras]; /* built-in fps */
    cout << "Frame size of the camera is " << cols << "x" << rows << "." << endl;
    cout << "Exposure value of both cameras is set to " << exposure << endl;
    char* winname = "real-time image acquisition";
    if (displayImages)
        namedWindow(winname, WINDOW_AUTOSIZE);
    cout << "Press ESC to terminate real-time acquisition." << endl;

    while (true)
    {
        timeStart = GetTickCount();
        Concurrency::parallel_for((unsigned long)0, numberOfCameras, [&](unsigned long i)
        {
            camera[i] >> frame[i];
            frameRate[i] = camera[i].get(CV_CAP_PROP_FPS); /* Built-in frame rate in Hz. */
            resize(frame[i], imgResized[i], Size(), f, f, INTER_LINEAR); /* void cv::resize(InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR) */
            imgResized[i].copyTo(combo(roi[i])); /* This is C++ API. */
        });
        frameCount++;
        if (displayImages)
        {
            imshow(winname, combo);
            moveWindow(winname, 780, 50);
        }
        int key = waitKey(10);
        if (key == VK_ESCAPE)
        {
            destroyWindow(winname);
            break;
        }
        /* Calculating FPS in my own way. */
        timeEnd = GetTickCount();
        fps = 1000.0 / (double)(timeEnd - timeStart); /* 1s = 1000ms */
        sum_fps += fps;
        avg_fps = sum_fps / frameCount;
        for (int i = 0; i < numberOfCameras; i++)
            cout << "FPScam" << i << "=" << frameRate[i] << " ";
        cout << "frame#" << frameCount << " my_fps=" << fps << " avg_fps=" << avg_fps << endl;
    }

    cout << "Compiled with OpenCV version " << CV_VERSION << endl; /* thanks to Shervin Emami */
    system("pause");
    return EXIT_SUCCESS;
}


// double triggerMode = camera.get(CV_CAP_PROP_PVAPI_FRAMESTARTTRIGGERMODE);
////camera.set(CV_CAP_PROP_PVAPI_FRAMESTARTTRIGGERMODE, 4.0);
//
//if (triggerMode == 0.)
//cout << "Trigger mode is Freerun" << endl;
//else if (triggerMode == 1.0)
//cout << "Trigger mode is SyncIn1" << endl;
//else if (triggerMode == 2.0)
//cout << "Trigger mode is SyncIn2" << endl;
//else if (triggerMode == 3.0)
//cout << "Trigger mode is FixedRate" << endl;
//else if (triggerMode == 4.0)
//cout << "Trigger mode is Software" << endl;
//else
//cout << "There is no trigger mode!!!";