【问题标题】:Viewing image displayed on axes查看轴上显示的图像
【发布时间】:2013-05-11 15:23:40
【问题描述】:

(http://s1273.photobucket.com/user/Chethan_tv/media/CBIR_zpsb48bce14.jpg.html)

上图是我的最终输出,open 按钮用于查看显示在各个轴上的图像。 我使用以下代码进行显示

function open1_Callback(filename, hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)

其中,6 是轴数。但是我遇到了错误 undefined variable handles 为了在轴上显示图像,我使用了以下代码

function displayResults(filename,hObject, eventdata, handles)

% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
guidata(hObject,handles)

文件名是一个文本文件。 如何使用按钮显示相应的图像?

【问题讨论】:

  • 我只想打开显示在坐标轴上的图像,该图像位于open 按钮上方。让观看者对大尺寸图像感到满意

标签: matlab matlab-figure matlab-guide


【解决方案1】:

这是因为您弄乱了 GUIDE 生成的代码。 通常回调函数定义如下:

function SomeButton_Callback(hObject, eventdata, handles)

但是在你写的代码中

function open1_Callback(filename, hObject, eventdata, handles)

但是guide仍然按照这个特定的顺序向回调函数发送三个参数(hObjecteventdatahandles)。所以 MatLab 会感到困惑并抛出错误。

你最好把你的文件名放到*_OpeningFcn函数的handles结构中,然后在所有的回调中使用它。

*_OpeningFcn 的末尾,您应该添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);

然后在你的按钮的回调函数中

function open1_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);

是的,您使用fopen 函数打开的所有文件都应该使用fclose 关闭。当您因为其他程序正在使用它而无法在您最喜欢的编辑器中更新您的文件时,您将很难学会。

另请参阅 (Making universal variables in MATLAB GUI)

更新以反映 cmets 中的讨论:

要实现您想要的行为,我会执行以下操作:

*_OpeningFcn 末尾添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images    = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);

    % store file name and image itself for later use
    handles.MyData.Images{N} = rgb;
    handles.MyData.FileNames{N} = imagename;

    % we have buttons like open1 open2 open3 etc...
    % add some info to the open buttons
    % so they will be aware which image they display
    btn = handles.(sprintf('open%d', N));
    set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);

然后在打开按钮的回调函数中执行以下操作

function open1_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

N   = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax  = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);

基本上,这个回调是通用的:它应该在不修改所有打开的按钮的情况下工作。您甚至可以将GUIDE中open2open3 ...按钮的回调函数更改为open1_Callback

【讨论】:

  • 你有几个axes/open按钮对:axes显示一个图像,open按钮应该以更大的尺寸显示相应的图像。对吗?
  • 这意味着你没有open6按钮
  • 您确定您的数据文件中列出了足够多的图像吗?
  • 这意味着N大于handles.MyData.Images中的元素个数
  • N 是 6 到 10 即 5。然后根据上面的代码,handles.MyData.Images 也得到 5 张图像。那么代码的哪一部分是错误的?并且请删除您以前的 cmets 以便进一步交谈
猜你喜欢
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 2014-04-12
  • 2014-06-07
  • 1970-01-01
  • 2021-12-18
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多