0

我想练习在matlab中设计GUI,这个GUI有两个功能-一个用于选择图像,第二个用于过滤,这种图形界面的一般结构非常简单
在此处输入图像描述

这是两个代码 - 一个在按下选择图像后选择图像,第二个在单击过滤器图像后使用简单平均过滤器过滤图像

function select_image_Callback(hObject, eventdata, handles)
% hObject    handle to select_image (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';'*.png'},'File select');
image=strcat(pathname,filename);
axes(handles.axes1);
imshow(image);

和过滤

function filter_image_Callback(hObject, eventdata, handles)
% hObject    handle to filter_image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h = ones(5,5) / 25;
Filtered_image = imfilter(image,h);
axes(handles.axes2);
imshow(Filtered_image);

但是当我运行代码时,我选择了简单的这个文件

在此处输入图像描述

我收到以下错误

Error using imfilter
Expected input number 1, A, to be one of these types:

numeric, logical

Instead its type was matlab.graphics.primitive.Image.

Error in imfilter>parse_inputs (line 186)
validateattributes(a,{'numeric' 'logical'},{'nonsparse'},mfilename,'A',1);

Error in imfilter (line 118)
[a, h, boundary, sameSize, convMode] = parse_inputs(varargin{:});

Error in filter_image_filter_image_Callback (line 92)
Filtered_image = imfilter(image,h);

Error in gui_mainfcn (line 95)
        feval(varargin{:});

Error in filter_image (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)filter_image('filter_image_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating UIControl Callback

为什么会这样?提前致谢

4

1 回答 1

0

根据我的问题和与问题相关的问题,我决定也包括我的解决方案,这是我在研究后发现的,这是我的代码片段,首先感谢@Benoit_11 的建议,我已将名称从 image 更改为 selected_image ,与我关于从另一个函数访问一个变量(在本例中为图像)的评论有关,我使用了函数 getimage,所以有我的完整解决方案

function select_image_Callback(hObject, eventdata, handles)
% hObject    handle to select_image (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';'*.png'},'File select');
selected_image=strcat(pathname,filename);
axes(handles.axes1);
imshow(selected_image);

% --- Executes on button press in filter_image.
function filter_image_Callback(hObject, eventdata, handles)
% hObject    handle to filter_image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selected_image=getimage(handles.axes1);
h = ones(5,5) / 25;
Filtered_image = imfilter(selected_image,h);
axes(handles.axes2);
imshow(Filtered_image);

在此处输入图像描述

于 2017-01-08T19:36:26.533 回答