用于浏览图像并将图像转换为灰度的 matlab gui 代码

matlab gui code for browsing an image and converting image to grayscale

这是用于浏览图像并将图像转换为灰度的 matlab gui 代码 有人请更正我的代码,它无法正常工作,我尝试了很多但无法理解这里有什么问题

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)
set(handles.edit3, 'Visible','off');
% Build the complete filename
global im,im2
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file'); 
 MyImage = strcat(pathname, filename);

%This code checks if the user pressed cancel on the dialog.
        if isequal(filename,0) || isequal(pathname,0)
            uiwait(msgbox ('User pressed cancel','failed','modal')  )
            hold on;
        else
            uiwait(msgbox('User selected image sucessfully','sucess','modal'));
            hold off;      

        end
        im=imread(path);
        im=im2double(im); %converts to double
         %for backup process :)
imshow(MyImage,'Parent',handles.axes2);
title('INPUT IMAGE WITH NOISE')
handles.output = hObject;
guidata(hObject, handles);


% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a fusture version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata,handles,varargin)
global im
imblack=im;
rgb2gray(imblack);
imshow(imblack,'Parent',handles.axes2);
title(' IMAGE AFTER GRAYSCALE CONVERSION')
%gaussian filter:
  %set(handles.axes2, 'Visible','off');
  %set(handles.edit3, 'Visible','on');

这是更新后的代码

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)
set(handles.edit3, 'Visible','off');
% Build the complete filename
global im
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file'); 
 MyImage = strcat(pathname, filename);

%This code checks if the user pressed cancel on the dialog.
        if isequal(filename,0) || isequal(pathname,0)
            uiwait(msgbox ('User pressed cancel','failed','modal')  )
            hold on;
        else
            uiwait(msgbox('User selected image sucessfully','sucess','modal'));
            hold off;      

        end
        im=imread(MyImage);
        im=im2double(im); %converts to double
         %for backup process :)
imshow(im,'Parent',handles.axes2);
title('INPUT IMAGE WITH NOISE')
handles.output = hObject;
guidata(hObject, handles);


% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a fusture version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata,handles,varargin)
global im
imblack=rgb2gray(im);
imshow(imblack,'Parent',handles.axes2);
title(' IMAGE AFTER GRAYSCALE CONVERSION')
%gaussian filter:
  %set(handles.axes2, 'Visible','off');
  %set(handles.edit3, 'Visible','on'); 

所做的更改

1) im=imread(path); 里面没有名称为 path 的变量。

2) imshow(MyImage,'Parent',handles.axes2); MyImage 包含文件路径。 im 包含图像。

3) rgb2gray(imblack); 返回值必须存储在变量中以便在下一行绘制。

4) imshow(imblack,'Parent',handles.axes2); 如果图像从上一个按钮回调中正确传递。然后将显示输入图像而不是灰度。

5) global im,im2 im2 未使用,因此 im2 已删除