Hi all,
I am new to Matlab. I need to know how to use a filter to eliminate non-uniformity in an image.

11 Comments

I can help you get started.
close all;
I = imread('11.png');
I = rgb2gray(I);
H = fspecial('disk',8);
background = imopen(I,strel('disk',40) );
background = imfilter(background,H,'replicate');
I2 = I - background;
background = imopen(I2,strel('disk',50) );
background = imfilter(background,H,'replicate');
I3 = I2 - background;
I4 = imadjust(I3);
figure;imshow(I4);
More hint
close all;
I = imread('11.png');
I = rgb2gray(I);
H = fspecial('disk',10);
background = imopen(I,strel('disk',77) );
background = imfilter(background,H,'replicate');
I2 = I - background;
background = imopen(I2,strel('disk',77) );
background = imfilter(background,H,'replicate');
I3 = I2 - background;
I4 = imadjust(I3);
I5 = medfilt2(I4,[30 30]);
I5 = I5-I4;
I5 = imcomplement(imadjust(I5));
figure;imshow(I5,[]);
Ok i am virtually telling u the answer replace the med filter with some low pass.
close all;
I = imread('11.png');
I = rgb2gray(I);
I2 = medfilt2(I,[20 20]);
I3 = I2-I;
I3 = imcomplement(imadjust(I3));
figure;imshow(I3,[]);
Siam
Siam on 28 Aug 2014
Edited: Siam on 2 Sep 2014
I am really impressed for your great suggestion. However; the resultant image of your code does not appear similar to the actual resultant image.
Ank
Ank on 28 Aug 2014
Yes. I has posted the first 2 just as some hints. The 3rd one is the closes to the answer. Your approach is correct but u can see from the result that median filter works better. Median filter is also a kind of low pass filter it is not linear that all. I guess after the results of the 3rd answer you need to process your image further like using local thresholding etc depends on what you want to do with it.
close all;
I = imread('11.png');
I = rgb2gray(I);
h = fspecial('gaussian',20,3);
I2 = imfilter(I,h);
%I2 = medfilt2(I,[20 20]);
I3 = I2-I;
I3 = imcomplement(imadjust(I3));
figure;imshow(I3,[]);
I = imread('11.png');
I = rgb2gray(I);
I2 = medfilt2(I,[20 20]);
I3 = I2-I;
I3 = imcomplement(imadjust(I3));
I4 = bradley(I3);
I3 = bradley(I);
I3 = I3+I4;
figure;imshow(I3,[]);
so thing something in similar lines
Ank
Ank on 28 Aug 2014
Edited: Ank on 28 Aug 2014
imcomplement is is used as i am subtracting. check the alternative i posted for color. should be the same for color if you process it on V. I guess u cannot threshold on the V there.
close all;
OI = imread('1.JPG');
OI = rgb2hsv(OI);
I = OI(:,:,3);
h = fspecial('gaussian',20,30);
I2 = imfilter(I,h);
I3 = -I2+I;
I3 = I3-min(min(I3));
OII= OI;
OII(:,:,3) = I3;
figure;imshow(hsv2rgb(OII));
I = OI(:,:,3);
I2 = medfilt2(I,[20 20]);
I3 = -I2+I;
I3 = I3-min(min(I3));
II = I3;
I4 = bradley(I3);
I3 = bradley(I);
I3 = I3+I4;
OI(:,:,3) = II;
figure;imshow(hsv2rgb(OI));
Siam
Siam on 4 Sep 2014
I have fixed the issue I was having.
Siam
Siam on 4 Sep 2014
Thank you very much.
Siam
Siam on 7 Sep 2014
How to measure window size?
Use trial and error until you get some output that you're happy with.

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 28 Aug 2014

1 vote

Siam, you really need to understand what process gave rise to the non-uniform background. This will help you decide what algorithm to use. But there are rules of thumb. For most situations finding the background and then dividing the image by the background is the proper way to go. For certain other instances (radiology, fluorescence microscopy, etc.) background subtraction is the best way. The best way to get a background is to snap a "blank shot" of just the background with no sample(s) in there are all. If you have samples in there, you can try to get rid of them by morphology (opening or closing) or just a global fitting. Morphology can follow local variations better than regression but can introduce undesirable artifacts. What I do is to fit the image of a uniform background to a 2D polynomial and then divide - that's what works best in my situation. You can do a local regression with a Savitzky-Golay filter but I find that in most cases where you're illuminating something with a lamp, the uneven light pattern is very broad and smooth and does not vary on a rapid basis, so a process that scans the image with a small local window just ends up giving you noise. A global fit will totally and effectively remove all video noise. See my attached demo where I use John D'Errico's polyfitn (which you need to get from the File Exchange here - be sure to check out John's other useful utilities while you're there.
For color images, usually you want to convert to HSV and correct only the V channel. This will avoid color artifacts that you might get if you correct the red, green, and blue channels independently.
I also attach a Savitky-Golay filter but it just does it in each direction and is not what you'd get if you did a true 2D fit at every window location, which you can again use John's polyfitn() for if you want/need to follow background non-uniformities more closely.

8 Comments

Ank
Ank on 28 Aug 2014
For your case the light variation can somewhat be approximated well by a polynomial thus using Image Analyst's method would the best (mine is more general case but would have artifacts that might require more morphological techniques).
Regarding the astronomical image, just call rgb2hsv, then v=hsv(:,:,v), then use the polyfitn code and see how it goes. But I did not see your microscope images attached. By the way, there is a whole body of work on haze removal - you can find papers at Vision Bib http://www.visionbib.com/bibliography/contents.html and those methods might be better for the astronomy images than background removal methods.
Siam
Siam on 28 Aug 2014
Edited: Siam on 29 Aug 2014
Here is an image. Please have a look and suggest me with a code if possible. Thank you.
You might convert to hsv then run adapthisteq() on the v channel.
Siam
Siam on 28 Aug 2014
Thank you for the suggestion however; adapthisteq will fix the contrast issues not the illumination. Please do correct me if I am wrong because equalization does enhances contrast right? Thank you.
It will also "flatten" the background. Not sure what you consider to be the background here so that's why I suggested that. Not even sure how to get an estimate of the background because there is so much clutter in the image. If it's a transmitted microscope image, why can't you just snap a blank shot to get the background?
Remove your slide with your sample on it (the cells or whatever). Just put in a totally clean slide and take a picture of the light coming through.
Why do you think you need to correct for background anyway? What do you actually want to measure?

Sign in to comment.

More Answers (1)

Spandan Tiwari
Spandan Tiwari on 28 Aug 2014

0 votes

The classical homomorphic filtering might be able to help here. See the following blog post on the blog Steve on Image Processing for details.

3 Comments

Siam
Siam on 29 Aug 2014
Thank you Spandan. I have seen that and if I am not wrong that is based on high-pass filter.
Yes, Basically it assumes that the really, really blurred version of the image is the background or illumination pattern.
Siam
Siam on 29 Aug 2014
Thank you for your suggestion.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!