Login  Register

Re: Leveling Image

Posted by Michael Schmid on Aug 23, 2011; 1:32pm
URL: http://imagej.273.s1.nabble.com/Leveling-Image-tp3683357p3683368.html

Hi Ahsan, Gabriel,

in principle it is true that one should avoid a posteriori background  
subtraction, but that's not so easy in scanning probe microscopies  
like AFM. It can sometimes happen that the tip changes by interaction  
with the surface, and this gives a step in the image.

By the way, subtracting a plane does not hurt at all as long as the  
slope is only a few degrees or less, which is usually the case. A  
normal AFM tip won't show you steep slopes anyhow, so subtracting a  
plane is equivalent to slighly tilting the sample to make its surface  
parallel with the x, y scan directions.

(To put it into perspective for the optical microscopy people: in AFM  
images one usually has image sizes of several micrometers, but a  
vertical scale of a few dozen nanometers or even less)

I'll paste below a quickly written plugin that subtracts the median  
from each line. Of course, this is not perfect, because you never  
know what the background should be, but better than having steps due  
to tip changes in the image. Line-by-line subtraction should be a  
reasonable approach if the surface is on average flat at length  
scales comparable to the image width.

Michael
________________________________________________________________

import ij.*;
import ij.process.*;
import ij.plugin.filter.*;

/** This plug-in filter subtracts from each line the median of
  *  the pixel values in the line.
  *  Selections (regions of interest) are ignored.
  *
  *  For background subtraction of scanning probe microscopy images
  *  with uneven background due to tip changes.
  *
  *  Copyright: This code is distributed under the terms of the GNU  
General Public License
  *  www.gnu.org/copyleft/gpl.html
  *
  *  Author: Michael Schmid, 23-Aug-2011
  */

public class Subtract_Line_Median implements PlugInFilter {
     private final static int flags = DOES_ALL | CONVERT_TO_FLOAT |  
PARALLELIZE_STACKS;
     private float offset = 128f;

     /** Method to return types supported etc.
      * @param arg unused here
      * @param imp The ImagePlus containing the image or stack
      * @return Code describing supported formats etc.
      */
     public int setup(String arg, ImagePlus imp) {
         if (imp != null) {
             if (imp.getType() == ImagePlus.GRAY16) offset = 32768f;
             else if (imp.getType() == ImagePlus.GRAY32) offset = 0f;
         }
         return flags;
     }

     /** This method is invoked for each slice during execution
      * @param ip The image subject to filtering.
      */
     public void run(ImageProcessor ip) {
         int width = ip.getWidth();
         int height = ip.getHeight();
         float[] pixels = (float[])ip.getPixels();
         int half = width/2;
         float[] aboveBuf = new float[width];
         float[] belowBuf = new float[width];
         float median = offset;  //initial guess
         for (int y = 0; y<width; y++) {
             int nAbove = 0, nBelow = 0;
             for (int p=y*width; p<(y+1)*width; p++) {
                 float v = pixels[p];
                 if (v > median) {
                     aboveBuf[nAbove] = v;
                     nAbove++;
                 }
                 else if (v < median) {
                     belowBuf[nBelow] = v;
                     nBelow++;
                 }
             }
             if (nAbove>half)
                 median = RankFilters.findNthLowestNumber(aboveBuf,  
nAbove, nAbove-half-1);
             else if (nBelow>half)
                 median = RankFilters.findNthLowestNumber(belowBuf,  
nBelow, half);
             for (int p=y*width; p<(y+1)*width; p++)
                 pixels[p] += offset-median;
         } //for y
     }
}

________________________________________________________________

On 23 Aug 2011, at 12:29, Gabriel Landini wrote:

> On Tuesday 23 Aug 2011, Ahsan wrote:
>> I have already removed the non uniform background from the  
>> original image
>> by fitting a plane on it and subtracting from the original image.
>
> Why you to fit a plane and subtract it before correcting the image  
> bands?
> Doesn't that change the topography of the image? and doesn't this  
> also change
> the offset that needs to be corrected so now it is not a constant  
> offset along
> the line anymore?
>
>> subtracting the median from each line individually but i couldn't  
>> find
>> much reading material about it that could explain why will it not  
>> remove
>> the actual data and only the artifacts.
>
> If you offset according to the median of  all the data in the line  
> (as opposed
> to a known reference that should be originally constant across  
> lines, such as
> a portion of background) this offset is not guaranteed to be the  
> right one
> unless the image is quite homogeneous.
>
> If the data indeed goes up, the median (and hence the offset) will  
> probably
> go up too, but the line perhaps does not need to be made darker  
> than it is
> just because the median is large.
>
> Sorry I do not have any further suggestions other than improving  
> the scanning
> quality. Maybe there is a standard method used in AFM, but I am not  
> familiar
> with this.
>
> Cheers
>
> Gabriel