且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用滑块在Matlab中旋转图像

更新时间:2023-02-27 10:27:22

下面是一个示例GUI:

Here is an example 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,请按照以下步骤操作:

If you prefer to build the GUI in GUIDE, follow these steps:

  • 创建GUI,并添加必要的组件:轴,滑块,静态文本(拖放)

  • create GUI, and add necessary components: axis, slider, static text (drag-and-drop)

使用属性检查器",根据需要更改滑块属性:: Min/Max/Value/SliderStep.如果您分配一个Tag以便能够在代码中查找组件,这也将有所帮助.

Using the "Property Inspector", change slider properties as required:: Min/Max/Value/SliderStep. Also would help if you assign a Tag to be able to find components in the code.

在图中的xxxx_OpeningFcn函数中,读取图像并将其存储在handles结构中,然后显示该图像:

In the figure's xxxx_OpeningFcn function, read and store the image in the handles structure, then show it:


    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) )


图像旋转是一种仿射变换,它将输入图像像素的位置(x,y)映射到输出图像的新坐标(x2,y2)上.问题在于输出坐标可能并不总是整数.由于数字图像是在离散像素的网格上表示的,因此采用了某种形式的重采样/插值(这就是为什么当以特定角度旋转时,直线看起来可能呈锯齿状).


Image rotation is an affine transformation which maps the position (x,y) of input image pixels onto new coordinates (x2,y2) for the output image. The problem is that the output coordinates may not always be integers. Since digital images are represented on a grid of discrete pixels, thus some form of resampling/interpolation is employed (which is why straight lines might look jagged when rotated at certain angles).

(插图借鉴自:了解数字图像插值)