2012-05-15 166 views
1

如何获取文本图像的垂直直方图或水平直方图以将文本分割为线条和连接词? 我用aforge有:aforge中的垂直直方图和水平直方图

HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(pic);    
Histogram histogram = his.Gray; 

但它没有工作,我无法看到的柱状图。

回答

1

如果不将直方图插入某种图表组件,您将无法看到某些东西。 AForge生成的Histogram是每行(对于HorizontalIntensityStatistics)或列(对于VerticalIntensityStatistics)的值的总和(在Values属性中表示为int的数组)。

如果你有一个白底黑字的完美的二值图像,代表线之间的领先像素行将是全白的,所以他们在VerticalIntensityStatistics值将是(imageWidth * 255)

0

这里是我做到了,我知道这是一个古老的线程,但它会帮助别人谁正在寻找的答案:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using AForge.Imaging; 
using WeifenLuo.WinFormsUI.Docking; 

namespace ImageFunctions.Forms 
{ 
    public partial class FrmHistogram : DockContent 
    { 
     public delegate void HistogramStatus(string Message); 
     public event HistogramStatus histogramStatus; 
     public delegate void HistogramCompleted(string Message); 
     public event HistogramCompleted histogramCompleted; 

     private Stopwatch swHistogram = new Stopwatch(); 
     private bool IsHorizontalIntensity { get; set; } 
     private string CurrentImage = null; 
     private System.Windows.Forms.DataVisualization.Charting.Chart chart = new System.Windows.Forms.DataVisualization.Charting.Chart(); 

     public FrmHistogram() 
     { 
      InitializeComponent(); 
      chart.Dock = DockStyle.Fill; 
      chart.BackColor = Color.LightYellow; 
      chart.ChartAreas.Add("Default"); 
      this.Controls.Add(chart); 
      IsHorizontalIntensity = true; // Default. 
     } 

     protected override string GetPersistString() 
     { 
      return this.Text; 

     } 

     #region Histogram 

     /// <summary> 
     /// Build the Histogram for Supplied Image 
     /// </summary> 
     /// <param name="image"> 
     /// String: Path to image that histogram is to be created out of 
     /// </param> 
     public void DoHistogram(string image) 
     { 
      CurrentImage = image; // Used for re-generating the histogram 
      bool IsGrayScale = AForge.Imaging.Image.IsGrayscale(new Bitmap(image)); 
      dynamic IntensityStatistics = null; // Use dynamic (a little like var) to assign this variable which maybe of different types. 

      swHistogram.Reset(); 
      swHistogram.Start(); 
      histogramStatus("Creating Histogram"); 

      AForge.Math.Histogram grayhist; 
      AForge.Math.Histogram Redhist; 
      AForge.Math.Histogram Greenhist; 
      AForge.Math.Histogram Bluehist; 
      // collect statistics 
      //NOTE: We have to use the braces on these statements see: http://stackoverflow.com/questions/2496589/variable-declarations-following-if-statements 
      if (IsHorizontalIntensity) 
      { 
       histogramStatus("Using HorizontalIntensityStatistics"); 
       IntensityStatistics = new HorizontalIntensityStatistics(new Bitmap(image)); 
      } 
      else 
      { 
       histogramStatus("Using VerticalIntensityStatistics"); 
       IntensityStatistics = new VerticalIntensityStatistics(new Bitmap(image)); 
      } 

      // get gray histogram (for grayscale image) 
      if (IsGrayScale) 
      { 
       grayhist = IntensityStatistics.Gray; 
       //TODO: DoGrayHistogram(); 
       histogramStatus("Grayscale Histogram"); 
      } 
      else 
      { 

       Redhist = IntensityStatistics.Red; 
       Greenhist = IntensityStatistics.Green; 
       Bluehist = IntensityStatistics.Blue; 
       DoRGBHistogram(Redhist, Greenhist, Bluehist); 
       histogramStatus("RGB Histogram"); 
      } 

      swHistogram.Stop(); 
      histogramCompleted("Histogram built in " + swHistogram.Elapsed); 

     } 

