【问题标题】:Vertical histogram & horizontal histogram in aforgeaforge中的垂直直方图和水平直方图
【发布时间】:2017-01-31 16:35:59
【问题描述】:

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

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

但它不起作用,我看不到直方图。

【问题讨论】:

    标签: c# aforge


    【解决方案1】:

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

    如果您有一个白色背景上的黑色文本的完美二值图像,则表示行之间前导的像素行将全是白色,因此它们在VerticalIntensityStatistics 中的值将是(imageWidth * 255)。

    【讨论】:

      【解决方案2】:

      您可以自己绘制图像。

      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。

      【讨论】:

        【解决方案3】:

        这是我的做法,我知道这是一个旧线程,但它会帮助其他正在寻找答案的人:

        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

        【讨论】:

          猜你喜欢
          • 2013-06-19
          • 2012-10-30
          • 1970-01-01
          • 2013-05-18
          • 1970-01-01
          • 1970-01-01
          • 2014-04-15
          • 1970-01-01
          相关资源
          最近更新 更多