如何在图像中找到黑点坐标(X,Y)

时间:2018-05-12 21:11:35

标签: c# image ocr coordinate

我在扫描图像中有3个黑点,其大小为600x400像素,我想知道C#中最好和最简单的方法来获取它们的坐标,如下所示:

dot1 X=400px Y=100px
dot2 X=100px Y=200px
dot3 X=300px Y=300px

enter image description here

1 个答案:

答案 0 :(得分:1)

我将采取远景并假设您只有两种颜色,如图所示:黑点和白色背景。

您可以解决这个问题并检测黑色的出现,这类似于算法的开始:

int HEIGHT = 400;
int WIDTH = 600;

// get the jpg image
Bitmap bitmap;
using(Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open )){
     Image image = Image.FromStream(bmpStream);
     bitmap = new Bitmap(image);
}

for (int x = 0; x < HEIGHT; x++){
  for (int y = 0; y < WIDTH; y++){
    Color pixelColor = bitmap.GetPixel(x, y);
    // check if it's black or a shade of black
    // e.g. if it belongs to an array of colors..etc
    // if so, record the coordinates (x,y)
  }
}