locked
Convert DXGI_FORMAT_B8G8R8A8_UNORM to grayscale

    Question

  • I've set up a camera interface that gives me frames as unsigned char (byte) arrays, each pixel is in the DXGI_FORMAT_B8G8R8A8_UNORM format. How can I convert it to grayscale?

    Right now I'm using this simple function, but I have some doubts about the way I extract the B-G-R components from the byte array (I don't know if is the right procedure when working with *_UNORM formats):

    void convertToGrayscale(byte* src, byte* dest, const int len) {
    	int i, ii;
    	int r, g, b;
    	for (i = 0, ii = 0; i < len; ++i, ii += 4)
    	{
    		b = src[ii];
    		g = src[ii + 1];
    		r = src[ii + 2];
    
    		dest[i] = (byte)((r * 0.21f) + (g * 0.72f) + (b * 0.07f));
    	}
    }


    Alessandro Francesconi http://www.alessandrofrancesconi.it


    Tuesday, August 19, 2014 10:02 AM

All replies

  • The simplest solution is to just average R, G, and B.

    dest[i] = (byte)((r + g + b) / 3 );

    BTW, you should take a look at the DirectXTex library.

    Tuesday, August 19, 2014 6:23 PM
  • The way I calculate the gray value it's not the real problem (btw the formula I use is the luminosity, that's better than a simpler average).

    As I wrote in the OP, I want to know if I'm reading the RGB values in the right way.


    Alessandro Francesconi http://www.alessandrofrancesconi.it

    Thursday, August 21, 2014 9:41 AM