【问题标题】:create GUI for selecting and filtering of Image创建用于选择和过滤图像的 GUI
【发布时间】:2017-01-08 18:46:36
【问题描述】:

我想练习一下matlab中的图形界面设计,这个图形界面有两个功能——一是选择图像,二是过滤,这种图形界面的总体结构非常简单

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

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

为什么会这样?提前致谢

【问题讨论】:

  • image 是 Matlab 中函数的名称,因此您可能会因为命名变量图像而发生冲突。尝试更改该变量的名称
  • 有一个问题,从第二个函数我无法访问第一个函数中的变量,我的意思是违反了变量范围,所以我应该将它声明为全局吗?
  • 我已经解决了,我会发布我的代码

标签: image matlab image-processing filter gui-designer


【解决方案1】:

基于我的问题和与问题相关的问题,我决定也包括我的解决方案,这是我在研究后发现的,这是我的代码片段,首先感谢 @Benoit_11 的建议,我已将名称从图像到 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2014-08-21
    • 2017-09-12
    • 1970-01-01
    • 2012-05-08
    • 2021-08-01
    相关资源
    最近更新 更多