How to use slider for image processing?

6 次查看(过去 30 天)
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)

采纳的回答

Image Analyst
Image Analyst 2013-10-8
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 个评论
Image Analyst
Image Analyst 2013-10-8
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 2013-10-9
Dear Image Analyst,
It worked. :)
Thanks very much.

请先登录,再进行评论。

更多回答(1 个)

cr
cr 2013-10-7
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 个评论
Kev
Kev 2013-10-7
编辑:Kev 2013-10-7
Thanks for reply, but you didn't provide solution.
Please help

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by