MATLAB: Preview video feed using surf

image acquisitionpreviewsurfvideo processing

Hi all,
I've got a gige camera and using Matlab to create a gui with some controlling functions/processing buttons for the camera images.
Using the following
Image = preview(VidObj);
ExtractedFrame = Image.CData;
I've been able to get the video feed to plot as 3D data which is what I'm looking for.
However, the preview window (small window, also showing the video stream) is always present and tends to be dominant in activity.
This is OK for the most but I think that because it's always visible (it's the active window on top of my gui figure), it's taking away control from my GUI.
I've added sliders in to control the viewing angle (which would be nicer to use the "surf" rotate by clicking and spinning the image, but again it works for what I need.
Where I'm struggling is adjusting camera exposure – it's presently set up as a
uicontrol('Style','edit', ...)
which is ideally how I'd like it, but the VideoPreview window always keeps the focus, so I can't actually highlight the exposure time to 'edit' the value (can't get the GUI active for long enough)
But to have the gui with additional buttons and controls is where I'm headed, and using preview(VidObj) only lets this be a 2D image (where I'd like to see peaks as the attached screenshot shows)
One can also see the controls I have – change colormap ('rainbow' = jet), control min & max Z, exposure, then the sliders to control the 'view' of the surf plot of the image.
As I'm also doing more image processing, I can export the image displayed (at the bottom), find camera values above certain Z (peak detection) and apply Z thresholding
Z(Z<Thresh)=0
by pressing the buttons at the bottom left of the GUI
I'm planning to add more, but tidying the surf(Image) from preview(VidObj) will probably be the quickest way to handle this (and further issues?!)
I include my main code also which is handling the camera acquisition
function DisplayCameraFeed(~, ~)
VidObj = videoinput('gige');
VidObj.SelectedSourceName = 'input1';
[~] = preview(VidObj); % Important to initialise camera!
while GUI_Handles.Toggle_MAIN.Value
NthFrame = NthFrame + 1;
%% This section gets the camera settings
VideoProperties = get(VidObj);
CameraProperties = getselectedsource(VidObj); % Added 20191204; to access features like camera exposure, etc
% VideoProperties.Previewing = 'off'; % TRIED TO REMOVE THE DOMINANT PREVIEW WINDOW, didn't work!
CameraProperties.ExposureTime = str2double(GUI_Handles.Text_SetExposure.String); % Extract the handle (it's a string) from the exposure
%% Now we're ready to get the image from the camera, and do any potential processing
Image = preview(VidObj);
ExtractedFrame = Image.CData;
% Check that surf/mesh will be able to display the image OK (greyscale)
switch size(ExtractedFrame, 3)
case 1 % No change needed
case 3 % Make RGB2Gray
ExtractedFrame = rgb2gray(ExtractedFrame);
otherwise % Oh dear!
keyboard;
end
GUI_Handles.GlobalCell{2, 4} = ExtractedFrame; % Store image
if GUI_Handles.Toggle_Slicing.Value % Only want the sliced image above the threshold
Toggle_Slicing;
ExtractedFrame = GUI_Handles.GlobalCell{2, 4};
end
%% Displaying of the image.
surf(flipud(ExtractedFrame)); % Matlab flips for surf plot
shading flat;
axis tight;
SetColorMap;
SliderCheck;
AXES = gca;
AXES.Position = [0.25, 0.25, 0.7, 0.7];
AXES.XAxis.Visible = 'off';
AXES.YAxis.Visible = 'off';
AXES.CLim = [GUI_Handles.Slider_Z_Min.Value, GUI_Handles.Slider_Z_Max.Value];
AXES.ZLim = [GUI_Handles.Slider_Z_Min.Value, GUI_Handles.Slider_Z_Max.Value];
AXES.View = [GUI_Handles.Slider_Angle_Azimuth.Value, GUI_Handles.Slider_Angle_Elevation.Value];
AXES.XLabel.String = sprintf('X');
AXES.YLabel.String = sprintf('Y');
AXES.XGrid = 'off';
AXES.YGrid = 'off';
AXES.ZGrid = 'off';
%% Add peak detection:
drawnow;
hold on;
Toggle_PeakDetector;
% drawnow;
hold off;
end
stoppreview(VidObj)
end
Thanks for your help 🙂

Best Answer

Sorted - manual setting, see line around 10 below. Just needed to do some more digging for anyone interested.
The below allows me to debug on macbook pro (facetime camera) and use on linux with the gige camera. Frame rate a bit shower than we'd like (6fps) but very usable.
SOLUTION:
function DisplayCameraFeed(~, ~)
NthFrame = 0;
if ismac
VidObj = videoinput('macvideo'); % 20191204 - added Mac camera use. This should allow debugging of all features (including exposure, etc)
else
VidObj = videoinput('gige');
end
VidSrc = getselectedsource(VidObj);
triggerconfig(VidObj, 'manual'); % 20191205; https://uk.mathworks.com/help/imaq/examples/acquiring-a-single-image-in-a-loop.html
start(VidObj);
while GUI_Handles.Toggle_MAIN.Value
if ~mod(NthFrame, 3)
StartTimer = tic;
end
if ~contains(VidObj.Name, 'macvideo')
ExposureTime = str2double(GUI_Handles.Input_SetExposure.String);
VidSrc.ExposureTime = ExposureTime;
end
NthFrame = NthFrame + 1;
ExtractedFrame = getsnapshot(VidObj);
switch size(ExtractedFrame, 3)
case 1 % No change needed
case 3 % Make RGB2Gray
ExtractedFrame = rgb2gray(ExtractedFrame);
otherwise % Oh dear!
keyboard;
end
if GUI_Handles.Toggle_Slicing.Value % Only want the sliced image above the threshold
GUI_Handles.GlobalCell{2, 4} = ExtractedFrame;
Toggle_Slicing;
ExtractedFrame = GUI_Handles.GlobalCell{2, 4};
end
%% Displaying of the image.
SetColorMap;
if GUI_Handles.Slider_Angle_Elevation.Value == 90
imagesc(ExtractedFrame);
AXES = gca;
else
surf(flipud(ExtractedFrame)); % Matlab flips for surf plot
shading flat;
axis tight;
AXES = gca;
AXES.ZLim = [GUI_Handles.Slider_Z_Min.Value, GUI_Handles.Slider_Z_Max.Value];
end
SliderCheck;
AXES.Position = [0.25, 0.25, 0.7, 0.7];
AXES.XAxis.Visible = 'off';
AXES.YAxis.Visible = 'off';
AXES.CLim = [GUI_Handles.Slider_Z_Min.Value, GUI_Handles.Slider_Z_Max.Value];
AXES.View = [GUI_Handles.Slider_Angle_Azimuth.Value, GUI_Handles.Slider_Angle_Elevation.Value];
AXES.XLabel.String = sprintf('X');
AXES.YLabel.String = sprintf('Y');
AXES.XGrid = 'off';
AXES.YGrid = 'off';
AXES.ZGrid = 'off';
SetColorMap;
CBAR = colorbar;
drawnow;
%% Add peak detection:
if GUI_Handles.Toggle_PeakDetector.Value
hold on;
Toggle_PeakDetector;
hold off;
end
if ~mod(NthFrame, 3)
GUI_Handles.GlobalCell{2, 4} = ExtractedFrame;
TimeTakenForNFrames = toc(StartTimer);
TimePerFrame = TimeTakenForNFrames / 3;
FrameRate = 1 / TimePerFrame;
set(GUI_Handles.Label_FrameRate, 'String',sprintf('Frame Rate = %0.2f', FrameRate));
end
end
stop(VidObj);
end