垂直直方图& aforge中的水平直方图

时间:2012-05-15 09:12:11

标签: c# aforge

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

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

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

3 个答案:

答案 0 :(得分:1)

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

如果在白色背景上有黑色文本的完美二进制图像,则表示行之间的前导的像素行将全部为白色,因此VerticalIntensityStatistics中的值将为(imageWidth * 255)

答案 1 :(得分:1)

您可以自己绘制图像。

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

答案 2 :(得分: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