Controlling uEye µEye camera in Matlab with shared library by loadlibrary
12 views (last 30 days)
Show older comments
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!
1 Comment
Adam Wyatt
on 4 Jun 2015
Edited: Adam Wyatt
on 7 Dec 2016
Have a look at my blog for information on how to do this:
Accepted Answer
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
2 Comments
Yao Qiu
on 24 Sep 2019
Hello, I am using the .NET framework with MATLAB to program the IDS ueye camera, but now I have problems with following issues:
I want to do the subsampling both in horizontal and vertical orientation, but from the uEye .NET manual, there is no options for subsampling in both orientation:
uEye.Defines.SubsamplingMode.Vertical2X
uEye.Defines.SubsamplingMode.Horizontal2X
uEye.Defines.SubsamplingMode.Vertical3X
uEye.Defines.SubsamplingMode.Horizontal3X
uEye.Defines.SubsamplingMode.Vertical4X
uEye.Defines.SubsamplingMode.Horizontal4X
uEye.Defines.SubsamplingMode.Vertical5X
uEye.Defines.SubsamplingMode.Horizontal5X
uEye.Defines.SubsamplingMode.Vertical6X
uEye.Defines.SubsamplingMode.Horizontal6X
IuEye.Defines.SubsamplingMode.Vertical8X
uEye.Defines.SubsamplingMode.Horizontal8X
uEye.Defines.SubsamplingMode.Vertical16X
uEye.Defines.SubsamplingMode.Horizontal16X
uEye.Defines.SubsamplingMode.Disable
It says that the two parameters Vertical and Horizontal can be linked by logical OR, I tried to do that but failed and here is the error:
cam.Size.Subsampling.Set(uEye.Defines.SubsamplingMode.Vertical4X|uEye.Defines.SubsamplingMode.Horizontal4X);
Error : Undefined operator '|' for input arguments of type 'uEye.Defines.SubsamplingMode'.
So do you know how to solve this problem? I am looking foward to you answer and I would be very appreciated about that
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?)
More Answers (0)
See Also
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!