簡體   English   中英

Matlab 中的 Slider 和 ButtonDownFcn 的問題

[英]problem with Slider and ButtonDownFcn in Matlab

我對 Slider 和 ButtonDownFcn 有疑問。

目前,我在 GUI 中向用戶顯示圖像(使用 GUIDE),然后通過在其周圍繪制一個矩形讓他們 select 車牌。

我添加了 slider 以度數為單位轉動圖像。

當我轉動圖像然后單擊它時,圖像返回到其初始 state(角度)。

可以做些什么來確保圖像保持在其位置,然后在其上繪制矩形?

這是我的 ButtonDownFcn function:

function axes1_ButtonDownFcn(hObject, eventdata, handles)
global image  h 
imshow(image, []);
h = imrect;
setColor(h, 'black');

這是我的 slider function:

function slider2_Callback(hObject, eventdata, handles)
global image hImage
slider_value = get(handles.slider2,'Value');
axes(handles.axes1);
hImage = imshow(imrotate(image,slider_value,'bilinear'), 'Parent', handles.axes1);
set(hImage, 'ButtonDownFcn', @(s,e) axes1_ButtonDownFcn());

編輯:現在一切正常,更重要的是它很清楚。 最后一件事你知道我如何從 X 和 Y 軸上刪除數字。 我嘗試axes off但沒有幫助。

在此處輸入圖像描述

多謝。

我相信通過例子來學習更容易。 因此,我將繼續我在您之前的問題中開始的示例。

當 GUI 啟動時,會顯示一個圖像,您可以使用 slider 旋轉該圖像。 點擊圖片時,select 一個矩形的ROI(可以繼續旋轉下面的圖片)。 滿意后,雙擊矩形,裁剪后的圖像顯示在其他軸上(一個從旋轉圖像裁剪,另一個從原始圖像裁剪),然后刪除矩形。

function rotationGUI2()
    %# setup GUI
    hFig = figure('menu','none', 'Position',[100 100 750 420]);
    hAx = axes('Parent',hFig, 'Units','pixels', 'Position',[50 70 350 300]);
    hAx1 = axes('Parent',hFig, 'Units','pixels', 'Position',[450 230 250 140]);
    hAx2 = axes('Parent',hFig, 'Units','pixels', 'Position',[450 70 250 140]);
    uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
        'Max',360, 'SliderStep',[1 10]./360, ...
        'Position',[100 20 300 30], 'Callback',@slider_callback) 
    hTxt = uicontrol('Style','text', 'String','0', ...
        'Units','pixels', 'Position',[240 25 20 15]);

    %# read and show image
    I = imread('cameraman.tif');
    hImg = imshow(I, 'Parent',hAx);
    set(hImg, 'ButtonDownFcn',@image_ButtonDownFcn);  %# attach event listener

    %# Callback functions
    function slider_callback(hObj, eventdata)
        angle = round( get(hObj,'Value') );             %# rotation angle
        I_rot = imrotate(I, angle, 'bilinear', 'crop'); %# rotate image
        set(hImg, 'CData',I_rot)                        %# update image
        set(hTxt, 'String',num2str(angle))              %# update text
    end
    function image_ButtonDownFcn(hObj,eventdata)
        hRect = imrect(hAx);
        setColor(hRect, 'black');
        rectPos = wait(hRect);
        delete(hRect)

        I1 = imcrop(I, rectPos);                  %# crop from original image
        I2 = imcrop(get(hImg,'CData'), rectPos);  %# crop from rotated image
        imshow(I1, 'Parent',hAx1)
        imshow(I2, 'Parent',hAx2)
    end
end

在此處輸入圖像描述

同樣,使用 GUIDE 生成的圖形重新創建示例應該是類似的。 HTH


編輯

您將在下面找到由 GUIDE 生成的類似示例。 和以前一樣,您必須創建 GUI 並通過拖放添加組件。 使用“Property Inspector”根據需要調整它們的屬性。 更重要的是,給每個人一個唯一的Tag 我在用着:

  • imgAxis、cropAxis、processAxis
  • 加載按鈕,進程按鈕
  • 角度滑塊,角度文本

指導

