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

12 views (last 30 days)
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!

Accepted Answer

Adam Wyatt
Adam Wyatt on 9 Dec 2014
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
  3 Comments
Will Reeves
Will Reeves on 13 May 2022
Thanks for the code... I just get an error on the first command "cam.Init" with the "IS_DEVICE_IN_USE" return value (it isn't in use... I unplug and replug just before running the code. Is something else possibly grabbing the camera?)
Jun
Jun ungefär 2 timmar ago
Edited: Jun ungefär 2 timmar ago
hi Adam,
the code works with 'SensorRaw8' being 'Mono8' instead. Do you have any idea how to make it work in rgb mode? I tried 'SensorRaw8', the image seems to be close to correct but with some weird grid of darker pixels.
%%%%%%%%%%%%%%
Just found out the solution for my ids camera,
when set in Mono8 mode (grayscale), the reshape works in the following code:
img.Data = reshape(uint8(tmp), [img.Width, img.Height, img.Bits/8]);
To be able to get RGB image, switch Mono8 to RGB8Packed:
cam.PixelFormat.Set(uEye.Defines.ColorMode.RGB8Packed)
and the reshape has to be the following process:
tempData = reshape(uint8(tmp), [img.Bits/8,img.Width,img.Height]);
img.Data = permute(tempData, [2 3 1]);
if not this way the data is reshaped in wrong order. You might need to play with the reshape and permute parameters to make your ids camera work correctly.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for USB Webcams in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!