Hi,
I am completing a univeristy assignment which is to create a GUI with various edge detection methods and to write our own method. As can be see from the first image below, the edge detection methods work well and display correctly, however when selecting my own method the final step, adding a white border, is applied to the wrong image (see second image).
I have attached the .m file for reference. I believe the error is towards the bottom of the my_own_method function.
Does any one know how I can apply the my_own_method function to axis 2 only?
Thanks

 採用された回答

Cris LaPierre
Cris LaPierre 2020 年 4 月 11 日

0 投票

It looks like the other edge detection methods are just loading an image of the result rather than actually processing the result.
When you plot your results, you are not specifying where to plot, so it is by default plotting them on the current axes (the last one that was used). This tends to be the one where the police image was added.
To fix this, makes axes2 the current axis before turning hold on and plotting. The line of code for doing this is already in each of case statements for the other methods:
axes(handles.axes2);
If you really want to be sure, you could also specify the axes to plot into as part of your plot command (though you still have to turn hold on). So your plot code might look like this.
...
axes(handles.axes2);
hold on
for k = 1:length(B)
boundary = B{k};
plot(handles.axes2,boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
hold off
Don't forget, it's always best practice to also turn hold off.

10 件のコメント

Ellen Taylor
Ellen Taylor 2020 年 4 月 12 日
Hi Cris,
Thank you for your help. Unfortunately this does not work, the following error occurs.
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)imageprocessinggui('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Rik
Rik 2020 年 4 月 12 日
You need to provide the full error message. This line only tells us there is an error during the execution of a GUI created with GUIDE. The full error message consists of all the red text. Call clc and execute the callback, that is the easiest way to obtain the full error message.
Ellen Taylor
Ellen Taylor 2020 年 4 月 18 日
Hi Rik,
Here is all the red text:
Unable to resolve the name handles.axes2.
Error in imageprocessinggui>my_own_method (line 169)
axes(handles.axes2);
Error in imageprocessinggui>popupmenu1_Callback (line 137)
img2 = my_own_method(img);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in imageprocessinggui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)imageprocessinggui('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Cris LaPierre
Cris LaPierre 2020 年 4 月 18 日
You call my_own_function from inside popupmenu1_Callback, which has handles as an input. That means you can pass this on to my_own_method. Add handles as an additional input to your function.
function [imgOut] = my_own_method(img, handles)
...
axes(handles.axes2);
hold on
for k = 1:length(B)
boundary = B{k};
plot(handles.axes2,boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
hold off
Cris LaPierre
Cris LaPierre 2020 年 4 月 18 日
Just a side note. I don't have access to your fig file, so I don't know what your tag names are for the elements in your figure. I'm assument axes2 is the axes you want to plot into, but you need to figure that part out. Or share the fig file for this gui as well.
Ellen Taylor
Ellen Taylor 2020 年 4 月 18 日
Hi Cris,
I added the handles as suggested above but still the following error message. I have attached the .fig and .m files.
Unrecognized function or variable 'hObject'.
Error in imageprocessinggui>my_own_method (line 155)
handles = guidata(hObject);
Error in imageprocessinggui>popupmenu1_Callback (line 137)
img2 = my_own_method(img);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in imageprocessinggui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)imageprocessinggui('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Rik
Rik 2020 年 4 月 18 日
Replace this
case 'Own Method'
img2 = my_own_method(img);
with this
case 'Own Method'
img2 = my_own_method(img,handles);
Ellen Taylor
Ellen Taylor 2020 年 4 月 18 日
If I run after changing as per above the following error message ocurs:
Unrecognized function or variable 'hObject'.
Error in imageprocessinggui>my_own_method (line 155)
handles = guidata(hObject);
Error in imageprocessinggui>popupmenu1_Callback (line 137)
img2 = my_own_method(img,handles);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in imageprocessinggui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)imageprocessinggui('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
However, if I remove the below line and run, no error messages occur but the white border seems to apply and then revert back to the image without the white border
handles = guidata(hObject);
Cris LaPierre
Cris LaPierre 2020 年 4 月 18 日
編集済み: Cris LaPierre 2020 年 4 月 19 日
The reason they are not appearing is because of the order you are plotting your images in. You immediately overwrite the boundaries with the imshow command in your try-catch statement.
Now that I have the fig file, I was able to play around. It ends up you do not need to pass handles to my_own_method after all. Instead, keep all your plotting in the same place (the try-catch statement in popupmenu1_Callback). Move your plotting of the boundaries to the catch statement, and just add the boundaries as a second output from my_own_method.
Update your "Own Method" catch statement to the following
...
case 'Own Method'
[img2,B] = my_own_method(img);
axes(handles.axes2);
imshow(img2);
hold on
for k = 1:length(B)
boundary = B{k};
plot(handles.axes2,boundary(:,2),boundary(:,1),'w','LineWidth',2)
end
Update "my_own_method" accordingly
function [imgOut,B] = my_own_method(img)
% Identifying the Thresholds of the Image
I = rgb2gray(img);
bw = imbinarize(I);
% Remove any object/threshold of object that is less than 30 pixels
bw = bwareaopen(bw,30);
% Close any gaps
se =strel('disk',2);
bw = imclose(bw,se);
% Fill the thresholds in
bw = imfill(bw,'holes');
% Identify the outer boundary of the fill items
[B,L] = bwboundaries(bw,'noholes');
% Fill the inner boundary in and show the image
imgOut = label2rgb(L,@jet,[.5 .5 .5]);
When I run this, I get the following output without any errors.
Ellen Taylor
Ellen Taylor 2020 年 4 月 19 日
Cris, thank you so much for all your help. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by