How to use slider for image processing?

Dear Matlab Community,
I'm working to create a tool that would measure diameter of our fibers. We've decided to create a GUI for ease of use and to change parameters when needed.
I'm creating GUI with the help of GUIDE. I want to have image updated on the axes when slider is moved, but it doesn't seem to work. Disp line is executed when slider is moved, but I'm not sure why picture isn't displayed. How do I know if bwopen line is executed? Why do variable disappear from matlab workspace? Lastly, Am I using right technique to import and handle images?
I'm totally new to this. I'm referring books to learn about GUI. I'm struggling with slider for about two weeks.
I'm referring to this text: "Learning to Program with MATLAB - Building GUI Tools"
Is there any other tutorial or text you'd recommend?
Sincerely, Kev
Here is the code for GUI:
% --- Executes on button press in load_file.
function load_file_Callback(hObject, eventdata, handles)
% hObject handle to load_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global read_image
[filename, user_canceled] = imgetfile();
%fmt = 'jpg';
%[path,user_cance]=imgetfile();
if user_canceled
msgbox(sprintf('Error'),'Error','Error');
return
end
read_image = imread(filename);
axes(handles.axes1);
imshow(read_image);
% --- Executes on button press in binarize.
function binarize_Callback(hObject, eventdata, handles)
% hObject handle to binarize (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global read_image
colour_to_gray = rgb2gray(read_image);
% Apply median filtering on grayscale image for smoothing
filtered_image = medfilt2(colour_to_gray);
%imtool(filtered_image)
% Convert grayscale into Binary using Otsu Method
level = graythresh(filtered_image);
binary = im2bw(filtered_image, level);
axes(handles.axes1);
imshow(binary);
% --- Executes on slider movement.
function xSlider_Callback(hObject, eventdata, handles)
% hObject handle to xSlider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global binary
x=get(handles.xSlider, 'Value');
xs=num2str(x);
set(handles.xText, 'String', xs);
disp( ' Go, go, go, world! ' );
bw_open = imopen(binary, strel('disk', x));
axes(handles.axes1);
imshow(bw_open)

 Respuesta aceptada

Image Analyst
Image Analyst el 8 de Oct. de 2013
Set a break point on the imopen line by clicking in the left margin - a red spot will appear. If it stops there then it will execute it when you type F10. You may or may not see an effect when you display bw_open depending on the image and the size of x. You can subtract to see if anything changed.
difference = abs(double(bw_open) - double(binary));
imshow(difference, []);
If it's all black then there is no difference.

7 comentarios

Kev
Kev el 8 de Oct. de 2013
Editada: Kev el 8 de Oct. de 2013
Dear Image Analyst,
I applied breakpoint before imopen, but axes disappears when imshow() line is executed. I also added above lines there and it didn't show any effect. My slider values are only integers between 1 to 10 and there would be noticeable changes for imopen with given values.
Thank you!
Image Analyst
Image Analyst el 8 de Oct. de 2013
Editada: Image Analyst el 8 de Oct. de 2013
Put in these lines and tell what it says
whos binary
whos bw_open
fprintf('Min = %d, Max = %d\n', min(binary(:)), max(binary(:)));
fprintf('Min = %d, Max = %d\n', min(bw_open(:)), max(bw_open(:)));
fprintf('Min = %d, Max = %d\n', min(difference(:)), max(differences(:)));
I'm receiving following output:
Name Size Bytes Class Attributes
binary 0x0 0 double global
Name Size Bytes Class Attributes
bw_open 0x0 0 double
Min = , Max = Min = , Max = Undefined function or variable "differences".
Error in fig1>xSlider_Callback (line 141) fprintf('Min = %d, Max = %d\n', min(differences(:)), max(differences(:)))
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in fig1 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)fig1('xSlider_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Image Analyst
Image Analyst el 8 de Oct. de 2013
Well there's your problem. Your binary variable is no good.
Kev
Kev el 8 de Oct. de 2013
How may I fix it, sir?
Image Analyst
Image Analyst el 8 de Oct. de 2013
Put "global binary" in your binarize_callback so that it becomes a global variable instead of a local variable seen only within that callback.
Kev
Kev el 9 de Oct. de 2013
Dear Image Analyst,
It worked. :)
Thanks very much.

Iniciar sesión para comentar.

Más respuestas (1)

cr
cr el 7 de Oct. de 2013

0 votos

Code looks ok. imopen might be giving you an output that you dont expect. i.e. the "opened" image might the same as the one already on axes that you dont see any change, misleadingly looking as if not functioning. The workspace variables inside a function are deleted as soon as the control exits the function. If you want access to them, either make them global, or better, use breakpoints and debugging.

1 comentario

Kev
Kev el 7 de Oct. de 2013
Editada: Kev el 7 de Oct. de 2013
Thanks for reply, but you didn't provide solution.
Please help

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Preguntada:

Kev
el 7 de Oct. de 2013

Comentada:

Kev
el 9 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by