如何使用Octave中的按钮在轴上显示图像?

时间:2017-08-03 02:00:27

标签: matlab octave matlab-figure

我正在使用GNU Octave 4.2.1。在Linux Debian上。我试图创建一个按钮(在Octave中称为pushbutton)打开像jpeg文件的图像并显示到轴。到目前为止,我的代码如下所示:

%image preview
cmdOpenImage = uicontrol(
  mainForm = "style", "pushbutton", "string", "OPEN THE IMAGE",
  "position", [100,630, 100, 30]
)

仍在为按钮工作,进度如下所示:

%image preview
cmdOpenImage = uicontrol(
  mainFrm =  "style", "pushbutton", 
              "string", "OPEN THE IMAGE",
              "position", [100,630, 100, 30],
              "ButtonDownFcn", {@previewImage, "1"}
)

function previewImage(h, e, a1)
  i = imread('donuts.jpg');
imshow(i);  
endfunction

我在MATLAB中的图像处理应用如下所示:

function cmdOpenImage_Callback(hObject, eventdata, handles)
[a, b] = uigetfile();
i = imread([a, b]);
guidata(hobject, handles);
axes(handles.PreviewImage);
imshow(i);

MATLAB中图像处理的prev app图片:

pushbutton displayed image on axes in matlab when clicked
单击时,按钮在轴上显示图像。

1 个答案:

答案 0 :(得分:3)

你的代码中有语法错误,逻辑有点混乱,但它足以让你知道你想要做什么。这是一个工作版本:

%% In file 'imageViewer.m'
function imageViewer ()
  MainFrm = figure ( ...
    'position', [100, 100, 250, 350]); 

  TitleFrm = axes ( ... 
    'position', [0, 0.8, 1, 0.2], ... 
    'color',    [0.9, 0.95, 1], ...
    'xtick',    [], ... 
    'ytick',    [], ...  
    'xlim',     [0, 1], ... 
    'ylim',     [0, 1] );

  Title = text (0.05, 0.5, 'Preview Image', 'fontsize', 30);

  ImgFrm = axes ( ...
    'position', [0, 0.2, 1, 0.6], ... 
    'xtick',    [], ... 
    'ytick',    [], ...
    'xlim',     [0, 1], ... 
    'ylim',     [0, 1]);

  ButtonFrm = uicontrol (MainFrm, ...
    'style',    'pushbutton', ... 
    'string',   'OPEN THE IMAGE', ...
    'units',    'normalized', ...
    'position', [0, 0, 1, 0.2], ...
    'callback', { @previewImage, ImgFrm });
end

%% callback subfunction (in same file)
function previewImage (hObject, eventdata, ImageFrame)
  [fname, fpath] = uigetfile();
  Img = imread (fullfile(fpath, fname));
  axes(ImageFrame);
  imshow(Img, []);
  axis image off
end

然后从终端运行imageViewer()

----------->