【发布时间】:2015-09-11 16:00:28
【问题描述】:
我现在正在开发一个完全编写的数据采集工具 在 MATLAB 中。我的同事希望我用 MATLAB 写这个东西 以便他们可以扩展和修改它。
该软件需要从两个连接的 USB 相机中抓取图片。 这些相机的 API 是用 C++ 编写的,并记录在案 -> Here。
问题来了: 当我编写一个抓取图片的 mex 文件时,它包含 摄像机的初始化和配置加载 花费很长时间。当我想抓取图片时 这样,MATLAB 需要超过 1 秒的时间来执行任务。 一旦初始化,摄像机就能够以 100 fps 的速度记录和发送。 我需要的最低帧速率是 10 fps。 我需要能够发回每张录制的照片 到 MATLAB。因为录制会话 需要采集工具大约需要 12 小时,我们 需要一个带有一些轻微后处理的实时屏幕。
是否可以在 mex 文件中生成一个循环 向 MATLAB 发送数据,然后等待 MATLAB 的返回信号 并继续? 这样我可以初始化相机并定期发送 图像到 MATLAB。
我是 C++ 的初学者,我很可能 不明白一个基本概念为什么这样 是不可能的。
感谢您提供我可以查看的任何建议或资源。
请在下面找到初始化相机的代码 使用 Basler 提供的 Pylon API。
// Based on the Grab_MultipleCameras.cpp Routine from Basler
/*
This routine grabs one frame from 2 cameras connected
via two USB3 ports. It directs the Output to MATLAB.
*/
// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
#include <pylon/usb/PylonUsbIncludes.h>
#include <pylon/usb/BaslerUsbInstantCamera.h>
#include <pylon/PylonUtilityIncludes.h>
// Include Files for MEX Generation
#include <matrix.h>
#include <mex.h>
// Namespace for using pylon objects.
using namespace Pylon;
// We are lazy and use Basler USB namespace
using namespace Basler_UsbCameraParams;
// Standard namespace
using namespace std;
// Define Variables Globally to be remembered between each call
// Filenames for CamConfig
const String_t filenames[] = { "NodeMapCam1.pfs","NodeMapCam2.pfs" };
// Limits the amount of cameras used for grabbing.
static const size_t camerasToUse = 2;
// Create an array of instant cameras for the found devices and
// avoid exceeding a maximum number of devices.
CBaslerUsbInstantCameraArray cameras(camerasToUse);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system.
// is initialized during the lifetime of this object
PylonAutoInitTerm autoInitTerm;
try
{
// Get the transport layer factory
CTlFactory& tlFactory = CTlFactory::GetInstance();
// Get all attached devices and exit application if no device or USB Port is found.
DeviceInfoList_t devices;
ITransportLayer *pTL = dynamic_cast<ITransportLayer*>(tlFactory.CreateTl(BaslerUsbDeviceClass));
if (pTL == NULL)
{
throw RUNTIME_EXCEPTION("No USB transport layer available.");
}
if (pTL->EnumerateDevices(devices) == 0)
{
throw RUNTIME_EXCEPTION("No camera present.");
}
// Create and attach all Pylon Devices. Load Configuration
for (size_t i = 0; i < cameras.GetSize(); ++i)
{
cameras[i].Attach(tlFactory.CreateDevice(devices[i]));
}
// Open all cameras.
cameras.Open();
// Load Configuration and execute Trigger
for (size_t i = 0; i < cameras.GetSize(); ++i)
{
CFeaturePersistence::Load(filenames[i], &cameras[i].GetNodeMap());
}
if (cameras[0].IsOpen() && cameras[1].IsOpen())
{
mexPrintf("\nCameras are fired up and configuration is applied\n");
// HERE I WOULD LIKE TO GRAB PICTURES AND SEND THEM
// PERIODICALLY TO MATLAB.
}
}
catch (GenICam::GenericException &e)
{
// Error handling
mexPrintf("\nAn exception occured:\n");
mexPrintf(e.GetDescription());
}
return;
}
【问题讨论】: