Preview video feed using surf

3 views (last 30 days)
Pete
Pete on 4 Dec 2019
Answered: Pete on 6 Dec 2019
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 :)
  1 Comment
Pete
Pete on 4 Dec 2019
Edited: Pete on 4 Dec 2019
I'm still looking into this and wondering if I can use something like
ExtractedFrame = getdata(VidObj)
or
ExtractedFrame = peekdata(VidObj, 1)
to constantly be drip-feeding my GUI with images from the live streaming?
But in trying to implement these, I've not been successful on the final aim!
getsnapshot(VidObj)
is almost there, but I don't think it's responsive enough for what I'm looking to do (in the code above, I've also changed lines 12 & 13 using
Image=preview(vidobj)
ExtractedImage = Image.CData
to
start(VidObj)
Again, any more help on this would be appreciated!

Sign in to comment.

Accepted Answer

Pete
Pete on 6 Dec 2019
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!