jolene
jolene

Reputation: 29

How to display desired image in MATLAB GUI?

I have a GUI as shown below:

GUI

And I would like to apply Otsu threshold on the image displayed after contrast enhancement. But whenever I pressed the apply button for Otsu threshold,the Otsu threshold will be applied on the original image instead of the image after contrast enhancement (refer to the attached GUI and coding). So how do I overcome this problem?

function pushbutton10_Callback(hObject, eventdata, handles)
I = im2double(handles.im);
imshow(I);
% prompts for the two inputs
prompt = {'Enter LOW contrast limit:','Enter HIGH contrast limit:'};
% title of the dialog box
dlg_title = 'Input';
% number of input lines available for each variable
num_lines = 1;
% default answer for each input
defaultans = {'0','1'};
% generate the dialog box and wait for answer
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
% convert string answers into doubles
lo_in = str2double(answer{1});
hi_in = str2double(answer{2});
% apply on image
K = imadjust(I,[lo_in hi_in],[]);

axes(handles.axes2);
imshow(K);
axes(handles.axes3);
imshow(K);



function pushbutton12_Callback(hObject, eventdata, handles)
I = im2double(handles.im);
im = rgb2gray(I);
level = graythresh(im)
a = im2bw(im,level);
axes(handles.axes3);
imshow(a);
axes(handles.axes4);
imshow(a);

Upvotes: 2

Views: 847

Answers (1)

Saeed Masoomi
Saeed Masoomi

Reputation: 1834

I think it better to define some field for handles structure in openingFcn function like below

handles.OriginaImage=0;
handles.afterEnhancement=0;

and when you read Original Image you update handles.OriginalImage as following code

handles.OriginalImage=imread("whatever.whatever");
% don't forget below line because this line update handles structure
guidata(hObject,handles) 

and when you apply contrast Enhancement you save it in handles.afterEnhancement and again update handles structure

lo_in = str2double(answer{1});
hi_in = str2double(answer{2});
% apply on image
handles.afterEnhancement = imadjust(handles.OriginalImage,[lo_in hi_in],[]);
guidata(hObject,handles)

then in pushbutton12_callback you can apply otsu method on Enhancement image by handles.afterEnhancement

I = im2double(handles.afterEnhancement);
im = rgb2gray(I);
level = graythresh(im)
a = im2bw(im,level);
axes(handles.axes3);
imshow(a);

I test this codes in my MATLABin my way

Example code

function varargout = Sdfl(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @Sdfl_OpeningFcn, ...
               'gui_OutputFcn',  @Sdfl_OutputFcn, ...
               'gui_LayoutFcn',  [] , ...
               'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
   [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
   gui_mainfcn(gui_State, varargin{:});
end

function Sdfl_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.original_image=0;
handles.blur_image=0;
% Update handles structure
guidata(hObject, handles);

function varargout = Sdfl_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

function pushbutton1_Callback(hObject, eventdata, handles)
handles.original_image=imread('pout.tif');
guidata(hObject, handles); %this line will update handles structure
axes(handles.axes1)
imshow(handles.original_image)

function pushbutton2_Callback(hObject, eventdata, handles)
handles.blur_image=imadjust(im2double(handles.original_image),[0.3 0.7],[]);
guidata(hObject, handles);
axes(handles.axes2)
imshow(handles.blur_image);

function pushbutton3_Callback(hObject, eventdata, handles)
thresh=graythresh(handles.blur_image);
bw_image=im2bw(handles.blur_image,thresh);
axes(handles.axes3)
imshow(bw_image);

output enter image description here

Upvotes: 2

Related Questions