Rotate video from camera preview

25 views (last 30 days)
PaoloB
PaoloB on 11 Mar 2015
Answered: Guoheng Zhao on 25 Jun 2019
n Matlab I can get a preview from a camera with the command: preview(obj,himage) that puts the preview stream into himage.
In my code I have an axes that contains the video preview but I would like to rotate the video by 90 degrees. This might be very stupid but I cannot figure out how is done.
My code is the following:
prevHaxes = axes('Parent', mainFig);
previewImage= imagesc(placeholderImg,'Parent', prevHaxes); %Placeholder for when the stream is off
preview(video_obj, previewImage);
I have tried to use imrotate but I got the following error:
"Expected input number 1, input image, to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image."
So my question is, is there a way to get the video preview from a camera but to visualize it rotated by an angle?

Accepted Answer

PaoloB
PaoloB on 13 Mar 2015
All right,
found a way and it is posted here for future reference. Assuming "placeholderImg" is the image where the preview will be displayed, it is possible to set a function that will intercept all frames. The function is UpdatePreviewWindowFcn. So before starting the preview, one sets:
setappdata(placeholderImg,'UpdatePreviewWindowFcn',@mypreview_fcn);
and defines mypreview_fcn as
function mypreview_fcn(obj, event, himage)
rotImg = rot90(event.Data);
set(himage, 'cdata', rotImg);
end
That is it.
  2 Comments
Jonathan
Jonathan on 26 Sep 2016
Could you give an detail on how to call this function? Thx.
Ian Hunter
Ian Hunter on 7 Sep 2018
I would also find that helpful. How do I replace the standard preview() call with the my preview_fcn call such that my figure will be updated with the fetched, then transformed frame. Thanks. -Ian

Sign in to comment.

More Answers (2)

Guoheng Zhao
Guoheng Zhao on 25 Jun 2019
Another way, not as elegant but simpler, is to set the axis view angle and direction.

Steve Beguin
Steve Beguin on 28 Nov 2016
Edited: Walter Roberson on 28 Nov 2016
Thanks PaoloB,
This is the most elegant solution I have seen! It works well in my GUI with multiple axes
Here are some hints on how I implemented it:
%////////////////////////////////
global cameraObj ;
global horizontal_flip;
horizontal_flip = 0;
global vertical_flip;
vertical_flip = 0;
global rotation;
rotation = 0;
cameraObj = videoinput('winvideo',selection); %selection is the camera device ID (in my case works for microscope cameras as well as webcams and anything connected through the manyCam software)
axes(handles.axesCamera); %sets the focus on the desired axis of the GUI
vidRes = cameraObj.VideoResolution;
nBands = cameraObj.NumberOfBands;
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
axis off;
PreviewHandle = preview(cameraObj,hImage); % the handle of the preview object
% ////////////////////////// then in specific callback for radiobutton or pushbutton here is the code
% --- Executes on button press in radiobuttonHorizontalFlip.
function radiobuttonHorizontalFlip_Callback(hObject, eventdata, handles)
global PreviewHandle;
global horizontal_flip;
if get(hObject,'Value')
horizontal_flip = 1;
else
horizontal_flip = 0;
end
setappdata(PreviewHandle,'UpdatePreviewWindowFcn',@mypreview_fcn);
% --- Executes on button press in radiobuttonVerticalFlip.
function radiobuttonVerticalFlip_Callback(hObject, eventdata, handles)
global PreviewHandle;
global vertical_flip;
if get(hObject,'Value')
vertical_flip = 1;
else
vertical_flip = 0;
end
setappdata(PreviewHandle,'UpdatePreviewWindowFcn',@mypreview_fcn);
% --- Executes on button press in pushbuttonRotatePlus90deg.
function pushbuttonRotatePlus90deg_Callback(hObject, eventdata, handles)
global PreviewHandle;
global rotation;
rotation = rotation + 1;
setappdata(PreviewHandle,'UpdatePreviewWindowFcn',@mypreview_fcn);
% --- Executes on button press in pushbuttonRotateMinus90deg.
function pushbuttonRotateMinus90deg_Callback(hObject, eventdata, handles)
global PreviewHandle;
global rotation;
rotation = rotation - 1;
setappdata(PreviewHandle,'UpdatePreviewWindowFcn',@mypreview_fcn);
function mypreview_fcn(obj, event, himage)
global horizontal_flip;
global vertical_flip;
global rotation;
Img = event.Data;
if horizontal_flip
Img = fliplr(Img);
end
if vertical_flip
Img = flipud(Img);
end
Img = rot90(Img,rotation);
set(himage, 'cdata', Img);
%////////////////////
Hope it will be helpful for someone trying to achieve the same thing
Cheers,
Steve

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!