如何使用按钮将加载的图像传递到另一个Matlab文件?

时间:2014-06-23 03:46:14

标签: image matlab

将图像加载到Matlab GUI后,如何使用pushbutton将该图像传递给另一个Matlab文件?当我按下GUI中的按钮时,图像应该传递给我的Matlab代码。

这是我的GUI代码

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile(...
{'*.jpg;*.gif;*.png;*.bmp',...
'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
'open the image file to be verified');
fullimagefilename = fullfile(pathname,filename);
axes1 = imread(fullimagefilename);
axes(handles.axes1);
image(axes1);

%axes(handles.axes1);
%imshow('E:\degraded images\3.jpg')


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    -pushbutton 1 is used to browse file .
    -and by pushbutton2 i want to pass the browsed file to my base file and its code is

im=imread('E:\degraded images\3.jpg'); 
hsv = rgb2hsv(im);
hueImage = hsv(:,:, 1);
meanHue = mean2(hueImage); 
figure, imshow(im);
title(meanHue);
%figure , imshow(im);
%im = imresize(im, 0.5);
%im2 = imread('E:\degraded images\3.jpg') ;
im2= im;
im=im(:,:,1);


sigmaA=8;
sigmaB=10;
sigmaMax=max([sigmaA sigmaB]);
fsz=[sigmaMax, sigmaMax];

1 个答案:

答案 0 :(得分:0)

如果我理解正确,GUI使用pushbutton1允许用户选择文件并在轴中显示。当用户按下另一个按钮pushbutton2时,可能是在同一个GUI上,您希望该按钮的回调可以访问所选(或浏览过的)图像。

这可以通过在handles结构中为所选图像数据创建一个字段,然后使用guidata函数存储该数据来完成。

在第一次回调中,执行以下操作

function pushbutton1_Callback(hObject, eventdata, handles)

[filename,pathname]=uigetfile(...
    {'*.jpg;*.gif;*.png;*.bmp',...
     'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
     'open the image file to be verified');

% need to handle the case where the user presses cancel
if filename~=0
    fullimagefilename = fullfile(pathname,filename);

    % create an img field in handles for the image that is being loaded
    handles.img = imread(fullimagefilename);

    axes(handles.axes1);
    image(handles.img);

    % save the application data
    guidata(hObject,handles);
end

现在在第二个回调中,您可以通过handles结构

访问此文件
function pushbutton2_Callback(hObject, eventdata, handles)

% check to ensure that img exists in handles
if isfield(handles,'img')

    % copy the image from the handles structure
    im=handles.img;

    % continue with your code
    hsv = rgb2hsv(im);

    % etc.

end

就是这样 - 第一个回调将图像保存为handles结构作为其中的字段,第二个回调通过handles字段从img检索该图像。

尝试一下,看看会发生什么!