How to find Black Pixel locations

蹲街弑〆低调 提交于 2019-12-10 14:10:43

问题


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.


回答1:


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.




回答2:


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;
}



回答3:


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.



来源:https://stackoverflow.com/questions/11751255/how-to-find-black-pixel-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!