Problem getting Mat from GigE camera (Sapera SDK)
Hi,
I am trying to get Sapera SDK SapBuffer object (Genie NANO GIgE camera) as a cv::Mat. From documentation for this sdk I know that "The SapBuffer Class includes the functionality to manipulate an array of buffer resources."
From SDK I know that in "SapBuffer* Buffers" there it is 1024x1280 image type: 32 Depth: 8. I am not sure if I am correctly acquiring Mat object (rest of the code is from example for this sdk):
SapLocation loc(serverName,0);
Acq = new SapAcquisition(loc, FALSE); //ETHERNET came doesnt work with this?
AcqDevice = new SapAcqDevice(loc, FALSE);
Buffers = new SapBufferWithTrash(2, AcqDevice);
View = new SapView(Buffers, SapHwndAutomatic);
Xfer = new SapAcqDeviceToBuf(AcqDevice, Buffers, AcqCallback, View);
// Create acquisition object
if (AcqDevice && !*AcqDevice && !AcqDevice->Create())
goto FreeHandles;
}
// Create buffer object
if (Buffers && !*Buffers && !Buffers->Create())
goto FreeHandles;
// Create transfer object
if (Xfer && !*Xfer && !Xfer->Create())
goto FreeHandles;
// Create view object
if (View && !*View && !View->Create())
goto FreeHandles;
// Start continous grab
Xfer->Grab();
src1 = new Mat(1024, 1280, CV_8U, Buffers);
std::cout << "Total(): " << src1->total() << std::endl;
//Buffers->GetAddress(pData);
imwrite("E:\OpenCV\Gray_Image.jpg",*src1);
//delete mat;
delete src1;
printf("Press any key to stop grab\n");
CorGetch();
// Stop grab
Xfer->Freeze();
if (!Xfer->Wait(5000))
printf("Grab could not stop properly.\n");
Its part of an example and my code is only Mat *src1 = NULL; and OpenCv part. After running it shows "The program stopped running".
Please help.
1024x1280 and type 32 depth 8 shouldn't be src1 = new Mat(1024, 1280, CV_8UC(4), Buffers); please don't use pointer for image you can do like this
if you are sure that it is 8 bits depth
some thoughts on this:
Thanks. I will bare it in mind but I get an error C2362 that initialization was skipped due to "gorto FreeHandles" (part that deletes all the pointers after closing the stream). After putting the code just before deleting Buffers by FreeHandlesThe src1.empty() gives me 0 src1.total()=1310720, but imwrite() makes the program stopping to work.
Ok i see that even printing src1 to command line stops the program eventually. I am guessing that I need to look more into the API and create my own code.
You should explicitly copy Buffer's data:
Thanks, but it did not help much. I have studied the code a little bit more and I know that after Xfer->Freeze(); is called (stops Grabbing new frames) I can use APIs own Buffers->Save("image3.bmp", "-format bmp", -1, 0) to save the image from camera. After reading it into OpenCV i find that it is 3 channel type 16 with depth =0 - CV_8UC3 if I am not mistaken. That said I am still not able to get data from "Buffer" into the Mat. I am confused. Should I look for some operation like: BOOL Read(UINT64 offset, int numElements, void* pData); that I can perform on "Buffers" to be able to read image into Mat?
Have you got a doc link?
Sure. https://drive.google.com/open?id=0Bwc... I've put it on google disk. Hope you can open it. SapBuffer class is described on page 76..
have you try demo/example given to display image ?
Yes I have. I did not find anything useful on how to transfer data from SapBuffer object anywhere else. I am not experienced in C++ but so far I know that "Buffers" is of type SapBuffer and it is not what I can input to my Mat. I can use Buffers.GetAddress() to "Initiates direct address to buffer resource data by a pointer" (returns void* pData pointer) and GetSpaceUsed() to "Gets the number of data bytes actually stored in a buffer resource" (returns int pSpaceUsed). Is there a way to read this bytes from memory into a C++ unsigned char array and then to OpenCV Mat ?Or is it a wrong approach altogether ?