我有一个图像,使用aforgenet直方图,我能够以数组形式获取垂直和水平直方图值。我可以基于它创建一个图形,但是我想基于垂直和水平在同一图像上以不同的颜色显示该值。简而言之,我想根据其直方图值在图像上绘制数据。
// get image statistics
AForge.Imaging.ImageStatistics statistics =
new AForge.Imaging.ImageStatistics( image );
// get the red histogram
AForge.Math.Histogram histogram = statistics.Red;
// get the values
double mean = histogram.Mean; // mean red value
double stddev = histogram.StdDev; // standard deviation of red values
int median = histogram.Median; // median red value
int min = histogram.Min; // min red value
int max = histogram.Max; // max value
// get 90% range around the median
AForge.IntRange range = histogram.GetRange( 0.9 );
答案 0 :(得分:0)
AForge.net有一个名为IPLab的示例应用程序。
它具有以下源代码以表格形式显示直方图:
public class HistogramWindow : Content
{
#region Windows form designer generated code
//.... ... ...
#endregion
#region gather stats
//... ... ...
#endregion
private static Color[] colors = new Color[]
{
Color.FromArgb(192, 0, 0),
Color.FromArgb(0, 192, 0),
Color.FromArgb(0, 0, 192),
Color.FromArgb(128, 128, 128),
};
private int currentImageHash = 0;
private ImageStatistics stat;
private AForge.Controls.Histogram histogram;
private AForge.Math.Histogram activeHistogram = null;
public HistogramWindow()
{
InitializeComponent();
}
// selection changed in channels combo
private void channelCombo_SelectedIndexChanged( object sender, System.EventArgs e )
{
if ( stat != null )
{
SwitchChannel( ( stat.IsGrayscale ) ? 3 : channelCombo.SelectedIndex );
}
}
// Switch channel
public void SwitchChannel( int channel )
{
if ( ( channel >= 0 ) && ( channel <= 2 ) )
{
if ( !stat.IsGrayscale )
{
histogram.Color = colors[channel];
activeHistogram = ( channel == 0 ) ? stat.Red : ( channel == 1 ) ? stat.Green : stat.Blue;
}
}
else if ( channel == 3 )
{
if ( stat.IsGrayscale )
{
histogram.Color = colors[3];
activeHistogram = stat.Gray;
}
}
if ( activeHistogram != null )
{
histogram.Values = activeHistogram.Values;
meanLabel.Text = activeHistogram.Mean.ToString( "F2" );
stdDevLabel.Text = activeHistogram.StdDev.ToString( "F2" );
medianLabel.Text = activeHistogram.Median.ToString( );
minLabel.Text = activeHistogram.Min.ToString( );
maxLabel.Text = activeHistogram.Max.ToString( );
}
}
// Cursor position changed over the hostogram
private void histogram_PositionChanged( object sender, AForge.Controls.HistogramEventArgs e )
{
int pos = e.Position;
if ( pos != -1 )
{
levelLabel.Text = pos.ToString( );
countLabel.Text = activeHistogram.Values[pos].ToString( );
percentileLabel.Text = ( (float) activeHistogram.Values[pos] * 100 / stat.PixelsCount ).ToString( "F2" );
}
else
{
levelLabel.Text = "";
countLabel.Text = "";
percentileLabel.Text = "";
}
}
// Selection changed in the hostogram
private void histogram_SelectionChanged( object sender, AForge.Controls.HistogramEventArgs e )
{
int min = e.Min;
int max = e.Max;
int count = 0;
levelLabel.Text = min.ToString( ) + "..." + max.ToString( );
// count pixels
for ( int i = min; i <= max; i++ )
{
count += activeHistogram.Values[i];
}
countLabel.Text = count.ToString( );
percentileLabel.Text = ( (float) count * 100 / stat.PixelsCount ).ToString( "F2" );
}
// On "Log" check - switch mode
private void logCheck_CheckedChanged( object sender, System.EventArgs e )
{
histogram.IsLogarithmicView = logCheck.Checked;
}
}
您可以检查和重用源代码。很简单。