I need to find the black spot present in the component image and produce output based on the presence of black spot in the component .. please help me with what methods should i use and code to do it

 Accepted Answer

Just threshold the red channel and call bwlabel(). See attached code.

4 Comments

Sir, i thank you for your code . Even with this code i am encountering certain problems as in some images i find edges are too somewhat dark as in the image i am attaching this time. while working with the code i am encountering wrong number of black spots present when i am trying to reduce or increase the intensity of image for comparison in this line of code comparisonbinaryImage = redChannel < 172 ; i am getting wrong number of spots present. How can i overcome it . can you help me with some tips and modifications in code required to get correct output
Yes, of course. The algorithm I gave worked for that image because that's the only image I had. If it doesn't work with other images, you'll have to make the algorithm more robust. If I make it work for these two images, then you'll come along with a third image it doesn't work for. You'll need to keep tweaking the algorithm until it can handle all possible cases that you expect to encounter.
One problem I see immediately is bad image capture parameters. For example you have a lot of specular reflection bouncing off your background. You should use broad source illumination, like you'd get from a properly designed light booth with a uniform paint job. Like an integrating sphere or as close as you can get to that. Then light would come from all angles equally. If you can't use a light booth then at least you should use big lights (like long fluorescent or LED tubes/bars, not compact light bulbs like incandescent or CFLs). Then you should use the technique of crossed polarizers. Put a polarizer in front of your lights, and a rotatable one in front of your camera lens. Rotate it until the specular hit spots go away.
Next you may want to have a jig where you can mount the parts in a known orientation perpendicular to the camera. Might as well have the jig hold around 6 parts so you can analyze more than one part per image.
Next, make sure your illumination is the same for every image. You can put a light meter in there and make sure you have around 1000 to 5000 Lux illuminance on your scene. Also put a standard in there like the X-rite ColorChecker Passport so that you can correct for illumination differences in case there still are any.
Next, take a picture of just the background alone. Find out the percentage light at every point so that you can divide that out. For example, if the light were only 90% as bright in the corners as the middle, you'd want to divide your images by 0.9 there, wouldn't you? Of course you would. I'm attaching a demo for background correction.
Then it should be easier for you to use a fixed threshold. You would not need an auto thresholding algorithm which may find a wrong threshold. You'll have control over the light and exposure and shading/vignetting and color correction, so a fixed threshold is entirely appropriate, and it's the easiest. It's always better to start with a good image rather than use image processing an an attempt to repair a bad image.
What if we want to do the same thing (black background) but have a pseudo color front image?
That would be an indexed image, if it's pseudocolored. But basically it's the same concept. Flat field correct the image to compensate for lens shading, then identify the background (hopefully it's smooth), perhaps by thresholding, or thresholding after some processing, like running it through stdfilt() to identify smooth and not-smooth regions.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!