Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Black Pixel locations

I'm working on a strange project. I have access to a laser cutter that I am using to make stencils (from metal). I can use coordinates to program the machine to cut a certain image, but what I was wondering was: 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 areas? I don't mind if it gives every pixel even though I need only the outer lines, I can do that part.

I've searched for this for a while, but the question has so many words with lots of results such as colors and pixels, that I find tons of information that isn't relevant. I would like to use C++ or C#, but I can use any language including scripting.

like image 739
trueCamelType Avatar asked Aug 01 '12 01:08

trueCamelType


3 Answers

I used GetPixel in 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;
}
like image 102
Ria Avatar answered Oct 13 '22 23:10

Ria


If we assume that the scanned image is perfectly white and perfectly black with no in-between colors, then we can just take the image as an array of rgb values and simply scan for 0 values. If the value is 0, it must be black right? However, the image probably won't be perfectly black, so you'll want some wiggle room.

What you do then would 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
       }
     }

Now if you use C#, it'll be easy to import most image formats, stick em in an array, and get your results. But if you want faster results, you'd be better off with C++.

This shortcut relies completely on the image values being very extreme. If large areas of your images are really grey, then the accuracy of this approach is terrible.

like image 40
RGroppa Avatar answered Oct 13 '22 23:10

RGroppa


While there are many solutions in many languages, I'll outline a simple solution that I would probably use myself. There is a imaging great library for Python called PIL (Python Imaging Library - http://www.pythonware.com/products/pil/) which could accomplish what you need very easily.

Here's an example of something that might 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 that will count any pixel that is not completely white, you might want to add some padding so pixels which are not EXACTLY white also get picked up as being white. You'll also have to keep track of which pixel you are currently looking at.

like image 35
mash Avatar answered Oct 13 '22 22:10

mash