How to Find Black Pixel Locations

I am working on a strange project. I have access to the laser cutter that I use to make stencils (from metal). I can use the coordinates to program the machine to cut out a specific image, but I was wondering: how can I write a program that would take a scanned image that was black and white and give me the coordinates of the black area ? I don’t mind if it gives every pixel, although I only need external lines, I can do this part.

I searched for this for a while, but there are a lot of words in the question with a lot of results, such as colors and pixels, which I find a lot of information that does not matter. I would like to use C ++ or C #, but I can use any language, including scripts.

+5
source share
3 answers

If we assume that the scanned image is completely white and completely black, without intermediate colors, then we can just take the image as an array of rgb values ​​and just look at 0 values. If the value is 0, should it be black on the right? However, the image will probably not be completely black, so you need a room for maneuver.

What you do then will look something like this:

    for(int i = 0; i < img.width; i++){
       for(int j = 0; j < img.height; j++){
          // 20 is an arbitrary value and subject to your opinion and need.
          if(img[i][j].color <= 20)
             //store i and j, those are your pixel location
       }
     }

, #, , . , ++.

. , .

+3

GetPixel C#:

public List<String> GetBlackDots()
{
    Color pixelColor;
    var list = new st<String>();
    for (int y = 0; y < bitmapImage.Height; y++)
    {
        for (int x = 0; x < bitmapImage.Width; x++)
        {
            pixelColor = bitmapImage.GetPixel(x, y);
            if (pixelColor.R == 0 && pixelColor.G == 0 && pixelColor.B == 0)
                list.Add(String.Format("x:{0} y:{1}", x, y));
        }
    }
    return list;
}
+3

, , , , . Python PIL (Python Imaging Library - http://www.pythonware.com/products/pil/), , .

Here is an example of what can help you get started.

image = Image.open("image.png")
datas = image.getdata()

for item in datas:
    if item[0] < 255 and item[1] < 255 and item[2] < 255 :
        // THIS PIXEL IS NOT WHITE

Of course, this will count any pixel that is not completely white, you might want to add some addition, so pixels that are not EXACT white are also turned white. You will also need to keep track of which pixel you are currently viewing.

+2
source

All Articles