如何制作图像直方图?

时间:2015-10-20 03:41:58

标签: c#

我必须在红色,绿色和蓝色平面上制作图像直方图。

我有这个代码,但它是在Visual Basic中我需要它在C# 我需要打开一个图像,然后我必须制作直方图。

Public Class Form1

Public Function histogramaAcumulado(ByVal bmp As Bitmap) As Integer(,)
    'Creamos una matriz que contendrá el histograma acumulado
    Dim Rojo, Verde, Azul As Byte 'Declaramos tres variables que almacenarán los colores
    Dim matrizAcumulada(2, 255) As Integer
    For i = 0 To bmp.Width - 1 'Recorremos la matriz
        For j = 0 To bmp.Height - 1
            Rojo = bmp.GetPixel(i, j).R 'Asignamos el color
            Verde = bmp.GetPixel(i, j).G
            Azul = bmp.GetPixel(i, j).B
            'ACumulamos los valores. 
            matrizAcumulada(0, Rojo) += 1
            matrizAcumulada(1, Verde) += 1
            matrizAcumulada(2, Azul) += 1
        Next
    Next
    Return matrizAcumulada
End Function

Dim histoAcumulado As Integer(,) 'Variable que almacenará los histogramas
Private Sub Form1_(Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Cargamos los histogramas
    Dim bmp As New Bitmap(PictureBox1.Image)
    histoAcumulado = histogramaAcumulado(bmp)
    'Ejecutamos el botón del histograma rojo
    Button1_Click(sender, e)
End Sub

'Histograma rojo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Borramos el posible contenido del chart
    Chart1.Series("Histograma").Points.Clear()
    'Los ponesmos del colores correspondiente
    Chart1.Series("Histograma").Color = Color.Red
    For i = 0 To 255
        Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado(0, i))
    Next
End Sub
'Histograma verde
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Borramos el posible contenido del chart
    Chart1.Series("Histograma").Points.Clear()
    Chart1.Series("Histograma").Color = Color.Green
    For i = 0 To 255
        Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado(1, i))
    Next
End Sub
'Histograma azul
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    'Borramos el posible contenido del chart
    Chart1.Series("Histograma").Points.Clear()
    Chart1.Series("Histograma").Color = Color.Blue
    For i = 0 To 255
        Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado(2, i))
    Next
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

  

我在C#中需要它我需要打开一个图像,对于那个图像我必须制作直方图

如果我理解你的问题,那基本上就是将VB.NET转换为c#。原始代码已经创建了直方图,可以在histogramaAcumulado()下面看到。

注意:通常在SO上我们更喜欢源代码是英语。我没有进行任何语言翻译。

附加说明:原始代码也会引用未定义的Chart1成员,从而将下面的代码标记为无法编译以及原始代码。我把它留作OP的练习,以进一步探索原始的VB代码来破译它的含义。

请在下面找到c#代码:

public class Form1
{
    public int[,] histogramaAcumulado(Bitmap bmp)
    {
        //Creamos una matriz que contendrá el histograma acumulado
        byte Rojo = 0;
        byte Verde = 0;
        byte Azul = 0;
        //Declaramos tres variables que almacenarán los colores
        int[,] matrizAcumulada = new int[3, 256];
        //Recorremos la matriz
        for (i = 0; i <= bmp.Width - 1; i++)
        {
            for (j = 0; j <= bmp.Height - 1; j++)
            {
                Rojo = bmp.GetPixel(i, j).R;
                //Asignamos el color
                Verde = bmp.GetPixel(i, j).G;
                Azul = bmp.GetPixel(i, j).B;
                //ACumulamos los valores. 
                matrizAcumulada[0, Rojo] += 1;
                matrizAcumulada[1, Verde] += 1;
                matrizAcumulada[2, Azul] += 1;
            }
        }
        return matrizAcumulada;
    }

    //Variable que almacenará los histogramas
    int[,] histoAcumulado;
    private void Form1_Load(object sender, EventArgs e)
    {
        //Cargamos los histogramas
        Bitmap bmp = new Bitmap(PictureBox1.Image);
        histoAcumulado = histogramaAcumulado(bmp);
        //Ejecutamos el botón del histograma rojo
        Button1_Click(sender, e);
    }

    //Histograma rojo
    private void Button1_Click(object sender, EventArgs e)
    {
        //Borramos el posible contenido del chart
        Chart1.Series("Histograma").Points.Clear();
        //Los ponesmos del colores correspondiente
        Chart1.Series("Histograma").Color = Color.Red;
        for (i = 0; i <= 255; i++)
        {
            Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado[0, i]);
        }
    }
    //Histograma verde
    private void Button2_Click(object sender, EventArgs e)
    {
        //Borramos el posible contenido del chart
        Chart1.Series("Histograma").Points.Clear();
        Chart1.Series("Histograma").Color = Color.Green;
        for (i = 0; i <= 255; i++)
        {
            Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado[1, i]);
        }
    }
    //Histograma azul
    private void Button3_Click(object sender, EventArgs e)
    {
        //Borramos el posible contenido del chart
        Chart1.Series("Histograma").Points.Clear();
        Chart1.Series("Histograma").Color = Color.Blue;
        for (i = 0; i <= 255; i++)
        {
            Chart1.Series("Histograma").Points.AddXY(i + 1, histoAcumulado[2, i]);
        }
    }
    public Form1()
    {
        Load += Form1_Load;
    }

}

转换工具

为了您的兴趣,代码是通过SharpDevelop Snippet Converter转换的。

相关问题