Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> 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; } </code></pre> <p>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 <a href="https://stackoverflow.com/questions/3983508/verify-image-sequence">this</a> problem. </p> <h2>Edit</h2> <p>VB people like full examples, maybe do C# as well.</p> <p><img src="https://i.stack.imgur.com/g7ZAs.png" alt="enter image description here"></p> <p>Drop 2 buttons and a picture box onto a form, and use code:</p> <pre><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); } } } </code></pre> <p>Yes, this works and colors are balanced correctly. </p> <p>But if you want an alternative, that has effective luminance, then:</p> <pre><code> 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}}); </code></pre> <h2>Edit 2: @Hans Passant.</h2> <p>Bitmap consists of pixels, that have color value <strong>RGBA</strong> {<strong>R</strong> ed, <strong>G</strong> reen, <strong>B</strong> lue, <strong>A</strong> lpha transparency}. To get grayscaled image from colored one, I multiply each pixel color value with my defined <code>colorMatrix</code>. </p> <p>Normal <a href="http://en.wikipedia.org/wiki/Matrix_multiplication" rel="nofollow noreferrer">2 matrix multiplying</a> speed is Θ(n^2), but this GDI+ linear conversion uses <a href="http://mathworld.wolfram.com/FastFourierTransform.html" rel="nofollow noreferrer">Fast Fourier Transform</a> to do it in Θ(n log(n)). This means for larger images, its vastly faster then other methods. </p> <p>Lets say I have input pixels <code>In</code>, with values <strong>{R, G, B, A}</strong> and I would like to get the formula for pixel values after matrix multiplication, and lets call it <code>Out</code> with values <strong>{A<sub>out</sub>, B<sub>out</sub>, C<sub>out</sub>, D<sub>out</sub>}</strong>.</p> <p><img src="https://i.stack.imgur.com/k9Xue.png" alt="enter image description here"></p> <p>Out:</p> <ul> <li>A<sub>out</sub> = r(4) A + r(3) B + r(2) G + r(1) R</li> <li>B<sub>out</sub> = g(4) A + g(3) B + g(2) G + g(1) R</li> <li>C<sub>out</sub> = b(4) A + b(3) B + b(2) G + b(1) R</li> <li>D<sub>out</sub> = a(4) A + a(3) B + a(2) G + a(1) R</li> </ul> <p>Or in this case:</p> <p><img src="https://i.stack.imgur.com/sKLMd.png" alt="enter image description here"></p> <pre><code>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 } </code></pre> <p>Where formula of effective luminance of grayscale image is <code>0.11 blue + 0.59 green + 0.3 red</code>. So it's correct.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload