Kanishka
Kanishka

Reputation: 361

Find defects in bottle shape using image processing using openCV

I am using Image processing, openCV , C++ to check the misshapes of bottles. I am very new to openCV. It will be a great help if someone can guide me a right direction how to achieve this. How can I detect the defects of the shape of the bottle using opencv and c++. I am giving bottle images as the inputs to the system.when a misshaped bottle is input system should detect it.

Defected bottle image : bad bottle

Good Bottle image : good bottle

Upvotes: 0

Views: 1758

Answers (3)

prestojunkie
prestojunkie

Reputation: 125

You could try the opencv template matching function. From the opencv documentation:

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).

It implements a sliding window scheme, by sliding the template image that we want to find over the source image and calculating a similarity metric that is stored in a result matrix.

In the result matrix, the darkest/brightest location indicates the highest matches (according to the template matching algorithm employed), which marks the position of the best match for the template. The brightest location can be found using the minMaxLoc function on the result matrix.

The signature of the matchTemplate method is as follows:

matchTemplate( image, template, result, match_method ); //Matches the template normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() ); //Normalizes the result double minVal; double maxVal; Point minLoc; Point maxLoc; Point matchLoc; minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() ); //Finds the minimum and maximum values in the result

OpenCV provides several different algorithms for the matching, such as finding the normalized square difference of intensities(CV_TM_SQDIFF_NORMED). For the result matrix obtained using CV_TM_SQDIFF_NORMED, the lowest values correspond to the best matches. For other methods such as normalized cross correlation (CV_TM_CCORR_NORMED), the highest values correspond to the best matches.

In your case, you could threshold the result matrix with a tolerance value for deviation from the template image, and if the result on thresholding is an empty Mat, you could identify the bottle to be defective. You might have to experiment a little to find an appropriate threshold. If you want an exact match, you have to look for 0/1 (according to method) in the result matrix.

You can find more on opencv template matching here.

Hope this helps.

Upvotes: 1

dhanushka
dhanushka

Reputation: 10682

  • Take the region of interest (ROI) and find contours.
  • Find convexhull
  • Find convexity defects
  • Do this for both the reference ROI and the defected ROI, then compare

The comparison would not be straightforward as you may have to establish some correspondence between the regions of the two contours(may be you can use a grid and use its cells as the ROIs - now many ROIs for a single image - to solve the correspondence complexities)

ROI in red:

enter image description here

Grid based approach (multiple ROIs):

enter image description here

Upvotes: 0

QT-ITK-VTK-Help
QT-ITK-VTK-Help

Reputation: 558

Basic approach: you can extract the edges then Register the two images. In openCV you will get couple of filters for this.

Perfect Approach: you can use statistical shape modeling algorithm, I am not sure if it is there in OPenCV.

Upvotes: 1

Related Questions