且构网

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

在 Matlab 中使用滑块旋转图像

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

这是一个 GUI 示例:

函数旋转GUI()%# 读取图像I = imread('cameraman.tif');%# 设置界面hFig = figure('menu','none');hAx = axes('父',hFig);uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...'Max',360, 'SliderStep',[1 10]./360, ...'位置',[150 5 300 20],'回调',@slider_callback)hTxt = uicontrol('样式','文本', '位置',[290 28 20 15], '字符串','0');%# 显示图片imshow(我,'父母',hAx)%# 回调函数函数滑块回调(hObj,事件数据)角度=圆形(get(hObj,'Value'));%# 以度为单位获取旋转角度imshow(imrotate(I,angle), 'Parent',hAx) %# 旋转图像set(hTxt, 'String',num2str(angle)) %# 更新文本结尾结尾

如果您更喜欢在 GUIDE 中构建 GUI,请按照以下步骤操作:

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

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

  • 在图中的xxxx_OpeningFcn函数中,读取图片并存储在handles结构中,然后展示出来:

句柄.I = imread('cameraman.tif');imshow(I, 'Parent',findobj(hObject,'Tag','imgAxis')) %# 使用你分配的标签guidata(hObject, 句柄);%# 更新句柄结构

  • 为您的滑块创建一个回调事件处理程序,并添加代码:
角度=圆形(get(hObject,'Value'));imshow(imrotate(handles.I,角度))

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

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

I have a GUI (using GUIDE) in Matlab, this is how it looks:

I want to rotate the image using slider and to show the change in real time.

I use axes to display the image.

how can I do this?

EDIT: I'm building OCR application. this is how the plate looks when I'm rotate it, the numbers are totally deformed.

thanks.

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


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

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

  • 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.

  • 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

  • Create a callback event handler for your slider, and add the code:
    angle = round( get(hObject,'Value') );
    imshow( imrotate(handles.I,angle) )


EDIT: 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).

(Illustration borrowed from: Understanding Digital Image Interpolation)