Rotate video from camera preview

4 次查看(过去 30 天)
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?

采纳的回答

PaoloB
PaoloB 2015-3-13
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 个评论
Jonathan
Jonathan 2016-9-26
Could you give an detail on how to call this function? Thx.
Ian Hunter
Ian Hunter 2018-9-7
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

请先登录,再进行评论。

更多回答(2 个)

Guoheng Zhao
Guoheng Zhao 2019-6-25
Another way, not as elegant but simpler, is to set the axis view angle and direction.
  1 个评论
Rob Campbell
Rob Campbell 2024-5-17
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".

请先登录,再进行评论。


Steve Beguin
Steve Beguin 2016-11-28
编辑:Walter Roberson 2016-11-28
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

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for USB Webcams 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by