# How to remove the glare and brightness in an image (Image preprocessing)?

I have an image

Edit:

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

Red Channel :

Green channel:

Blue channel:

Edit 2:

Defect detection using Gabor filter

Its Histogram :

How to calculate its appropriate threshold adaptivily.?

-
Is the setup of the lights known ? – nav Jan 24 '12 at 4:44
No actually , is there no way of removing these to spots through filtering etc? – vini Jan 24 '12 at 12:00
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. – nav Jan 27 '12 at 6:14

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]


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]]]


-
is there an inpaint method in matlab? – vini Jan 26 '12 at 15:49
Sorry, I don't know the answer to that. – Matthias Odisio Jan 26 '12 at 15:55
The defect detection is perfect however inpainting has to be implemented as there is no built in function in MATLAB – vini Jan 26 '12 at 16:13
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. – Matthias Odisio Jan 26 '12 at 16:23
Inpainting can be done with what is commonly known as "Poisson image blending" Tutorial here, Matlab code and examples here. – Maurits Jan 26 '12 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;
imagesc(imflat)


results in:

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

hist(double(img(:)),0:255)


and try to select an appropriate threshold based on that.

-
pls check my edit 2 – vini Jan 25 '12 at 17:04
@vini Try look at the cut-off where 90% of the signal is explained – tdc Jan 25 '12 at 17:20
i could just threshold but my ultimate goal is of defect detection.. which doesnt help in any way if i threshold it – vini Jan 26 '12 at 12:38
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. – tdc Jan 26 '12 at 13:53
yes my question is that i now how to detect defects however this glare hinders the result – vini Jan 26 '12 at 14:15

Without 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 : -

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

-
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 – vini Jan 24 '12 at 12:47
did you try just taking individual channels (from RGB or YUV) and just operating on them? – mrkulk Jan 24 '12 at 13:19
The most glared image is the one in the green channel .. How do i operate and correct it? – vini Jan 24 '12 at 13:35
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. – mrkulk Jan 24 '12 at 15:27
how will it help me in producing a glare free image? – vini Jan 24 '12 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

-