INFO 290T Assignment 7

You are tasked with writing the software for a parts inspection system. The input to this system is a color image of a circular part. Locate and fit a circle (center and radius) to the part. You will use the expectation/maximization (EM) algorithm to simultaneously locate the boundary of the circular part and fit a circle to the part.

To begin, your system should load a color (RGB) image, convert the image to grayscale and extract the salient edges in the image. You can use Canny edge detector from Python's OpenCV library:

     skimage.feature.canny(image=im_gray, sigma=2.5).astype(np.int32)

This function will return a binary image with a value of 1 at a pixel location corresponding to a salient edge and 0 otherwise. Extract the x- and y-coordinates of all pixels with an edge value equal to 1 (see numpy's argwhere). It is from these coordinates that we will find and fit a circle.

The coordinates of each edge pixel correspond to one of two models: (1) a circle with center (cx,cy) and radius (r); or (2) an "outlier" model. The probability of belonging to model 1 will be modeled as a Gaussian distribution (g(d) = exp(-d2/s), where d is the distance of each pixel to the circle. The probability of belonging to model 2 will be modeled as a uniform distribution and fixed to be 1/10.

You should begin your EM iterations by initializing (cx,cy) to be the image center, r=190 pixels, and s=128.

In the E-step, the residual error for model 1 is based on the shortest distance, d, between a point (x,y) and the current estimate of the circle with center (cx,cy) and radius r. This distance is: d = abs( sqrt( (x-cx)2 + (y-cy)2 ) - r).

In the M-step, you will use weighted total least-squares to re-estimate the paramters of the circle. Implement the total least-squares estimator described in Section 2 of this paper. The weights for this weighted total least-squares estimator are computed in the E-step.

After performing the E- and M-step, update the value of s as described in lecture.

Repeatedly perform the E- and M-steps until the difference between subsequent estimates of the circle center and radius are each less than 0.1 pixels.

On each EM iteration you should display two images: (1) the current circle superimposed on top of the original RGB image; and (2) the current probability of each pixel location belonging to model 1 visualized as an image.

Show your final fitting results on the five images provided here. If your solution is similar to ours, it will fail on the image disc4.jpg. Briefly explain what is causing this failure and how you would fix it. This code snippet will allow you to display as an animation images on each iteration of your EM loop.

     from IPython.display import clear_output
     from time import sleep

     plt.imshow( im_gray )
     plt.show() 
     sleep(0.5)
     clear_output(wait=True)