最好不要使用全局變量,而是將數據存儲在將傳遞給回調函數的handles結構中。 只需記住在任何更改其數據的方法結束時調用guidata以提交更改。

對於手動將處理程序附加到圖像ButtonDownFcn事件的部分,您還應該將handles結構傳遞給 arguments 列表。

也就是說,這里是 GUI 背后的代碼(相關部分):

%# --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
    %# store handles to figure, axes, and others
    handles.fig = hObject;
    handles.hAx = findobj(hObject, 'Tag','imgAxis');
    handles.hAxCrop = findobj(hObject, 'Tag','cropAxis');
    handles.hAxProcess = findobj(hObject, 'Tag','processAxis');
    handles.img = [];
    handles.hImg = [];
    handles.hImgCrop = [];

    %# disable interaction of some components until image is loaded
    set(findobj(hObject, 'Tag','angleSlider'), 'Enable','off')
    set(findobj(hObject, 'Tag','processButton'), 'Enable','off')

    %# Update handles structure
    guidata(hObject, handles);
end

%# --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
    varargout{1} = handles.fig;
end

%# --- Executes on slider movement.
function angleSlider_Callback(hObject, eventdata, handles)
    angle = round( get(hObject,'Value') );                    %# rotation angle
    I_rot = imrotate(handles.img, angle, 'bilinear', 'crop'); %# rotate image
    set(handles.hImg, 'CData',I_rot)                          %# update image
    set(findobj(handles.fig,'Tag','angleText'), 'String',num2str(angle))
end

%# --- Executes on button press in processButton.
function processButton_Callback(hObject, eventdata, handles)
    %# get cropped image
    I = get(handles.hImgCrop, 'CData');

    %# do some processing: here i'm simply detecting the edges
    if isrgb(I), I = rgb2gray(I); end
    %#BW = im2bw(I, graythresh(I));
    BW = edge(I, 'canny');

    %# show processed image
    imshow(BW, 'Parent',handles.hAxProcess)

    %# Update handles structure
    guidata(hObject, handles);
end

%# --- Executes on button press in loadButton.
function loadButton_Callback(hObject, eventdata, handles)
    %# get image file location
    [fName, fPath] = uigetfile(...
        {'*.psd;*.bmp;*.jpg;*.tif;*.png;*.gif','All Image Files'; ...
        '*.*','All Files' }, 'File Selector', ...
        fullfile(matlabroot,'toolbox','images','imdemos'));
    if fPath==0
        msgbox('No file selected', 'File Selector', 'error');
        return
    end

    %# read and show image
    handles.img = imread( fullfile(fPath,fName) );
    handles.hImg = imshow(handles.img, 'Parent',handles.hAx);

    %# attach handler
    set(handles.hImg, 'ButtonDownFcn',{@imgAxis_ButtonDownFcn,handles});

    %# reenable disabled components
    set(findobj(handles.fig, 'Tag','angleSlider'), 'Enable','on')

    %# Update handles structure
    guidata(hObject, handles);
end

%# --- Executes on mouse press over axes background.
function imgAxis_ButtonDownFcn(hObject, eventdata, handles)
    %# check if some image is shown
    if isempty(handles.hImg) || ~ishandle(handles.hImg)
        return
    end

    %# select ROI using a rectangle
    hRect = imrect(handles.hAx);
    setColor(hRect, 'black');
    rectPos = wait(hRect);
    delete(hRect)

    %# crop image from rotated image, and show it
    I = imcrop(get(handles.hImg,'CData'), rectPos);
    handles.hImgCrop = imshow(I, 'Parent',handles.hAxCrop);

    %# reenable disabled components
    set(findobj(handles.fig, 'Tag','processButton'), 'Enable','on')

    %# Update handles structure
    guidata(hObject, handles);
end

截屏

我希望這一切都清楚

在您的axes1_ButtonDownFcn中,行imshow(image, []); 當您單擊它時,它會向您顯示未旋轉的圖像。 我認為刪除該行應該可以解決您的問題。 然后,您可以使用gca來獲取您的軸句柄。 簡而言之,試試這個

function axes1_ButtonDownFcn(hObject, eventdata, handles)
global image  h 
h = imrect(gca);
setColor(h, 'black');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM