本文介绍了colorvalue中的灰度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从相机获得每个像素的灰度值,格式为23453 ...(16位值)然后我用.pmp为设置pixelcolor

I get from a camera greyscale values for each pixel in the format 23453... (16 bit values)And I set the pixelcolor for my .bmp with

image.SetPixel(cols, rows, color);

但是如何将灰度值转换为正确的颜色值?这样可以正确显示图片的灰度值吗?

but how can I get my greyscale value into a correct color value?So the picture can be correct be displayed in grayscalevalues?

谢谢

推荐答案

 public static Bitmap getGrayscale(Bitmap hc){
        Bitmap result = new Bitmap(hc.Width, hc.Height);
        ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
            new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});

        using (Graphics g = Graphics.FromImage(result)) {
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetColorMatrix(colorMatrix);
            g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
               0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
        }
        return result;
    }

这实际上是一个非常复杂的问题.在我的示例中,基本上,我创建了一个过滤器并将其应用于现有的位图,并进行了一些精巧的矩阵计算.我前一阵子解决了这个问题,试图解决这个问题.

That is actually quite complex problem. In my example, basically I create a filter and apply it to the existing bitmap, with some nifty matrix calculation. I solved it while ago, trying to solve this problem.

VB人们喜欢完整的示例,也许也可以使用C#.

VB people like full examples, maybe do C# as well.

将2个按钮和一个图片框拖放到窗体上,并使用代码:

Drop 2 buttons and a picture box onto a form, and use code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace gray
{
    public partial class Example : Form
    {
        public Example()
        {
            InitializeComponent();
        }

        public static Bitmap getGrayscale(Bitmap hc)
        {
            Bitmap result = new Bitmap(hc.Width, hc.Height);
            ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0.5f,0.5f,0.5f,0,0},
            new float[]{0.5f,0.5f,0.5f,0,0}, new float[]{0,0,0,1,0,0},
            new float[]{0,0,0,0,1,0}, new float[]{0,0,0,0,0,1}});

            using (Graphics g = Graphics.FromImage(result))
            {
                ImageAttributes attributes = new ImageAttributes();
                attributes.SetColorMatrix(colorMatrix);
                g.DrawImage(hc, new Rectangle(0, 0, hc.Width, hc.Height),
                   0, 0, hc.Width, hc.Height, GraphicsUnit.Pixel, attributes);
            }
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open Image File";
            fDialog.Filter = "PNG Files|*.png|Bitmap Files|*.bmp";
            fDialog.InitialDirectory = @"C:\";

            if (fDialog.ShowDialog() == DialogResult.OK)
                this.pictureBox1.ImageLocation = fDialog.FileName.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image == null)
            {
                MessageBox.Show("Sorry no image to alter.");
                return;
            }
            this.pictureBox1.Image = getGrayscale((Bitmap)this.pictureBox1.Image);
        }
    }
}

是的,这可以正常工作并且颜色可以正确平衡.

Yes, this works and colors are balanced correctly.

但是,如果您想要具有有效亮度的替代方法,则:

But if you want an alternative, that has effective luminance, then:

    ColorMatrix colorMatrix = new ColorMatrix(new float[][]{   
                              new float[]{ 0.3f, 0.3f, 0.3f,0,0},
                              new float[]{0.59f,0.59f,0.59f,0,0},
                              new float[]{0.11f,0.11f,0.11f,0,0},
                              new float[]{    0,    0,    0,1,0,0},
                              new float[]{    0,    0,    0,0,1,0},
                              new float[]{    0,    0,    0,0,0,1}});

@Hans Passant.

位图由具有颜色值 RGBA { R ed, G reen, B lue的像素组成, A lpha透明}.为了从彩色图像中获得灰度图像,我将每个像素颜色值与定义的colorMatrix相乘.

Edit 2: @Hans Passant.

Bitmap consists of pixels, that have color value RGBA {R ed, G reen, B lue, A lpha transparency}. To get grayscaled image from colored one, I multiply each pixel color value with my defined colorMatrix.

正常 2矩阵相乘的速度为Θ(n ^ 2),但此GDI +线性转换使用快速傅里叶变换以Θ(n log(n))进行.这意味着对于较大的图像,其速度比其他方法快得多.

Normal 2 matrix multiplying speed is Θ(n^2), but this GDI+ linear conversion uses Fast Fourier Transform to do it in Θ(n log(n)). This means for larger images, its vastly faster then other methods.

让我们说我有输入像素In,其值为 {R,G,B,A} ,我想在矩阵乘法后得到像素值的公式,并将其称为Out的值为 {A ,B ,C ,D } .

Lets say I have input pixels In, with values {R, G, B, A} and I would like to get the formula for pixel values after matrix multiplication, and lets call it Out with values {A, B, C, D}.

出局:

  • A = r(4)A + r(3)B + r(2)G + r(1)R
  • B = g(4)A + g(3)B + g(2)G + g(1)R
  • C = b(4)A + b(3)B + b(2)G + b(1)R
  • D = a(4)A + a(3)B + a(2)G + a(1)R
  • A = r(4) A + r(3) B + r(2) G + r(1) R
  • B = g(4) A + g(3) B + g(2) G + g(1) R
  • C = b(4) A + b(3) B + b(2) G + b(1) R
  • D = a(4) A + a(3) B + a(2) G + a(1) R

或者在这种情况下:

Out = {
  0.11 B + 0.59 G + 0.3 R,
  0.11 B + 0.59 G + 0.3 R,
  0.11 B + 0.59 G + 0.3 R,
  A,
  0,
  0
}

其中灰度图像的有效亮度的公式为0.11 blue + 0.59 green + 0.3 red.没错.

Where formula of effective luminance of grayscale image is 0.11 blue + 0.59 green + 0.3 red. So it's correct.

这篇关于colorvalue中的灰度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:45