views:

33

answers:

1

Hello! I have a problem with getting the pixels from an image. I load a image, select a pixel from the image and retrieve it's color and then i generate a matrix indexMatrix[bitmap_height][bitmap_width] which contains 1 or 0 depending if the [x,y] color of the bitmap is the same as the color selected. The problem is that the program doesn't select all the pixels although it should. It only retrieves a part of them ( i am sure the pixels 'forgotten' are the same color as the selected color )

The wierd thing is that if i run my program for the new image ( the one constructed from the matrix ) it returns the same image ( as it should ) but i can't figure out how to fix the problem.

Please Help!!!

Regards, Alex Badescu and some code from my project :

bitmap declaration:

m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);

Here i calculate the matrix:

int bitmapWidth = m_Bitmap.Width;
        int bitmapHeight = m_Bitmap.Height;


        indexMatrix = new int[bitmapHeight][];

        if (imageIsLoaded && colorIsSelected)
        {
            for (int i = 0; i < bitmapHeight; i++)
            {
                indexMatrix[i] = new int[bitmapWidth];

                for (int j = 0; j < bitmapWidth; j++)
                {
                    Color temp = m_Bitmap.GetPixel(j, i);
                    if (temp == selectedColor)
                        indexMatrix[i][j] = 1;
                    else indexMatrix[i][j] = 0;


                }
            }
            MessageBox.Show("matrix generated succesfully");
        }
        matrixIsCalculated = true;
    }
+1  A: 

There is no obvious failure mode here. Other than that the pixel isn't actually a match with the color. Being off by, say, only one in the Color.B value for example. You cannot see this with the unaided eye.

These kind of very subtle color changes are quite common when the image was resized. An interpolation filter alters the color subtly, even if not strictly needed. Another failure mode is using a compressed image format like JPEG, the compression algorithm changes colors.

Hans Passant
i've created a .bmp in paint and the program works fine. The problem is that , i understood , .jpg applies a subtle change in color to some pixels when converted . Is there anyway to go around this ?
Badescu Alexandru
Hot dog, I love it when I guessed the problem right without any hint. Dealing with jpegs is a lossy proposition in your case. Just don't bother, the png format is nice. Compact *and* no random color changes.
Hans Passant