【问题标题】:Split histogram into different regions in Matlab在Matlab中将直方图拆分为不同的区域
【发布时间】:2017-08-11 02:10:42
【问题描述】:

我有一个使用 imhist 的图像直方图,它包含 3 个不同的区域,如附图所示,我想获得直方图最大连续区域的边界或间隔,在这种情况下,第二个区域是我正在寻找的边界是 43 和 225

【问题讨论】:

    标签: matlab histogram


    【解决方案1】:

    您可以像这样找到每个区域的开始和结束箱

    [counts,binLocations] = imhist(I);
    der = diff([false; counts>0; false]);
    upedge = find(der == 1);
    downedge = find(der == -1) - 1;
    regions = [binLocations(upedge) binLocations(downedge)];
    

    如果这些值不完全为零,但非常接近于零,那么您可以将0 替换为上述代码中的某个阈值。

    例子

    im = uint8(zeros(300,400));
    im(1:100,:) = uint8(randi([0,40],[100,400]));
    im(101:200,:) = uint8(randi([90,100],[100,400]));
    im(201:300,:) = uint8(randi([140,240],[100,400]));
    
    [counts,binLocations] = imhist(im);
    der = diff([false; counts>0; false]);
    upedge = find(der == 1);
    downedge = find(der == -1) - 1;
    regions = [binLocations(upedge) binLocations(downedge)];
    

    结果

    regions =
    
         0    40
        90   100
       140   240
    

    【讨论】:

    • 非常感谢您的回答,但它给了我 0 和 255,因为事实上,直方图有 3 个区域,第一个区域用于 binLocations=0 第二个区域,其中 binLocations 介于 43 和 225 之间这是我正在寻找的一个,第三个是 binLocations=255
    • 修改后的答案。将适用于宽度宽度为 1 的区域,如您描述的 0 和 255。如果有更多的地区,也会分裂成更多的地区。
    • 为什么不直接使用find(count > (thres + 10*eps(thres)), 'last')
    • @Yvon 因为那只会找到最后一个区域的结尾。我假设你的意思是find(count > (thres + 10*eps(thres)), 1, 'last')
    【解决方案2】:

    我将使用question 的答案来查找数组中连续非零元素的区域。

    假设我们有这个数组(直方图):

    h = [0,0,0,1,2,3,44,77,5,656,0,0,0,0,0,0,2,99,7,34];
    

    现在我们想知道每个连续非零元素的区域是否开始和结束,在这个例子中我们想要

    startIndex = [4,17]
    endIndex   = [10,20]
    lengths    = [7,4]
    

    为了得到这个结果,我们使用问题中的代码如下:

    dsig = diff([1,h(:)'==0,1]);
    startIndex = find(dsig < 0);
    endIndex = find(dsig > 0)-1;
    duration = endIndex-startIndex+1;
    

    并获得最长的区域使用:

    [~,maxLengthIndex] = max(lengths);
    maxStartIndex = startIndex(maxLengthIndex);
    maxEndIndex = endIndex(maxLengthIndex);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      相关资源
      最近更新 更多