Matlab中使用滑块旋转图像

发布于 2024-11-18 08:16:16 字数 324 浏览 2 评论 0原文

我在 Matlab 中有一个 GUI(使用 GUIDE),它看起来是这样的:

在此处输入图像描述

我想旋转使用滑块进行图像并实时显示变化。

我使用轴来显示图像。

我该怎么做?

编辑:我正在构建 OCR 应用程序。这就是我旋转盘子时的样子,数字完全变形了。

在此处输入图像描述

谢谢。

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

enter image description here

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.

enter image description here

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2024-11-25 08:16:16

以下是 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。如果您分配一个标签以便能够在代码中查找组件,也会有所帮助。

  • 在图中的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) )

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

在此处输入图像描述

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

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

enter image description here


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

enter image description here

(Illustration borrowed from: Understanding Digital Image Interpolation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文