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일

1 개 추천

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일

1 개 추천

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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 MATLAB Support Package for USB Webcams에 대해 자세히 알아보기

질문:

2015년 3월 11일

댓글:

2024년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by