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?
채택된 답변
추가 답변 (2개)
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
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
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
카테고리
도움말 센터 및 File 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!