2011-11-20 41 views

Trả lời

6

Đoạn mã sau sẽ phân đoạn dữ liệu biểu đồ RED GREEN và BLUE và đặt chúng vào một mảng phao cho bất kỳ mục đích sử dụng nào bạn muốn.

float[] BlueHist; 
float[] GreenHist; 
float[] RedHist; 

Image<Bgr, Byte> img = new Image<Bgr, byte>("ImageFileName"); 

DenseHistogram Histo = new DenseHistogram(255, new RangeF(0, 255)); 

Image<Gray, Byte> img2Blue = img[0]; 
Image<Gray, Byte> img2Green = img[1]; 
Image<Gray, Byte> img2Red = img[2]; 


Histo.Calculate(new Image<Gray, Byte>[] { img2Blue }, true, null); 
//The data is here 
//Histo.MatND.ManagedArray 
BlueHist = new float[256]; 
Histo.MatND.ManagedArray.CopyTo(BlueHist, 0); 

Histo.Clear(); 

Histo.Calculate(new Image<Gray, Byte>[] { img2Green }, true, null); 
GreenHist = new float[256]; 
Histo.MatND.ManagedArray.CopyTo(GreenHist, 0); 

Histo.Clear(); 

Histo.Calculate(new Image<Gray, Byte>[] { img2Red }, true, null); 
RedHist = new float[256]; 
Histo.MatND.ManagedArray.CopyTo(RedHist, 0); 

và điều này sẽ làm biểu đồ xám:

float[] GrayHist; 

Image<Gray, Byte> img_gray = new Image<Gray, byte>("ImageFileName"); 

Histo.Calculate(new Image<Gray, Byte>[] { img_gray }, true, null); 
//The data is here 
//Histo.MatND.ManagedArray 
GrayHist = new float[256]; 
Histo.MatND.ManagedArray.CopyTo(GrayHist, 0); 

Hope this helps,

Chúc mừng,

Chris

[Chỉnh sửa]

Để vẽ biểu đồ, bạn sẽ cần phải sử dụng hoặc bạn sở hữu hoặc một điều khiển được thiết kế như Zedgraph (Điều này được cung cấp với EMGU) ở đây là một bài viết rất hay về codeproject cho thấy nó sử dụng.

http://www.codeproject.com/KB/graphics/zedgraph.aspx

Cheers

Chris

+0

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

+1

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

2

Hiển thị Histograms trong Emgu là siêu dễ dàng và vui vẻ. Chỉ cần thực hiện một điều khiển biểu đồ trên biểu mẫu của bạn, sau đó gọi điều này trong vòng lặp của bạn và bạn đã hoàn tất.

 histogramBox1.ClearHistogram(); 
     histogramBox1.GenerateHistograms(frame, 256); 
     histogramBox1.Refresh(); 
0

Tridimensional histogram

Image<Bgr, Byte>[] inp = new Image<Bgr, byte>("fileName.jpg"); 
int nBins = 256; 
DenseHistogram hist = new DenseHistogram(new int[] { nBins, nBins, nBins }, new RangeF[] { new RangeF(0, 255), new RangeF(0, 255), new RangeF(0, 255) }); 
hist.Calculate(inp.Split(), false, null); 

// To get value of single bin 
int b = 255; int g = 0; int r = 0; //blue 
int count = Convert.ToInt32(hist.MatND.ManagedArray.GetValue(b, g, r)); //count = no of pixels in color Bgr(b,g,r) 

//To get all values in a single array 
List<Tuple<Bgr, int>> histVal = new List<Tuple<Bgr, int>>(nBins * nBins * nBins); 
for (int i = 0; i < nBins; i++) 
    for (int j = 0; j < nBins; j++) 
     for (int k = 0; k < nBins; k++) 
      histVal.Add(new Tuple<Bgr, int>(new Bgr(i, j, k), Convert.ToInt32(hist.MatND.ManagedArray.GetValue(i, j, k)))); 

Monodimensional histogram

int nBins = 256; 
float[] valHist = new float[nBins]; 
Image<Gray, Byte>[] inp = new Image<Gray, byte>("fileName.jpg"); 
DenseHistogram hist = new DenseHistogram(nBins, new RangeF(0, 255)); 
hist.Calculate(new Image<Gray, Byte>[] { inp }, true, null); 
hist.MatND.ManagedArray.CopyTo(valHist,0); 
0

Điều quan trọng là làm theo các thủ tục để thêm Emgu.CV.UI.dll vào hộp công cụ của bạn trong Windows Forms để để sử dụng tất cả các điều khiển Windows Forms mà Emgu CV cung cấp (bao gồm HistogramBox).

Trước hết, bạn cần mở biểu mẫu của mình trong chế độ xem thiết kế. Từ Hộp công cụ, nhấp chuột phải vào không gian trống của cột 'Chung'. Điều này sẽ bật lên một trình đơn lựa chọn, nơi lựa chọn 'Chọn mục' có sẵn, xem hình ảnh dưới đây.

Designer Form View

Sau đó, nhấp vào 'Chọn mục'; bạn sẽ thấy hộp thoại 'Chọn Hộp công cụ'. Từ đó, nhấp vào nút 'Duyệt ..' ở góc dưới bên phải của hộp thoại. tập tin

enter image description here

Chọn 'Emgu.CV.UI.dll' từ hộp thoại 'Open', nhấp vào nút 'Open'. Bây giờ bạn sẽ thấy điều khiển ImageBox đã được thêm vào hộp thoại 'Chọn hộp công cụ'. Nhấp vào 'Ok'. Sau đó, bạn nên lưu ý các điều khiển sau được thêm vào Hộp công cụ của bạn (Áp dụng cho phiên bản 3.10 của Emgu. Một số phiên bản khác của Emgu có thể có các điều khiển khác hoặc thiếu các điều khiển được đề cập bên dưới.)

  • HistogramBox
  • ImageBox
  • MatrixBox
  • PanAndZoomPictureBox.

ToolBoxControls

Sau đó, bạn sẽ có thể kéo và thả để hình của bạn như bạn thấy phù hợp với Windows Forms điều khiển Emgu CV đã được xây dựng nó. Hoặc bạn có thể sử dụng chúng cách lập trình:

Form frm = new Form(); 
var img = CvInvoke.Imread(this.PictureBox.ImageLocation, Emgu.CV.CvEnum.LoadImageType.Grayscale).ToImage<Gray, Byte>(); 

HistogramBox histo = new HistogramBox(); 

histo.ClearHistogram(); 
histo.GenerateHistograms(img, 256); 
histo.Dock = DockStyle.Fill; 
histo.Refresh(); 

frm.Controls.Add(histo); 

frm.ShowDialog(); 

Câu trả lời này được lấy cảm hứng trong Add Image Box Control hướng dẫn.

0

Ở đâ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 vấn đề liên quan