Jump to content

Image processing application

akio123008
Go to solution Solved by 0x21,
byte r = pixelColor.R;
byte g = pixelColor.G;
byte b = pixelColor.B;

 

Hello everyone,

I'm quite new to C# programming and I'm working on a program that takes an image and tells the user some information about "color statistics". (average color, dominant color perhaps some histogram eventually) I've got the following code right now that simply iterates through the pixels of the image, obtains their color information and prints it in the console:

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Bitmap test = new Bitmap(@"*path*");

            for (int i = 0; i < test.Width; i++)
            {
                for (int j = 0; j < test.Height; j++)
                {
                    Color pixelColor = test.GetPixel(i, j);
                    Console.WriteLine(pixelColor);

                }

            }
           

        }
    }
}

 

But the thing is, I want to do some processing on the RGB values that come out, therefore I'd like to have some way of converting the RGB values from pixelColor into a variables, so that I can store them in arrays and do the work.  What would be the simplest way to do this?

Link to comment
Share on other sites

Link to post
Share on other sites

byte r = pixelColor.R;
byte g = pixelColor.G;
byte b = pixelColor.B;

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think this would depend on how you want to process things later. (Do you want the data as just a single stream of rgb data, or maintain the 2d shape of the image...combine the rgb data or separate into an r, g, and b)

I assume based on what you have posted that you don't want to just store the values as an array of color.

 

(3d array for an image) [separate rgb and preserve the structure]

byte img[,,] = new byte[4,imagewidth,imageheight];
//other code
//inner loop
img[0, x, y] = color.R
img[1, x, y] = color.G
img[2, x, y] = color.B
img[4, x, y] = color.A

(2d array)

byte img[,] = new byte[4,imagewidth*imageheight];
//other code
//inner loop
img[0, x + y*imagewidth] = color.R //x + y*imagewidth makes sure there are no collisions
...

 

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

I highly recommend you convert your colors from RGB to a different 'color space' such as HSL so you can track, analyze, and manipulate the colors in a more intuitive way.  In HSL, you have the H channel carrying the hue (color), the S carrying the saturation, and the L carrying the lightness/luminance/brightness.  With these other color spaces, you can very easily see if two colors are simply brighter or washed out versions of each other.

Link to comment
Share on other sites

Link to post
Share on other sites

if it's for personal use, you could go one step further and convert your BMP to RAW (for example freeware IrfanView can save a picture to a RAW format where you have 3 bytes for each pixel) and then you can simply use file read functions to read the bytes directly from the file 

you could define an array with 3 dimensions colors[ 255,255,255] with an unsigned int or something as data type for each ... then read 3 bytes at a time from the raw file and increment colors [byte 1, byte 2 , byte 3 ] by 1 to track how many pixels had that color.

Then at the end just walk the array and increment a value each time an element in array is different than 0 and you get the number of unique colors

 

You could read a bmp directly as well, the format is fairy simple, could jump over the header and you'd only have to take in account that each row has to have a multiple of 4 bytes, so there's some padding with null bytes , and also the issue that bitmaps are saved from bottom to top.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×