     private void DoRGBHistogram(AForge.Math.Histogram RedHist, AForge.Math.Histogram GreenHist, AForge.Math.Histogram BlueHist) 
     { 


      // Decide which set of values are placed at back, in the middle and to the front of the graph. 
      List<double> lis = new List<double>(); 
      lis.Add(RedHist.Mean); 
      lis.Add(GreenHist.Mean); 
      lis.Add(BlueHist.Mean); 
      lis.Sort(); 

      try 
      { 
       chart.Series.Add("Red"); 
       chart.Series.Add("Green"); 
       chart.Series.Add("Blue"); 

       // Set SplineArea chart type 
       chart.Series["Red"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea; 
       chart.Series["Green"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea; 
       chart.Series["Blue"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea; 
       // set line tension 
       chart.Series["Red"]["LineTension"] = "0.8"; 
       chart.Series["Green"]["LineTension"] = "0.8"; 
       chart.Series["Blue"]["LineTension"] = "0.8"; 
       // Set colour and transparency 
       chart.Series["Red"].Color = Color.FromArgb(50, Color.Red); 
       chart.Series["Green"].Color = Color.FromArgb(50, Color.Green); 
       chart.Series["Blue"].Color = Color.FromArgb(50, Color.Blue); 
       // Disable X & Y axis labels 
       chart.ChartAreas["Default"].AxisX.LabelStyle.Enabled = false; 
       chart.ChartAreas["Default"].AxisY.LabelStyle.Enabled = false; 
       chart.ChartAreas["Default"].AxisX.MinorGrid.Enabled = false; 
       chart.ChartAreas["Default"].AxisX.MajorGrid.Enabled = false; 
       chart.ChartAreas["Default"].AxisY.MinorGrid.Enabled = false; 
       chart.ChartAreas["Default"].AxisY.MajorGrid.Enabled = false; 
      } 
      catch (Exception) 
      { 
       // Throws an exception if it is already created. 
      } 
      chart.Series["Red"].Points.Clear(); 
      chart.Series["Green"].Points.Clear(); 
      chart.Series["Blue"].Points.Clear(); 

      foreach (double value in RedHist.Values) 
      { 
       chart.Series["Red"].Points.AddY(value); 
      } 
      foreach (double value in GreenHist.Values) 
      { 
       chart.Series["Green"].Points.AddY(value); 
      } 
      foreach (double value in BlueHist.Values) 
      { 
       chart.Series["Blue"].Points.AddY(value); 
      } 

     } 

     #endregion 

     /// <summary> 
     /// If the Horizontal Intensity is checked then set the IsHorizontalIntensity to true 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void horizontalIntensityMenuItem_Click(object sender, EventArgs e) 
     { 
      ToggleMenu(); 
      if (horizontalIntensityMenuItem.Checked) IsHorizontalIntensity = true; 
      DoHistogram(CurrentImage); 
     } 

     /// <summary> 
     /// The two options Horizontal & Vertical are Mutually exclusive this make sure that both options are not checked at the same time 
     /// One is checked as default - the other is not, so they will swap around. 
     /// </summary> 
     private void ToggleMenu() 
     { 
      horizontalIntensityMenuItem.Checked = !horizontalIntensityMenuItem.Checked; // Toggle this 
      verticalIntensityMenuItem.Checked = !verticalIntensityMenuItem.Checked; // Toggle this 
     } 

     /// <summary> 
     /// If the Horizontal Intensity is checked then set the IsHorizontalIntensity to False 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void verticalIntensityMenuItem_Click(object sender, EventArgs e) 
     { 
      ToggleMenu(); 
      if (horizontalIntensityMenuItem.Checked) IsHorizontalIntensity = false; 
      DoHistogram(CurrentImage); 
     } 




    } 
} 

DoHistogram()获取直方图值 DoRGBHistogram()实际上副本将RGB值转换为表单上的图表。 您将从本系列或文章中获得更多信息:http://code.msdn.microsoft.com/Professional-Image-35101193

0

您可以自己绘制图像。

public static Bitmap GetImage(this AForge.Math.Histogram hist) { 
     var img = new Emgu.CV.Image<Gray,byte>(hist.Values.Length, 100,new Gray(255)); 
     for (int i = 0; i < hist.Values.Length; i++) { 
      CvInvoke.Line(img, new Point(i,100), new Point(i,100 - (int)(hist.Values[i]/hist.Max)), new MCvScalar(0, 0, 0)); 
     } 
     return img.Bitmap;} 

在这段代码中我已经使用EmguCV,但你可以使用System.Drawing方法。看看这answer