MATLAB:在选定的直方图区域内寻找最小值

MATLAB: Finding minimum within a selected histogram region

所以我在 Matlab 中创建了一个图像直方图,用户可以使用 imrect select 想要的区域。

我希望能够找到用户 selects 所在区域的最小 y 值。

这是我目前的情况:

handles.pet_hist_rect = imrect(gca);
[xmin ymin width height] = getPosition(handles.pet_hist_rect);
xdata = get(findobj(gcf,'type','patch'),'xdata');
ydata = get(findobj(gcf,'type','patch'),'ydata');

我不确定如何提取 [xmin, xmin+width] 范围内的最小 y 值(从 ydata)

提前致谢!

我认为您的代码首先没有 运行,因为您试图将 getPosition(一个 1x4 数组)的输出分配给另一个数组的单个条目,但这是行不通的.更正为

position = getPosition(handles.pet_hist_rect);

您现在可以访问 xmin 作为位置 (1),ymin 作为位置 (2) 等等。 现在,ymin = position(2) 已经是您所要求的(y 的最小值),但我不确定,我会在这里找到您。无需查询图形属性。如果这不是您要查找的内容,您将不得不稍微改写一下问题。

编辑:以下是超级粗略的,应该有效,但如果任何直方图计数为零,则无效!

close all;
hist(rand(1000, 1));
handles.pet_hist_rect = imrect(gca);
position = getPosition(handles.pet_hist_rect);
xdata = get(findobj(gcf,'type','patch'),'xdata');
ydata = get(findobj(gcf,'type','patch'),'ydata');
x = xdata{1};
x(x < position(1))=0;
x(x > position(1) + position(3))=0;
x(x>0) = 1;
y = ydata{1}(logical(x));
y(y==0) = NaN;
m = min(y);