Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use a gray image that getpixel() command should give me the low number for black region and high value for white region. But don't work finely for me and give me incorrect value for every region.

For example in some white region this code give me number 1(related to black) and in the some black region give me 150 or more.

ANYONE CAN HELP ME??

What I have tried:

I use this code in dotspatial instead of picturebox:

public partial class formMain : Form
{

    Bitmap b = new Bitmap(@"F:\Hrit1\MSG1MostO.bmp");

    private void mapMain_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {

        Coordinate c= mapMain.PixelToProj(new System.Drawing.Point(e.X , e.Y ));

        int pixel = Convert.ToInt32(c.X);
        int line = Convert.ToInt32(c.Y);
        if (line < 0 || pixel < 0 || line > 850 || pixel > 1145)
        {
            line = 0;
            pixel = 0;
        }

        Color col = b.GetPixel(pixel, line);

        textBox1.Text = col.ToString();

}
  }
Posted
Updated 17-Apr-18 3:19am
v3

1 solution

GetPixel() returns a Color Structure (System.Drawing)[^]. With gray scale images all RGB components will have the same value. So use one of the R, G, and B properties to get the intensity. The returned value is a byte where 0 is black and 255 is white.
 
Share this answer
 
Comments
Member 13623647 17-Apr-18 9:16am    
That's right.
for example: one of my output in textbox1 is "color [A=255,R=85,G=85,B=85]".
when I use this code :
"textBox1.Text = col.R.ToString();"
I receive number 85.
But my question is another thing.
I say when I use above code to read pixel value getpixel() don't work finely.
For example in some white region above code give me number 1(related to black) and in the some black region give me 150 or more.
Thanks for your contribution .

can you solve my problem?
F-ES Sitecore 17-Apr-18 9:30am    
It could be something to do with the image you're using. Create a new image that you know is pure white or pure black and experiment on that.
Jochen Arndt 17-Apr-18 9:32am    
Sorry I overlooked the DotSpatial.

But how do you know it is wrong?

Have you opened the file in a bitmap editor and checked the pixel at the same position? I would guess that you will get the same value.

Even regions detected as "white" might contain some dark pixels and vice versa.
Member 13623647 17-Apr-18 9:42am    
I verify this image in Qgis and for locations that I give low number in my code, Qgis give the proportional and logical value of its color.I think problem is from my code. Do you have any idea ?
Jochen Arndt 17-Apr-18 9:52am    
I'm sorry, I have no idea and don't know QGis.

But all you have is a bitmap file. That stores pixels as [A]RGB line wise and you retrieve the color at a specific point. That will be same when opening the file with a bitmap editor like MS Paint and checking at the same position (your line and pixel variables).

If the result is not as expected it might be sourced elsewhere in your code (probably in other mapMain class methods) or the PixelToProj() conversion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900