I wanted to dig a bit further into image processing and seeing what else I could do to automatically detect labels. Please read part 1, part 2, part 3 and part 4 first, if you haven't already.
My previous methods focused on histograms using masks but this only works well if the image is perfectly centered and cropped properly. This is not always the case, as I already showed in the last articles. It would work better if I knew where the label would be in the image, so I can create a different mask for the right part of the image and then work with histograms.
Detecting where a label starts can be done with edge detection. This should work in most cases, because the label has a different colour than the vinyl. In OpenCV there are various ways to detect outer edges and I am going to use the "Canny edge detector".
For this I first translate the image to greyscale, and then run the edge detector (for the threshold values I used 40 and 200). For one of my testing images that looks like this:
and when plotting the edges on the original:
which is completely useless. The edge detector found 434 edges, which is not what I want.
As you can see there is quite a bit of noise, for example some dust in the lower left corner, some grooves in the upper left corner and a number etched in the runout groove in the lower right corner. First I want to get rid of this noise, and then try again.
Many tutorials suggeted I should use a Gaussian blur before running the edge detector. So, after running a Gaussian blur (kernel size: 5x5) I get the following:
which plotted on the original image looks like this:
This is still not what I want: 274 edges were detected. So I looked a bit further and discovered the median blur. Adrian from Pyimagesearch called it "most effective when removing salt-and-pepper noise" which sounded like a good method to try to get rid of the dust particles.
With a kernel size of 7 I get 225 edges (still not what I wanted), and with a kernel size of 9 only one outer edge is detected, which is exactly what I was looking for:
and when plotting the outer edge on the image:
Success! With my histogram masking algorithm I got bad results with a few images, so I revisited them with the edge detector, with good results, for example:
Edge detection didn't work with every of my test images though. This was mostly due the colour of the labels and the vinyl being very close. Nevertheless it seems like edge detection can be a very effective tool.
Next step: finding the radius and center, which I am going to cover in a future blog post.
My previous methods focused on histograms using masks but this only works well if the image is perfectly centered and cropped properly. This is not always the case, as I already showed in the last articles. It would work better if I knew where the label would be in the image, so I can create a different mask for the right part of the image and then work with histograms.
Detecting where a label starts can be done with edge detection. This should work in most cases, because the label has a different colour than the vinyl. In OpenCV there are various ways to detect outer edges and I am going to use the "Canny edge detector".
For this I first translate the image to greyscale, and then run the edge detector (for the threshold values I used 40 and 200). For one of my testing images that looks like this:
and when plotting the edges on the original:
which is completely useless. The edge detector found 434 edges, which is not what I want.
As you can see there is quite a bit of noise, for example some dust in the lower left corner, some grooves in the upper left corner and a number etched in the runout groove in the lower right corner. First I want to get rid of this noise, and then try again.
Many tutorials suggeted I should use a Gaussian blur before running the edge detector. So, after running a Gaussian blur (kernel size: 5x5) I get the following:
which plotted on the original image looks like this:
This is still not what I want: 274 edges were detected. So I looked a bit further and discovered the median blur. Adrian from Pyimagesearch called it "most effective when removing salt-and-pepper noise" which sounded like a good method to try to get rid of the dust particles.
With a kernel size of 7 I get 225 edges (still not what I wanted), and with a kernel size of 9 only one outer edge is detected, which is exactly what I was looking for:
and when plotting the outer edge on the image:
Success! With my histogram masking algorithm I got bad results with a few images, so I revisited them with the edge detector, with good results, for example:
Edge detection didn't work with every of my test images though. This was mostly due the colour of the labels and the vinyl being very close. Nevertheless it seems like edge detection can be a very effective tool.
Next step: finding the radius and center, which I am going to cover in a future blog post.
Comments
Post a Comment