I have an image

enter image description here

Is there any way of removing the bright white spots ? Please help thanks


After operating with gaussian and then displaying using imagesc get the following output which clearly shows the bright red spots How do i get rid of them

enter image description here

Red Channel :

enter image description here

Green channel:

enter image description here

Blue channel:

enter image description here

Edit 2:

Defect detection using Gabor filter

enter image description here

Its Histogram :

enter image description here

How to calculate its appropriate threshold adaptivily.?

  • $\begingroup$ Is the setup of the lights known ? $\endgroup$
    – nav
    Jan 24, 2012 at 4:44
  • $\begingroup$ No actually , is there no way of removing these to spots through filtering etc? $\endgroup$
    – vini
    Jan 24, 2012 at 12:00
  • 4
    $\begingroup$ In general, this is an ill-posed problem. You have too many unknowns and will have to resort to a heuristic-based solution. Good luck picking an answer. $\endgroup$
    – nav
    Jan 27, 2012 at 6:14

5 Answers 5


Let's assume glare portions are the only saturated areas in the image. Detection can be performed by thresholding the intensity (code in Mathematica):

saturated = Binarize[ColorConvert[img, "Grayscale"], .9]

enter image description here

Then we need only to replace the portions of the image around the saturation mask (enlarging the mask is done by the morphological function Dilation). Inpainting using texture synthesis (using the function Inpaint) seems to work well in this example, though I can not test it as input to your defect-detection algorithm:

Inpaint[img, Dilation[saturated, DiskMatrix[20]]]

enter image description here

  • $\begingroup$ is there an inpaint method in matlab? $\endgroup$
    – vini
    Jan 26, 2012 at 15:49
  • $\begingroup$ Sorry, I don't know the answer to that. $\endgroup$ Jan 26, 2012 at 15:55
  • $\begingroup$ The defect detection is perfect however inpainting has to be implemented as there is no built in function in MATLAB $\endgroup$
    – vini
    Jan 26, 2012 at 16:13
  • 1
    $\begingroup$ I am glad that the answer resolved the problem. As for inpainting in matlab, that's probably well suited for an independent question on SO. See stackoverflow.com/search?q=matlab+inpainting as a start. $\endgroup$ Jan 26, 2012 at 16:23
  • 2
    $\begingroup$ Inpainting can be done with what is commonly known as "Poisson image blending" Tutorial here, Matlab code and examples here. $\endgroup$
    – Maurits
    Jan 26, 2012 at 18:44

This may be a bit of a simplistic answer, but could you just threshold? e.g.:

img = imread('daRNS.png');
imflat = img; 
imflat(img>150) = 150; 

results in:

flattened image

It would obviously be better to select the threshold adaptively. For example you could look at the image histogram:



and try to select an appropriate threshold based on that.

  • $\begingroup$ pls check my edit 2 $\endgroup$
    – vini
    Jan 25, 2012 at 17:04
  • $\begingroup$ @vini Try look at the cut-off where 90% of the signal is explained $\endgroup$
    – tdc
    Jan 25, 2012 at 17:20
  • $\begingroup$ i could just threshold but my ultimate goal is of defect detection.. which doesnt help in any way if i threshold it $\endgroup$
    – vini
    Jan 26, 2012 at 12:38
  • 3
    $\begingroup$ But your question is "How to remove the glare and brightness in an image (Image preprocessing)?" not how to detect defects, which is another (more difficult) question. @mrkulk below appears to have provided a near complete answer to that question as well below. $\endgroup$
    – tdc
    Jan 26, 2012 at 13:53
  • $\begingroup$ yes my question is that i now how to detect defects however this glare hinders the result $\endgroup$
    – vini
    Jan 26, 2012 at 14:15

enter image description hereWithout lighting information, it is difficult. However, if the shape of the object in the image is known, you could setup a shape template of the white glare (gaussian) and do a sliding window to find possible detection of glare (followed by color blending from adjacent area). Perceptually, we infer 3D shape from images using shading. If shape from shading is able to give the surface gradient, we could do a sliding window and check our glare template at each location.

After canny edge detection : -

enter image description here

Basically, the overlap ( max overlapping area ) between image #1 and #2 will be the defect.

  • $\begingroup$ Describing what am trying to do - I am applying a gabor filter which is mostly used for texture segmentation to find defects in fruits however the glare poses a problem as the filter displays the two white spots also as a potential defect which i do not ideally want $\endgroup$
    – vini
    Jan 24, 2012 at 12:47
  • $\begingroup$ did you try just taking individual channels (from RGB or YUV) and just operating on them? $\endgroup$
    – mrkulk
    Jan 24, 2012 at 13:19
  • $\begingroup$ The most glared image is the one in the green channel .. How do i operate and correct it? $\endgroup$
    – vini
    Jan 24, 2012 at 13:35
  • $\begingroup$ I tried taking the edge image (canny) with a threshold of 0.5. As expected, I did not see the glare in the image. This should give you a strong prior for regions which do NOT contain glare. $\endgroup$
    – mrkulk
    Jan 24, 2012 at 15:27
  • $\begingroup$ how will it help me in producing a glare free image? $\endgroup$
    – vini
    Jan 24, 2012 at 17:51

My opinion is that this is a machine vision problem in which you should be controlling the lighting and have a good idea of the maximum brightness of a non-glare pixel brightness in the image. Defect detection is generally a machine vision problem rather than a computer vision problem.

What we see as a result of lighting is an addition of specular and diffuse reflections of light (plus some emittance but its negligible here).

The specular component is the glare, on shiny surface like this apple, it is much more than the diffuse reflection (>10x)

This means that if you setup your lighting, gain and exposure prior to this, on a diffuse surface, you can be sure that nothing will be even close to saturated. So using a fixed threshold is actually the preferred solution here, as long as you've proven with enough data that "no pixels not containing glare" would be above the threshold. In essence you are setting up the lighting conditions, and camera parameters such that classification of a pixel becomes trivial, in this case performed by a simple threshold, rather than a more complex machine learned function of pixels around it.

I like "vini"'s approach, no real need to show the RGB planes. Just a simple grayscale threshold would actually work here.

1- you design the lighting conditions, not ambient

2- make the classification job extremely trivial (thresholding)

3- measure the feature

4- compare to tolerance


Convert to lab colour space first, mapminmax, then use the first luminosity channel. That reduces the colour issues. Then use some thresholding on the top 80% brightest pixel. Check and test for a dip in the histogram, the best threshold is near the bottom of the dip. If there is no local min in this region your image probably has minimal reflections...... Martin


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.