下面是一个 GUI 示例:
function rotationGUI()
%# read image
I = imread('cameraman.tif');
%# setup GUI
hFig = figure('menu','none');
hAx = axes('Parent',hFig);
uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
'Max',360, 'SliderStep',[1 10]./360, ...
'Position',[150 5 300 20], 'Callback',@slider_callback)
hTxt = uicontrol('Style','text', 'Position',[290 28 20 15], 'String','0');
%# show image
imshow(I, 'Parent',hAx)
%# Callback function
function slider_callback(hObj, eventdata)
angle = round(get(hObj,'Value')); %# get rotation angle in degrees
imshow(imrotate(I,angle), 'Parent',hAx) %# rotate image
set(hTxt, 'String',num2str(angle)) %# update text
end
end
如果您更喜欢在 GUIDE 中构建 GUI,请按照下列步骤操作:
创建 GUI,并添加必要的组件:轴、滑块、静态文本(拖放)
使用“属性检查器”,根据需要更改滑块属性::Min/Max/Value/SliderStep
。如果您分配一个也会有所帮助Tag
能够在代码中找到组件。
图中的xxxx_OpeningFcn
函数,读取图像并将其存储在handles
结构,然后显示它:
handles.I = imread('cameraman.tif');
imshow(I, 'Parent',findobj(hObject,'Tag','imgAxis')) %# use tag you assigned
guidata(hObject, handles); %# Update handles structure
angle = round( get(hObject,'Value') );
imshow( imrotate(handles.I,angle) )
EDIT:图像旋转是一种仿射变换,它将输入图像像素的位置 (x,y) 映射到输出图像的新坐标 (x2,y2)。问题是输出坐标可能并不总是整数。由于数字图像是在离散像素网格上表示的,因此采用了某种形式的重采样/插值(这就是为什么直线在以某些角度旋转时可能看起来呈锯齿状)。
(插图借自:了解数字图像插值 http://www.cambridgeincolour.com/tutorials/image-interpolation.htm)