Здесь вы можете найти ответ на Рисование гистограммы серого изображения с помощью Emgucv в контексте метода MVFM WPF. Надеюсь, это будет кому-то полезно
MainWindow.xaml
Это вид
<Window x:Class="Histogram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:HistogramAndOverlayingMVVM.ViewModel"
xmlns:local="clr-namespace:Histogram"
mc:Ignorable="d"
Title="MainWindow" WindowState="Maximized">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Button Content="LoadImage" Command="{Binding OpenImg}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Height="Auto" Margin="31,5,0,0" />
<Button Content="Histogram" Command="{Binding Histogram}"HorizontalAlignment="Left" Margin="135,5,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto"/>
<Image Source="{Binding Image.Source}" HorizontalAlignment="Left" Height="346" Margin="9,39,0,0" VerticalAlignment="Top" Width="546"/>
<Image Source="{Binding Hist.Source}" HorizontalAlignment="Left" Height="346" Margin="569,39,-353.4,0" VerticalAlignment="Top" Width="546"/>
</Grid></Window>
MainWindowViewModel.cs
это класс модели представления
class MainWindowViewModel
{
public ICommand OpenImg { get; set; }
Image<Bgr, byte> imgInput;
Histogram his = new Histogram();
public MainWindowViewModel()
{
OpenImg = new RelayCommand(openImg, (obj) => true);
_image = new System.Windows.Controls.Image();
_hist = new System.Windows.Controls.Image();
}
private void openImg(object obj = null)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png;*.bmp;*.tiff|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
string ImgPath = op.FileName;
imgInput = new Image<Bgr, byte>(ImgPath);
Image.Source = BitmapSourceConvert.ToBitmapSource(imgInput);
}
}
private System.Windows.Controls.Image _image;
public System.Windows.Controls.Image Image
{
get { return _image; }
set
{
_image = value;
}
}
private ICommand _histogram;
public ICommand Histogram
{
set { _histogram = value; }
get
{
if(_histogram == null)
{
_histogram = new RelayCommand(param => DrawHistogram((EventArgs)param));
}
return _histogram;
}
}
private void DrawHistogram(EventArgs e)
{
Bitmap img;
img = his.ApplyHistogram(imgInput);
Hist.Source = BitmapSourceConvert.BitmapToImageSource(img);
}
private System.Windows.Controls.Image _hist;
public System.Windows.Controls.Image Hist
{
get { return _hist; }
set
{
_hist = value;
}
}
}
Histogram.cs
это класс модели
public class Histogram
{
public Bitmap ApplyHistogram(Image<Bgr,byte> imgInput)
{
Image<Gray, Byte> src = imgInput.Convert<Gray, byte>();
DenseHistogram hist = new DenseHistogram(256, new RangeF(0.0f, 255f));
hist.Calculate(new Image<Gray, byte>[] { src }, true, null);
// Get the max value of histogram
double minVal = 0.0;
double maxVal = 0.0;
Point minLoc = new Point();
Point maxLoc = new Point();
CvInvoke.MinMaxLoc(hist, ref minVal, ref maxVal, ref minLoc, ref maxLoc);
// Scale histogram
const int width = 256;
const int height = 250;
var histData = hist.GetBinValues();
Bitmap histo = DrawHistogram(maxVal, width, height, histData);
return histo;
}
private static Bitmap DrawHistogram(double maxVal, int width, int height, float[] histData)
{
Bitmap histo = new Bitmap(width, height);
Graphics g = Graphics.FromImage(histo);
g.Clear(SystemColors.Window);
Pen penGray = new Pen(Brushes.DarkGray);
for (var i = 0; i < histData.GetLength(0); i++)
{
var val = (float)histData.GetValue(i);
val = (float)(val * (maxVal != 0 ? height / maxVal : 0.0));
Point s = new Point(i, height);
Point e = new Point(i, height - (int)val);
g.DrawLine(penGray, s, e);
}
return histo;
}
}