Issues with Properly exiting camera

10 次查看(过去 30 天)
Bera
Bera 2025-1-28
回答: Darshak 2025-6-23
Hey All
I have a uEye 148x camera. Essentially I am doing all the proper deleting and clearing but when I open the app back up it says no device connected. This only happens when it thinks Matlab or python is still using it. I have been able to exit the camera and have the app work via Python but I haven't had much luck with properly exiting via Matlab.
This is my code
% -------------------- INITIALIZE CAMERA --------------------
clc; clear; close all;
% Set up camera
info = imaqhwinfo;
if isempty(info.InstalledAdaptors)
error('No cameras found. Connect a camera and try again.');
end
adaptor = info.InstalledAdaptors{1};
camInfo = imaqhwinfo(adaptor);
deviceID = camInfo.DeviceInfo(1).DeviceID;
vid = videoinput(adaptor, deviceID, camInfo.DeviceInfo(1).SupportedFormats{1});
src = getselectedsource(vid);
% Set camera parameters
% vid.FramesPerTrigger = 1;
% src.Exposure = -6;
% src.Gain = 10;
% adding an itial delay to make sure everything is working as expected
pause(1)
preview(vid)
% Capture initial image
img = getsnapshot(vid);
imshow(img)
closepreview(vid)
stop(vid)
delete(vid)
clear vid
Any help would be much appreciated

回答(1 个)

Darshak
Darshak 2025-6-23
Hey @Bera,
This is a tricky situation that comes up often with uEye (IDS Imaging) cameras when interfacing with MATLAB. Even though everything looks correctly shut down in the code, the underlying driver or hardware handle may remain active, which leads to the camera not being released properly. This can be frustrating, especially when trying to switch between MATLAB and the uEye application.
A few things to try that have worked in similar setups:
  • Make sure that you are not just stopping and deleting the object, but also fully resetting the Image Acquisition Toolbox state.
  • MATLAB sometimes keeps internal references to hardware drivers even after the video object is cleared.
  • The “imaqreset” function is often the key step that's missing in many shutdown scripts.
Below is a general structure that tends to release the hardware properly when using MATLAB with uEye cameras:
clc; clear; close all;
info = imaqhwinfo;
if isempty(info.InstalledAdaptors)
error('No image acquisition adaptors installed.');
end
adaptor = 'ueye';
camInfo = imaqhwinfo(adaptor);
deviceID = camInfo.DeviceInfo(1).DeviceID;
vid = videoinput(adaptor, deviceID, camInfo.DeviceInfo(1).SupportedFormats{1});
src = getselectedsource(vid);
pause(1);
preview(vid);
pause(1);
img = getsnapshot(vid);
imshow(img);
pause(1);
closepreview(vid);
stop(vid);
flushdata(vid);
delete(vid);
clear vid;
imaqreset;
A few things worth noting:
  • “flushdata” (vid) clears any internal frame buffers before deletion.
  • “imaqreset” is critical as it resets all adaptors and releases any lingering device locks or handles, which is likely what the uEye application is detecting.
  • Sometimes it helps to avoid “clear all” as it doesn't reset hardware connections like “imaqreset” does.
Also make sure MATLAB is not minimized or running a background figure or camera preview window when attempting the release, as these can sometimes keep the device context alive.
The following documentation links can be referred to for a better understanding:
There may be deeper OS-level device locks, but this usually resolves the common usage conflict.

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by