I am writing a grabber code for a Teledyne Dalsa Gige camera. I am using the Sapera Lt SDK and I am moving the image to OpenCV for further processing. When I want to show the image the legacy window is left behind with a random name
I try this solution but do not work.
#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);
}
}
can someone see what is my mistake?
I found the solution:
I commented on the window name:
//namedWindow("image", 1);
No more random windows.