[英]Calculating minimum and maximum values of a histogram
I have an image that is grayscale (has a single band) with values ranging from -182 to 94. However, for the most part, the histogram of the image occupies just a narrow range as shown in the diagram below. 我有一幅灰度图像(具有单个波段),其值介于-182到94之间。但是,在大多数情况下,图像的直方图仅占很小的范围,如下图所示。 I have the values of the pixels in an array.
我有一个数组中的像素值。 I want to find the start and end of the histogram (in this case around 22 to 70).
我想找到直方图的起点和终点(在这种情况下,约为22到70)。 Is there any built in function that I can use to do that?
我可以使用任何内置函数吗? I am using C#, Emgu CV and GDAL
我正在使用C#,Emgu CV和GDAL
If I understand correctly, you can pixels as an sbyte array, in which case this works for me: 如果我理解正确,则可以将像素作为sbyte数组,在这种情况下,这对我有用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleMinMax
{
class Program
{
static void Main(string[] args)
{
sbyte[] array1 = { 1, -1, -2, 0, 99, -111 };
MinMax(array1);
void MinMax(sbyte[] array)
{
// Report minimum and maximum values.
Console.WriteLine("max = {0}; min = {1}", array.Max(), array.Min());
}
}
}
}
Assuming your histogram values are in an int[]
called hist
, you can use some LINQ extension methods to find the largest contiguous group of values, and find their start and end positions in the array. 假设直方图值位于称为
hist
的int[]
,则可以使用某些LINQ扩展方法来找到最大的连续值组,并在数组中找到它们的开始和结束位置。 This is overkill if your histogram simply has all zeros with one non-zero area, and does not handle multiple non-zero areas very well - it just picks the longest horizontal span. 如果您的直方图仅具有一个非零区域的全零,并且不能很好地处理多个非零区域,那就太过分了-它只选择最长的水平跨度。
var histPos = hist
.Select((hval, pos) => new { hval, pos })
.GroupByWhile((prev,cur) => prev.hval != 0 && cur.hval != 0)
.MaxBy(zvg => zvg.Count())
.Select(zvg => zvg.pos);
var start = histPos.Min();
var end = histPos.Max();
The extension methods I used are GroupByWhile
which groups sequential objects as long as a boolean lambda returns true, and MaxBy
which returns the object that has the largest return value from a lambda. 我使用的扩展方法是
GroupByWhile
,它对布尔对象lambda返回true的对象进行组合,而MaxBy
从lambda返回具有最大返回值的对象。
public static class IEnumerableExt {
// TKey combineFn((TKey Key, T Value) PrevKeyItem, T curItem):
// PrevKeyItem.Key = Previous Key
// PrevKeyItem.Value = Previous Item
// curItem = Current Item
// returns new Key
public static IEnumerable<(TKey Key, T Value)> ScanPair<T, TKey>(this IEnumerable<T> src, TKey seedKey, Func<(TKey Key, T Value), T, TKey> combineFn) {
using (var srce = src.GetEnumerator()) {
if (srce.MoveNext()) {
var prevkv = (seedKey, srce.Current);
while (srce.MoveNext()) {
yield return prevkv;
prevkv = (combineFn(prevkv, srce.Current), srce.Current);
}
yield return prevkv;
}
}
}
// bool testFn(T prevItem, T curItem)
// returns groups by sequential matching bool
public static IEnumerable<IGrouping<int, T>> GroupByWhile<T>(this IEnumerable<T> src, Func<T, T, bool> testFn) =>
src.ScanPair(1, (kvp, cur) => testFn(kvp.Value, cur) ? kvp.Key : kvp.Key + 1)
.GroupBy(kvp => kvp.Key, kvp => kvp.Value);
public static T MaxBy<T, TKey>(this IEnumerable<T> src, Func<T, TKey> keySelector, Comparer<TKey> keyComparer) => src.Aggregate((a, b) => keyComparer.Compare(keySelector(a), keySelector(b)) > 0 ? a : b);
public static T MaxBy<T, TKey>(this IEnumerable<T> src, Func<T, TKey> keySelector) => src.Aggregate((a, b) => Comparer<TKey>.Default.Compare(keySelector(a), keySelector(b)) > 0 ? a : b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.