Roundness part test with digital microscope, need circle with pass and fail

I’m trying to make a testing conveyor for my job to make things easier. I’ve already got the design and conveyors figured out but i need to figure out how to get the modified webcam (digital microscope) to test with a pass and fail for the roundness of the ends of these parts. the camera will look at the part from above and I want the screen to display the image of the part with a circle. if the part matches up and is round to fill the shape, then i want the pass signal to move the flipper to the left so when the conveyor moves forward and drops the part it will go into the pass box. if the part is not fully round i want the circle to turn red and then move the flipper the other way so that when the conveyor moves the next part into place it will drop the failed part into the fail box.

If I were doing this, my starting point would be:

  1. calibrate the camera intrinsics. The primary reason for this is to model the optical distortion (which I presume is a factor since you are using a webcam)
  2. Calibrate the extrinsics of the camera with respect to the plane that the objects will be in. This is necessary because, in general, the projection of a circle is an ellipse, so you need to be able to account for this.
  3. correct for the radial distortion and perspective distortion using the calibration results from 1 and 2. There is more than one way to do this, but I would use cv::initUndistortRectifyMap with the perspective transform included, and cv::undistort to apply the undistortion all at once.
  4. image processing / blob detection / filtering as needed + cv::fitEllipse
  5. pass/fail based on ellipse

You could take some shortcuts - for example if your lens doesn’t have radial distortion, you might be tempted to skip step 1, but it probably does, and you could end up causing yourself more problems. Similarly if your camera is pointed perpendicular to the object plane, you might be tempted to skip step 2, but again you could be creating problems for yourself if the camera (optical axis) isn’t perpendicular to the object plane.