Pill Detection System

Hello Readers,
This is a blog where I’ll be talking about my pill detection system project. This system identfied faulty and damaged pills within a thresholded tolerance level. Anything above the threshold level was to be rejected and scraped off.
Note: This is just an example of the system I built while interning at a company. The real code and in detail process cannot be revealed as it has been deemed confidential by the organization.

The system worked as follows:-

  1. The pills which were to be scanned were released from a funnel on a conveyer belt.

  2. Attached to the conveyer belt is a camera which captures the image of the pills. You can refer to the image below.

  3. There are two images attached below the first one is the desired output and the second images is an image where the pill with an orange stain should be rejected to the color difference.

Data

Data

After capturing the images, using certain segmentation techniques the faulty pills were to be identified and rejected.

The Process

The first step is to extract the image. It is necessay to convert the image into a grayscale representation, thus by making it easier to access the region of interest. Data This is the grayscale image.

After converting the image to a grayscale image, I have applied thresholding to the grayscale image, to deal with binary represetation of the image.

I would be using the thresholded image to draw contours, bounding boxes and deal with the dimensions of the pills.This is how I retrieved a thresholded image.
Data

Have a look at the image retrieved from the code cell above.

Data

After this I have used the threshold image to identify edges and gradients around the objects.

Data
This is made easy by tracking changes horizontally and vertically throughout the images. The edges are marked around the object when the scanning window/ kernel slides over the images and moves from the black to the white background and vice versa (i.e the kernel notices the color changes when the window slides over the white and the black colors).

All the major part of retrieving the region of interest is completed. Now the challenging part is to identify the pills with the undesired type. I have started this step by drawing contours around the image.
Data

This is how the output looks like. Data

After this comes the main step where I have to identify the faulty pills. Firstly i have calculated the total area occupied by the contours of the pills. After that i have planned to operated only with the pills, thus setting a conmdition of area>1000.
The arguments x,y,w,h refer to the x-axis, y-axis, width and height respectively. Using these arguments, I have applied basic geometry and mathematics to get to the centre of the pills, where I’ll be checking the color of the pill.
After getting to the centre of the pill I have checked and applied a condition which states if the pixel at that certain point of the image(centre of the pill) has a value of red > 200(pixels are represented in BGR format) should be rejected. This has been shown by drawing a red contour around the pill. The final output can be seen below. Data