Simple image processing tasks

RGB colour modell

There is a 'Prgb' picture, with 'n' column-number (Width), 'm' row-number (Height). The colour value in the 'i,j' pixel is

Prgb(i,j) = r(i,j)Red + g(i,j)Green + b(i,j)Blue,

where r(i,j) is the intensity value for the Red band in the (i,j)th pixel
g(i,j) is the intensity value for the Green band in the (i,j)th pixel
b(i,j) is the intensity value for the Blue band in the (i,j)th pixel

The type of intesity is byte, i.e. values can change from 0 to 255.

Greyscale conversion

Pgrey(i,j) = grey(i,j)R + grey(i,j)G + grey(i,j)Blue

where grey(i,j) = [(r(i,j) + g(i,j) + b(i,j)]/3

 public Bitmap grayScale(Bitmap img)
        {
            imageByteArray imba = new imageByteArray();
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;
                    int gray = (byte)((byIn[ind] + byIn[ind + 1] + byIn[ind + 2])/3);
                    byOut[ind + 2] = (byte)gray;//Red
                    byOut[ind + 1] = (byte)gray;
                    byOut[ind] = (byte)gray; 
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }

Inverting color

Prgb(i,j) = (255 - r(i,j))Red + (255 - g(i,j))Green + (255 - b(i,j))Blue

        public Bitmap invertImage(Bitmap img)
        {
            imageByteArray imba = new imageByteArray(); // look at Image to byte array, byte array to bitmap
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;
                    byte bmax = 255;
                    byOut[ind + 2] = (byte)(bmax - byIn[ind + 2]); //Red
                    byOut[ind + 1] = (byte)(bmax - byIn[ind + 1]);  //Green
                    byOut[ind] = (byte)(bmax - byIn[ind]);   //Blue
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }

Histogram equalisation

Histogram equalisation transforms the picture intensity from interval [intensity-min, intensity-max] to [0,255]


  public Bitmap HistoEqualisation(Bitmap img)
        {
            imageByteArray imba = new imageByteArray();
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            //search for min max
            byte[] pmin=new byte[3];
            byte[] pmax = new byte[3];
            for (int i = 1; i < 3; i++) { pmin[i] = 255; }
            for (int i = 1; i < 3; i++) { pmax[i] = 0; }

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;

                    if (byIn[ind] < pmin[0]) { pmin[0] = byIn[ind]; }
                    if (byIn[ind] > pmax[0]) { pmax[0] = byIn[ind]; }

                    if (byIn[ind +1] < pmin[1]) { pmin[1] = byIn[ind+1]; }
                    if (byIn[ind + 1] > pmax[1]) { pmax[1] = byIn[ind+1]; }

                    if (byIn[ind + 2] < pmin[2]) { pmin[2] = byIn[ind + 2]; }
                    if (byIn[ind + 2] > pmax[2]) { pmax[2] = byIn[ind + 2]; }
                }
            }

            //equalisation
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;

                    int pval=255 * (byIn[ind + 2] - pmin[2])/(pmax[2] - pmin[2]);
                    byOut[ind + 2] = (byte)pval;//Red

                    pval=255 * (byIn[ind + 1] - pmin[1])/(pmax[1] - pmin[1]);
                    byOut[ind + 1] = (byte)pval;

                    pval=255 * (byIn[ind] - pmin[0])/(pmax[0] - pmin[0]);
                    byOut[ind] = (byte)pval; 
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }

Colour separation (cat into bands)


        public Bitmap RedBand(Bitmap img)
        {
            imageByteArray imba = new imageByteArray();
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;
                    int col = byIn[ind + 2];
                    byOut[ind + 2] = (byte)col;//Red
                    byOut[ind + 1] = 0;
                    byOut[ind] = 0;
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }


        public Bitmap GreenBand(Bitmap img)
        {
            imageByteArray imba = new imageByteArray();
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;
                    int col = byIn[ind + 1];
                    byOut[ind + 2] = 0;//Red
                    byOut[ind + 1] = (byte)col;
                    byOut[ind] = 0;
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }

        public Bitmap BlueBand(Bitmap img)
        {
            imageByteArray imba = new imageByteArray();
            byte[] byIn = imba.BitmapToByteArray(img);
            byte[] byOut = new byte[byIn.Length];

            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    Int32 ind = i * imba.Stride + j * imba.BytesPerPixel;
                    int col = byIn[ind];
                    byOut[ind + 2] = 0;//Red
                    byOut[ind + 1] = 0;
                    byOut[ind] = (byte)col;
                }
            }
            return imba.ByteArrayToBitmap(byOut, img.Width, img.Height);
        }