Getting the coordinates from a point from an image

4.4k views Asked by At

I am creating a program that can print out the x- & y- Coordinates from a certain pixel. There is a function like 'GetPixel', this will however get the RGB codes from a given coordinate. What I want is just the vice versa, so I have already the RGB codes and now I'm doing a threshold through my Image to know whether it contains a Color pixel that I desired or not.

This is my code:

So firstly I will upload an image:

    public BitmapImage bitmap;

    public void hochladen_Click(object sender, EventArgs e)
    {
        // Create OpenFileDialog 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Set filter for file extension and default file extension 
        dlg.DefaultExt = ".bmp";
        dlg.Filter = "BMP Files (*.bmp)|*.bmp";

        // Get the selected file name and display in a TextBox 
        if (dlg.ShowDialog() == true)
       { 
            // Open document 
            string filename = dlg.FileName;
            bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(filename);
            bitmap.EndInit();
            image.Source = bitmap;
        }
    }

Then when I click a button in my application, it should do a threshold from my Image, and I am going to detect a red Point (R = 255, G = B = 0)

    public Color c;

    private void detektieren_Click(object sender, RoutedEventArgs e)
    {
        double x = bitmap.Width;
        double y = bitmap.Height;
        bl.Content = x + "x" + y;

So from this Point on, it shouldn't be difficult to find the coordinate:

        for (int i = 0; i < x; i++)
        {
            for (int j = 0; i < j; j++)
            {
                if (c.R == 255 && c.G == 0 && c.B == 0)
                {
                    //
                }
            }
        }
    }

Anyone has idea? Thanks in advance.

2

There are 2 answers

0
King King On BEST ANSWER

Finding pixels matching a RGB value of course may return many pixels, try the following code to get all the pixels represented by Point structure:

public Point[] GetPixelsFromRGB(byte[] rgbData, int stride, Color colorToFind){
  int k = stride/4;
  return rgbData.Select((x,i)=>new{x,i})
                .GroupBy(a=>a.i/4,(key,a)=>a.ToArray())
                .Where(g=>g[0].x == colorToFind.Red &&
                          g[1].x == colorToFind.Green &&
                          g[2].x == colorToFind.Blue && g[3].x == 255)
                .Select(g=> new Point(g[0].i%k, g[0].i / k)).ToArray();
}
//Use this method to get the rgbData
int stride = bitmap.PixelWidth * 4;
byte[] rgbData = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(rgbData, stride, 0);
//then call the method above:
var pixels = GetPixelsFromRGB(rgbData, stride, Colors.Red);

Note that the code above has not been tested, I just typed directly into the answer editor.

0
user2754279 On

After a Little bit modification, it works. So this is my code:

    public void detektieren_Click(object sender, RoutedEventArgs e)
    {
        for (i = 0; i < bitmap.Height; i++)
        {
            for (j = 0; j < bitmap.Width; j++)
            {
                stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
                data = new byte[stride * bitmap.PixelHeight];
                bitmap.CopyPixels(data, stride, 0);
                index = i * stride + 4 * j;

                byte A = data[index + 3];
                byte R = data[index + 2];
                byte G = data[index + 1];
                byte B = data[index];

                // Create a writer and open the file:
                StreamWriter Messdaten;
                if (!File.Exists("C:/Users/.../Messdaten.csv"))
                {
                    Messdaten = new StreamWriter("C:/Users/.../Messdaten.csv");
                }
                else
                {
                    Messdaten = File.AppendText("C:/Users/.../Messdaten.csv");
                }

                // Write to the file:
                Messdaten.WriteLine(index + ";" + A + ";" + R + ";" + G + ";" + B);

                // Close the stream:
                Messdaten.Close();

                if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
                {
                    // Create a writer and open the file:
                    StreamWriter Messdaten2;
                    if (!File.Exists("C:/Users/.../Messdaten2.csv"))
                    {
                       Messdaten2 = new StreamWriter("C:/Users/.../Messdaten2.csv");
                    }
                    else
                    {
                        Messdaten2 = File.AppendText("C:/Users/.../Messdaten2.csv");
                    }

                    // Write to the file:
                    Messdaten2.WriteLine(index + ";" + i + ";" + j);

                    // Close the stream:
                    Messdaten2.Close();
                }
            }
        }
    }

In the first Excel file (Messdaten.csv), all RGB values from a each single Pixel will be shown. In the second one (Messdaten2.csv) it will Show all Pixels that -let's take for instance- have a value A=0,R=0,G=0,B=255 (= Blue).

Now, how do I create a sum & mean of Pixel i and Pixel j (they're set of values) ? Tried to do this:

                if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
                {
                    int x_sum = 0; int y_sum = 0;
                    int x_count = 0; int y_count = 0;
                    int x_mw = 0; int y_mw = 0;

                    x_sum = x_sum + i;
                    x_count++;
                    y_sum = y_sum + j;
                    y_count++;

                    x_mw = x_sum / x_count;
                    y_mw = y_sum / y_count;
                }

But why it didn't work? The x_sum and y_sum only Show the last value of Pixel i and j, and the x_count and y_count (as presumed) show only the value of 1. What did I wrong?