从for循环中调用Matlab滑块

从for循环中调用Matlab滑块,matlab,function,image-processing,slider,Matlab,Function,Image Processing,Slider,我创建了一个Matlab项目,其中从文件中读取图像,然后使用预定义的阈值将其转换为二进制。对于类似图像的整个文件夹重复此for循环 我正在尝试实现一个滑块,这样我可以使用滑块实时更改阈值,从而调整二进制图像。我在确定在何处定义滑块的uicontrol以及从循环中的何处/如何调用滑块时遇到问题 我有滑块的uicontrol: uicontrol(... 'tag', 'fff',... 'style', 'slider',... 'callback', @ui_

我创建了一个Matlab项目,其中从文件中读取图像,然后使用预定义的阈值将其转换为二进制。对于类似图像的整个文件夹重复此for循环

我正在尝试实现一个滑块,这样我可以使用滑块实时更改阈值,从而调整二进制图像。我在确定在何处定义滑块的uicontrol以及从循环中的何处/如何调用滑块时遇到问题

我有滑块的uicontrol:

uicontrol(...
      'tag', 'fff',...
      'style', 'slider',...
      'callback', @ui_slider_Callback,...
      'position', [20 20 200 50],...
      'tooltipstring', 'Colormap scaling relative to actual slice',...
      'Max', 250,...
      'Min', 0,...
      'value', 230,...
      'SliderStep', [0.002, 0.002]);
我也知道某个地方需要这条线,但我不确定是否需要定义滑块函数:

thresholdValue = get(hObject,'Value');
我的代码的简化版本如下:

function

yourfolder=path name;
d=dir([yourfolder '\*.jpg']);
files={d.name};

for q=1:numel(files);

    I = imread(files{q});

    J = rgb2gray(I);

    thresholdValue = 230;

    binaryImage = J < thresholdValue;

    imshow(binaryImage);

    drawnow;

end

end
函数
yourfolder=路径名;
d=dir([yourfolder'\*.jpg']);
文件={d.name};
对于q=1:numel(文件);
I=imread(文件{q});
J=rgb2灰色(I);
阈值=230;
二进制图像=J<阈值;
imshow(二进制图像);
现在抽;
结束
结束
诚然,我对函数和调用函数的知识非常有限,但如果能提供任何帮助,我将不胜感激。

您需要定义函数。一种方法是将其定义在与GUI相同的
*.m
文件中,MATLAB调用

一个小的功能示例:

function testcode
% Initialize GUI
h.fig = figure('MenuBar', 'none', 'ToolBar', 'none');
h.ax = axes( ...
    'Parent', h.fig, ...
    'Units', 'Normalized', ...
    'Position', [0.1 0.15 0.8 0.8] ...
    );
h.slide = uicontrol( ...
    'tag', 'fff', ...
    'style', 'slider', ...
    'Units', 'Normalized', ...
    'position', [0.05 0.03 0.9 0.05], ...
    'tooltipstring', 'Colormap scaling relative to actual slice', ...
    'Max', 250, ...
    'Min', 0, ...
    'value', 230, ...
    'SliderStep', [0.002, 0.002] ...
    );

% Dummy image
I = imread('ngc6543a.jpg');
h.img = rgb2gray(I);
thresholdValue = get(h.slide, 'Value');
binaryImage = h.img < thresholdValue;
imshow(binaryImage, 'Parent', h.ax);

% Need to set callback after all our elements are initialized
set(h.slide, 'Callback', {@ui_slider_Callback, h});

end

function ui_slider_Callback(~, ~, h)
    thresholdValue = get(h.slide, 'Value');
    binaryImage = h.img < thresholdValue;
    imshow(binaryImage, 'Parent', h.ax);
    drawnow
end
函数测试代码
%初始化GUI
h、 图=图(“菜单栏”、“无”、“工具栏”、“无”);
h、 ax=轴(。。。
‘父’,h.fig。。。
'单位','标准化'。。。
“位置”[0.1 0.15 0.8 0.8]。。。
);
h、 幻灯片=uicontrol(。。。
'标记','fff'。。。
'样式','滑块'。。。
'单位','标准化'。。。
‘位置’,[0.05 0.03 0.9 0.05]。。。
“tooltipstring”、“相对于实际切片的颜色贴图缩放”。。。
'最大',250。。。
“Min”,0。。。
“价值”,230。。。
'滑块步骤',[0.002,0.002]。。。
);
%虚拟图像
I=imread('ngc6543a.jpg');
h、 img=rgb2gray(I);
thresholdValue=get(h.slide,'Value');
二进制图像=h.img<阈值;
imshow(二进制图像,'Parent',h.ax);
%需要在初始化所有元素后设置回调
set(h.slide,'Callback',{@ui\u slider\u Callback,h});
结束
函数ui\u滑块\u回调(~,~,h)
thresholdValue=get(h.slide,'Value');
二进制图像=h.img<阈值;
imshow(二进制图像,'Parent',h.ax);
刷新屏幕
结束
默认情况下,回调总是传递两个变量,调用对象的句柄和
eventdata
结构,其内容各不相同。正如回调文档中所解释的,您可以通过将所有内容包装到单元格数组中来向回调传递额外的输入。需要注意的一点是,传递给回调函数的变量的值是它在定义回调函数时的值。因此,在初始化所有图形对象之后,您将看到我定义了回调


我明确地使用了滑块句柄,而不是使用
hObj
来获取阈值。这纯粹是个人喜好,任何一种方法都很好。

太好了!正是我想要的。非常感谢你的帮助!