Image processing glass quality control, my code isn't working good

8 views (last 30 days)
Umut Yildiz
Umut Yildiz on 22 Apr 2022
Commented: Walter Roberson on 23 Apr 2022
Hello everyone,
I am new on MATLAB and Image Processing :)
I am working on the detection of the faulty product by applying image processing techniques on the glass bottles coming from the conveyor belt, I cannot find the error after applying the threshold and im2bw codes to the picture. The bottle appears black, the background is white, as a result I don't get the error, where am I going wrong? Can anyone help?
I've MATLAB R2020a and R2021b
x=imread('bottle.jpg');
threshold_x = graythresh(x);
blackwhite = im2bw(x, threshold_x);
subplot(1,2,1),imshow(x);
subplot(1,2,2),imshow(blackwhite);
y=imread('secondbottle.jpeg');
threshold_y = graythresh(y);
bw_yimage = im2bw(y, threshold_y);
subplot(1,2,1),imshow(y);
subplot(1,2,2),imshow(bw_yimage);
[g,c,d]=size(x);
y=imresize(y,[g,c]);
subplot(1,3,1);
imshow(blackwhite);
title('No Defect');
subplot(1,3,2);
imshow(bw_yimage);
title('Image of PCB which is manufactured');
subplot(1,3,3);
imshow(x-y);
title('Error');
  6 Comments
Walter Roberson
Walter Roberson on 23 Apr 2022
The bottle appears black, the background is white, as a result I don't get the error, where am I going wrong?
Your table is white, so it is the brightest part. The bottle is darker than that, so when you use that method to calculate the threshold, the bottle comes out black.
You need a different method of thresholding. For example, experiment with thresholding by "Green > Red + some constant".

Sign in to comment.

Answers (2)

DGM
DGM on 22 Apr 2022
I'm going to take a shot in the dark and assume that the image difference is such that the result is getting truncated. Normally you'd be looking at the absolute difference, otherwise you only see half the error. In order to do that, you'll have to recast and scale to avoid intermediate truncation.
x = imread('cameraman.tif');
threshold_x = graythresh(x);
blackwhite = im2bw(x, threshold_x);
subplot(1,2,1),imshow(x);
subplot(1,2,2),imshow(blackwhite);
y = fliplr(imread('cameraman.tif'));
threshold_y = graythresh(y);
bw_yimage = im2bw(y, threshold_y);
subplot(1,2,1),imshow(y);
subplot(1,2,2),imshow(bw_yimage);
figure
[g,c,d]=size(x);
y=imresize(y,[g,c]);
subplot(1,3,1);
imshow(blackwhite);
title('No Defect');
subplot(1,3,2);
imshow(bw_yimage);
title('Image of PCB which is manufactured');
subplot(1,3,3);
imshow(abs(im2double(x)-im2double(y)));
title('Error');
  2 Comments
DGM
DGM on 23 Apr 2022
You don't need to flip your process image. I just did that for sake of example -- so that I could re-use the same image and it would be clear how the object content of the two copies was interacting in the difference image.
You still might find that the difference image is dominated by slight inconsistencies in object location in the process image, so I'm not sure how big of a problem registration will be (or how big typical defects are).

Sign in to comment.


Image Analyst
Image Analyst on 23 Apr 2022
Wow, so much wrong with this, where do I start? Well for starters, lower your camera and point it at the bottle so the optic axis is perpendicular to the bottle. Next, know out the specular reflections by using a polarizer in front of the lamp and another, rotatable one in front of the lens. The lighting is not optimal, to say the least. Then, your background is not uniform - half on table and half background/wall. How can that be a good thing? It's not. The background should be black, or at least uniform.
You probably don't know this but I've been looking at glass for the last 25 years - it's one of my most important image processing apps. What we do is to have a point source of light (LED spotlight) aimed at the glass at a 45 degree angle. The glass is in a completely matte light booth painted black. The camera looks at the glass with normal incidence. Any defect in the glass, or residue on the glass, scatters light into the camera and appears bright. The images are remarkably good, so I suggest you use that method. Then I use a DOG (Difference Of Gaussians) filter, that we threshold, to find spots/defects. Everything that is not a spot/defect is background/residue/film.
Do that and then we can have a discussion about how to find your defects. Until then, your images are too horrible to do anything with.
  2 Comments
Umut Yildiz
Umut Yildiz on 23 Apr 2022
Hello Sir
I couldn't manage to do what you said. I'm trying with another bottle but it didn't work. Is there a chance for that you can give me an example? I think i understood you wrong. I've been doing mistakes for all day. I couldn't get any results.
Could you please send me an example bottle pics which are taken with optimal lightining and angle?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!