我正在为 Teledyne Dalsa Gige 相机编写采集器代码。我正在使用 Sapera Lt SDK 并将图像移动到 OpenCV 以进行进一步处理。当我想显示图像时,遗留窗口会留下一个随机名称
我尝试了这个解决方案,但不起作用。
#include "conio.h"
#include "math.h"
#include "sapclassbasic.h"
#include "ExampleUtils.h"
#include <opencv2/opencv.hpp>
// Restore deprecated function warnings with Visual Studio 2005
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(default: 4995)
#endif
// Static Functions
using namespace cv;
static void XferCallback(SapXferCallbackInfo *pInfo);
static BOOL GetOptions(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex, char *configFileName);
static BOOL GetOptionsFromCommandLine(int argc, char *argv[], char *acqServerName, UINT32 *pAcqDeviceIndex, char *configFileName);
void ExportToOpenCV_Direct(SapBuffer* pSapBuf);
int main(int argc, char* argv[])
{
UINT32 acqDeviceNumber;
char* acqServerName = new char[CORSERVER_MAX_STRLEN];
char* configFilename = new char[MAX_PATH];
printf("Sapera Console Grab Example (C++ version)\n");
// Call GetOptions to determine which acquisition device to use and which config
// file (CCF) should be loaded to configure it.
// Note: if this were an MFC-enabled application, we could have replaced the lengthy GetOptions
// function with the CAcqConfigDlg dialog of the Sapera++ GUI Classes (see GrabMFC example)
acqServerName = "Nano-C1940_1";
acqDeviceNumber = 0;
configFilename = "NoFile";
SapAcquisition Acq;
SapAcqDevice AcqDevice;
SapBufferWithTrash Buffers;
SapTransfer AcqToBuf = SapAcqToBuf(&Acq, &Buffers);
SapTransfer AcqDeviceToBuf = SapAcqDeviceToBuf(&AcqDevice, &Buffers);
SapTransfer* Xfer = NULL;
SapView View;
SapLocation loc(acqServerName, acqDeviceNumber);
if (SapManager::GetResourceCount(acqServerName, SapManager::ResourceAcq) > 0)
{
Acq = SapAcquisition(loc, configFilename);
Buffers = SapBufferWithTrash(2, &Acq);
View = SapView(&Buffers, SapHwndAutomatic);
AcqToBuf = SapAcqToBuf(&Acq, &Buffers, XferCallback, &View);
Xfer = &AcqToBuf;
// Create acquisition object
if (!Acq.Create())
goto FreeHandles;
}
else if (SapManager::GetResourceCount(acqServerName, SapManager::ResourceAcqDevice) > 0)
{
if (strcmp(configFilename, "NoFile") == 0)
AcqDevice = SapAcqDevice(loc, FALSE);
else
AcqDevice = SapAcqDevice(loc, configFilename);
Buffers = SapBufferWithTrash(2, &AcqDevice);
View = SapView(&Buffers, SapHwndAutomatic);
AcqDeviceToBuf = SapAcqDeviceToBuf(&AcqDevice, &Buffers, XferCallback, &View);
Xfer = &AcqDeviceToBuf;
// Create acquisition object
if (!AcqDevice.Create())
goto FreeHandles;
}
// Create buffer object
if (!Buffers.Create())
goto FreeHandles;
// Create transfer object
if (Xfer && !Xfer->Create())
goto FreeHandles;
// Create view object
if (!View.Create())
goto FreeHandles;
// Start continous grab
Xfer->Grab();
printf("Press any key to stop grab\n");
CorGetch();
// Stop grab
Xfer->Freeze();
if (!Xfer->Wait(5000))
printf("Grab could not stop properly.\n");
FreeHandles:
//unregister the acquisition callback
Acq.UnregisterCallback();
// Destroy view object
if (!View.Destroy()) return FALSE;
// Destroy transfer object
if (Xfer && *Xfer && !Xfer->Destroy()) return FALSE;
// Destroy buffer object
if (!Buffers.Destroy()) return FALSE;
// Destroy acquisition object
if (!Acq.Destroy()) return FALSE;
// Destroy acquisition object
if (!AcqDevice.Destroy()) return FALSE;
return 0;
}
static void XferCallback(SapXferCallbackInfo *pInfo)
{
SapView *pView = (SapView *)pInfo->GetContext();
// refresh view
pView->Show();
SapBuffer* Buffer_View = (pView->GetBuffer());
ExportToOpenCV_Direct(Buffer_View);
}
void ExportToOpenCV_Direct(SapBuffer* pSapBuf)
{
if (pSapBuf == NULL)
return;
SapFormat sapFormat = pSapBuf->GetFormat();
int OpenCV_Type = 0;
OpenCV_Type = CV_8UC3;
if (sapFormat != SapFormatUnknown)
{
// Export to OpenCV Mat object using SapBuffer data directly
void* pBuf = NULL;
pSapBuf->GetAddress(&pBuf);
Mat exportImg(pSapBuf->GetHeight(), pSapBuf->GetWidth(), OpenCV_Type, pBuf);
namedWindow("image", 1);
// Display OpenCV Image
imshow("image", exportImg);
pSapBuf->ReleaseAddress(&pBuf);
waitKey(1);
}
}
有人可以看到我的错误吗?