问题描述
我有一个GUI,如下所示:
I have a GUI as shown below:
我想对对比度增强后显示的图像应用Otsu阈值。但每当我按下Otsu阈值的应用按钮时,Otsu阈值将应用于原始图像而不是对比度增强后的图像(参见附加的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);
推荐答案
我认为最好为<$定义一些字段c $ c>处理结构 openingFcn
函数如下
I think it better to define some field for handles
structure in openingFcn
function like below
handles.OriginaImage=0;
handles.afterEnhancement=0;
当您阅读原始图像时,您更新 handles.OriginalImage
如下代码
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)
当您应用对比度增强时,将其保存在 handles.afterEnhancement
中,并再次更新句柄
结构
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)
然后在pushbutton12_callback中你可以在增强图像上应用otsu方法通过 handles.afterEnhancement
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);
我在 MATLAB
中测试此代码我的方式
I test this codes in my MATLAB
in my way
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);
输出
这篇关于如何在MATLAB GUI中显示所需的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!