Rotate video from camera preview

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?

 Respuesta aceptada

PaoloB
PaoloB el 13 de Mzo. de 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 comentarios

Jonathan
Jonathan el 26 de Sept. de 2016
Could you give an detail on how to call this function? Thx.
Ian Hunter
Ian Hunter el 7 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (2)

Guoheng Zhao
Guoheng Zhao el 25 de Jun. de 2019

1 voto

Another way, not as elegant but simpler, is to set the axis view angle and direction.

1 comentario

Rob Campbell
Rob Campbell el 17 de Mayo de 2024
This possibly might be faster, too. Could be a performance hit with the callback. There definitely is when updating an imaging from a camera using a listener and callback instead of using "preview".

Iniciar sesión para comentar.

Steve Beguin
Steve Beguin el 28 de Nov. de 2016
Editada: Walter Roberson el 28 de Nov. de 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

Categorías

Más información sobre MATLAB Support Package for USB Webcams en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Mzo. de 2015

Comentada:

el 17 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by