Ở đây bạn có thể tìm thấy câu trả lời cho Vẽ biểu đồ hình ảnh màu xám sử dụng Emgucv trong bối cảnh phương pháp MVVM WPF .. hy vọng nó sẽ có ích cho một ai đó
MainWindow.xaml
này Chế độ xem
<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
đây là lớp viewmodel
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
đây là mô hình lớp
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;
}
}
Các paremeter đầu tiên của Tính là một mảng của hình ảnh. Nó có nghĩa là tôi có thể cho nó {img2Blue , img2Red, img2Green} và nó tính toán tất cả? Bạn có thể chứng minh cách sử dụng này không? Cảm ơn nhiều. – Gqqnbig
Chỉ cần chỉnh sửa nhanh bài đăng của Chris 'DenseHistogram Histo = new DenseHistogram (255, new RangeF (0, 255)); 'nên là' DenseHistogram Histo = new DenseHistogram (256, new RangeF (0, 256)); sẽ thiếu bất kỳ giá trị nào trong thùng 255 của bạn như tôi vừa trải qua khi tôi tạo một hình ảnh theo hình ảnh. – foofunner