如何使用PHP检测图像上的圆形颜色

I need to detect whether or not circle is painted over. I tried construction like this:

for ($i=1; $i <= 10; $i++) { //FOR LINE
  for ($line=1; $line <= 4; $line++) { //FOR COLUMN
      $rgb = imagecolorat($img, $x_start, $y_start);
      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;
      //Detect black or white - how?
      $y_start -= 50;

  }
  $x_start += 52;
  $y_start = 1023;
  echo '-----------------------------<br>';
}

But despite the fact that I don't know how to recognize color, some pixels point to number in circles, therefore it write that this circle is black, but, in fact, it is white. How to fix this problem and how to detect black color? Thank you.

View attached file

  1. As there is no real colour information, convert the image to greyscale first to simplify your processing.

  2. Then blur the image slightly so that tiny variations in the scanning are smoothed out.

  3. Then threshold the image so that it only consists of pure black and white tones and all the values will be 0 or 255 and your task will be simple.

Here I used ImageMagick to do the above steps like this:

convert score.jpg -rotate 90 -blur 0x2 -threshold 50% result.png

enter image description here

As an alternative on step 2, you could use a small 3x3 or 5x5 median filter to fill any holes in the pencilled in circles. Another option might be a morphological dilation to fill the holes.

Also, watch out for students who fill in more than one circle ;-)

depends on how much energy you want to invest. if you have position of each field fixed (scans are all the same), you don't need to do some position checking (using those black squares in the corners) and can just make bigger samples and calculate average color in each sample (sample being one answer field). if average color of a sample is black, it is probably filled. but some studends may use cross as a way to fix their answer and then this solution is not perfect.