All Downloads are FREE. Search and download functionalities are using the official Maven repository.

imageJ.Background_Correction Maven / Gradle / Ivy

Go to download

Orbit, a versatile image analysis software for biological image-based quantification

There is a newer version: 3.15
Show newest version
/*
 *     Orbit, a versatile image analysis software for biological image-based quantification.
 *     Copyright (C) 2009 - 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91, CH-4123 Allschwil, Switzerland.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see .
 *
 */

package imageJ;

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

/**
  *     Background Correction
  *     @version 1.0  July, 2001
  *
  *     This plugin does:
  *       1.  generates a background image estimated through iterations
  *       of the minimum ranking and the number of iterations is defined
  *       by the user;
  *       2.  substracts the background image from the orginal image
  *       and generates a result image.
  *       3.  auto-contrast the result image.
  *
  *     This filter works quite well for images collected under
  *     uneven illumination.  It only works for 8 bit grayscale images.
  *
  *     Requires ImageJ version 1.23 or above.
  *
  *     For the sample image, the uneven background can be corrected
  *     using 3 iterations of Minimum ranking with Radius=4 (default
  *     setting).
  *
  *     @author	  Terry Wu, Ph.D.
  *     @author   University of Minnesota
  *     @author	  [email protected]
  *
  *     June, 2008:
  *        jerome mutterer replaced rank type2 by ranktype RankFilters.MIN (2 is now MAX)
  *
*/

public class Background_Correction {

//  int numberIteration=3;
//  int radius=4;
//  boolean autoContrast = true;


  public ImagePlus run (ImageProcessor ip, int numberIteration, int radius, boolean autoContrast){
    ImagePlus copy1, copy2;
    ImageProcessor imageProcessor1, imageProcessor2;

    // Make 2 copies of the original image
    copy1=duplicateImage(ip);
    imageProcessor1=copy1.getProcessor();
    copy2=duplicateImage(ip);
    imageProcessor2=copy2.getProcessor();

    // Get the background image
    estimateBackground(imageProcessor2, numberIteration, radius);

    // Substract background from the original image
    imageProcessor1.copyBits(imageProcessor2,0,0,Blitter.SUBTRACT);

    // Auto-contrast result image
    if(autoContrast){
      ImagePlus result=WindowManager.getCurrentImage();
      this.autoAdjust(copy1, imageProcessor1);
    }

    // Display the result image
    copy2=null;
    return copy1;
  }

  void estimateBackground(ImageProcessor ip, int iteration, int radius){
    RankFilters rankFilter=new RankFilters();
    for(int i=1; i<=iteration; i++){
      IJ.showStatus("Please wait ...  Iteration: " + i);
      rankFilter.rank(ip, radius, RankFilters.MIN);
    }
  }

  ImagePlus duplicateImage(ImageProcessor iProcessor){
    int w=iProcessor.getWidth();
    int h=iProcessor.getHeight();
    ImagePlus iPlus=NewImage.createByteImage("Image", w, h, 1, NewImage.FILL_BLACK);
    ImageProcessor imageProcessor=iPlus.getProcessor();
    imageProcessor.copyBits(iProcessor, 0,0, Blitter.COPY);
    return iPlus;
  }


//    The following autoAjust() method is a slightly modified version of the autoAjust()
//    from ij.plugin.frame.ContrastAdjuster by Wayne Rasband 
  void autoAdjust(ImagePlus imp, ImageProcessor ip){
    double min, max;
    Calibration cal = imp.getCalibration();
    imp.setCalibration(null);
    ImageStatistics stats = imp.getStatistics();
    imp.setCalibration(cal);
    int[] histogram = stats.histogram;
    int threshold = stats.pixelCount/5000;
    int i = -1;
    boolean found = false;
    do {
          i++;
          found = histogram[i] > threshold;
        } while (!found && i<255);
      int hmin = i;
      i = 256;
    do {
          i--;
          found = histogram[i] > threshold;
        } while (!found && i>0);
      int hmax = i;
    if (hmax>hmin){
      imp.killRoi();
      min = stats.histMin+hmin*stats.binSize;
      max = stats.histMin+hmax*stats.binSize;
      ip.setMinAndMax(min, max);
    }
    Roi roi = imp.getRoi();
    if (roi!=null){
     ImageProcessor mask = roi.getMask();
      if (mask!=null)
      ip.reset(mask);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy