MATLAB: Controlling uEye µEye camera in Matlab with shared library by loadlibrary

cameradllloadlibrarypointerssamplesample codeshared libraryueyeµeye

Hi everbody!
I'm trying to control a uEye camera with Matlab. I don't want to do anything fancy but this is the first time I'm doing such a thing so I'm kind of stuck right now.
So far I have tried to use the loadlibrary(…) functionality with the installed dll-file but always ended up in a mess of errors and warnings. I think my main problem is the use of pointers in Matlab which is necessary for some of the library's functions.
Does anybody have experience with that kind of programming? Or can anybody provide me with a sample code?
Thank you for your help!

Best Answer

The issue is not quite to do with handling pointers - Matlab does that quite well when connecting to DLLs - you have to pass a dummy variable in place of the pointer and then matlab appends the result to the return parameters:
e.g.
int GrabImage(uint8 *data);
becomes
[ret, data] = GrabImage(zeros(SizeVectorOfImage));
where the original function returns a success/failure error code. I believe the dummy variable needs to have allocated sufficient memory to hold the data, but I cannot remember exactly - its been a long time since I used this approach.
The actual problem comes from linking with the other libraries and using non-native data types. In general, I found one has to write a wrapper dll, or a mex file to interface between Matlab and the uEye DLL. You can then use this to allocated memory and help speed things up. However, this gets cumbersome as you have to implement each function manually.
Alternatively you can access the .NET assembly directly, although you have to work around the fact that Matlab does not use pointers. Here is an example:
Remember to "EXIT" your camera before trying to re-initialise the same camera
% Add NET assembly if it does not exist
% May need to change specific location of library
asm = System.AppDomain.CurrentDomain.GetAssemblies;
if ~any(arrayfun(@(n) strncmpi(char(asm.Get(n-1).FullName), ...
'uEyeDotNet', length('uEyeDotNet')), 1:asm.Length))
NET.addAssembly(...
'C:\Program Files\IDS\uEye\Develop\DotNet\signed\uEyeDotNet.dll');
end
% Create camera object handle
cam = uEye.Camera;
% Open 1st available camera
% Returns if unsuccessful
if ~strcmp(char(cam.Init), 'SUCCESS')
error('Could not initialize camera');
end
% Set display mode to bitmap (DiB)
if ~strcmp(char(cam.Display.Mode.Set(uEye.Defines.DisplayMode.DiB)), ...
'SUCCESS')
error('Could not set display mode');
end
% Set colormode to 8-bit RAW
if ~strcmp(char(cam.PixelFormat.Set(uEye.Defines.ColorMode.SensorRaw8)), ...
'SUCCESS')
error('Could not set pixel format');
end
% Set trigger mode to software (single image acquisition)
if ~strcmp(char(cam.Trigger.Set(uEye.Defines.TriggerMode.Software)), 'SUCCESS')
error('Could not set trigger format');
end
% Allocate image memory
[ErrChk, img.ID] = cam.Memory.Allocate(true);
if ~strcmp(char(ErrChk), 'SUCCESS')
error('Could not allocate memory');
end
% Obtain image information
[ErrChk, img.Width, img.Height, img.Bits, img.Pitch] ...
= cam.Memory.Inquire(img.ID);
if ~strcmp(char(ErrChk), 'SUCCESS')
error('Could not get image information');
end
% Acquire image

if ~strcmp(char(cam.Acquisition.Freeze(true)), 'SUCCESS')
error('Could not acquire image');
end
% Extract image

[ErrChk, tmp] = cam.Memory.CopyToArray(img.ID);
if ~strcmp(char(ErrChk), 'SUCCESS')
error('Could not obtain image data');
end
% Reshape image

img.Data = reshape(uint8(tmp), [img.Width, img.Height, img.Bits/8]);
% Draw image

himg = imshow(img.Data, 'Border', 'tight');
% Acquire & draw 100 times
for n=1:100
% Acquire image
if ~strcmp(char(cam.Acquisition.Freeze(true)), 'SUCCESS')
error('Could not acquire image');
end
% Extract image
[ErrChk, tmp] = cam.Memory.CopyToArray(img.ID);
if ~strcmp(char(ErrChk), 'SUCCESS')
error('Could not obtain image data');
end
% Reshape image
img.Data = reshape(uint8(tmp), [img.Width, img.Height, img.Bits/8]);
% Draw image
set(himg, 'CData', img.Data);
drawnow;
end
% Close camera
if ~strcmp(char(cam.Exit), 'SUCCESS')
error('Could not close camera');
end