【发布时间】:2017-08-11 02:10:42
【问题描述】:
【问题讨论】:
【问题讨论】:
您可以像这样找到每个区域的开始和结束箱
[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
【讨论】:
find(count > (thres + 10*eps(thres)), 'last')?
find(count > (thres + 10*eps(thres)), 1, 'last')
我将使用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);
【讨论】: