Login  Register

Re: Quick Help? Pixel Subtraction

Posted by Gabriel Landini on Dec 12, 2005; 10:58pm
URL: http://imagej.273.s1.nabble.com/Quick-Help-Pixel-Subtraction-tp3704239p3704240.html

On Monday 12 December 2005 20:54, Gary Laevsky wrote:
> Our homemade system has some inherent noise in the PMT, to the tune
> of about 1000 pixels out of 30,000 or so.  I'd like to "delete" the
> 255s.  In many cases, the next pixel value is about 150, so I'm also
> having some "stretch" issues.  If I could just delete the 255s....

Not sure if these 3 options would to what you want, but be aware that these
methods "invent" values for the noisy pixels...

A) simplest:
* duplicate original and call it image1
* threshold the image1 at 255,255
* subtract original-image1 (pixels at 255 will now be 0).


B) 8 bit & 24 bit image denoising
////////////////////////////////////////////////////////////////////////////
// SaturatedSinglePixelDenoising.txt
// G. Landini 16/July/2005
// This macro detects saturated *single* pixels (8 connected) in the
// image (grey=255) and replaces them with the pixel value of
// either the mean or the median of the neighbours (choose which
// method one by  un-commenting the lines indicated below.
// Works with 8bit and 24 bit images only.
// Needs the Particles8 plugin available at:
// http://www.dentistry.bham.ac.uk/landinig/software/software.html
run("Duplicate...", "title=Denoised");
setBatchMode(true);
run("Duplicate...", "title=pixels");
if (bitDepth==24) run("8-bit");
setThreshold(255, 255);
run("Threshold", "thresholded remaining");
//---
// if you want to denoise small clusters of pixels, rather than single pixels,
// then comment the following 5 lines, but be aware that the average
// or median will not be correct as other pixels in the cluster may
// contribute to the corrected value:
run("Duplicate...", "title=non-single");
run("Particles8 ", "white show=Particles filter minimum=2 maximum=999999
overwrite");
imageCalculator("Subtract", "pixels","non-single");
selectWindow("non-single");
close();
//---
selectWindow("Denoised");
run("Duplicate...", "title=median");
// use average of the 8 neighbours
run("Convolve...", "text1=[1 1 1 1 0 1 1 1 1] normalize");
// use median of the 3x3 neighbourhood
//run("Median...", "radius=1");
if (bitDepth==24){
  selectWindow("pixels");
  run("RGB Color");
}
imageCalculator("AND create", "pixels","median");
imageCalculator("Subtract", "Denoised","pixels");
imageCalculator("Add", "Denoised","Result of pixels");
selectWindow("median");
close();
selectWindow("Result of pixels");
close();
selectWindow("pixels");
close();
setBatchMode(false);
////////////////////////////////////////////////////////////////////////////


C) 8 bit images only denoising
////////////////////////////////////////////////////////////////////////////
// SaturatedPixelsDenoising.txt
// G. Landini 16/July/2005
// This macro detects saturated  pixels (8 connected) in the
// image (grey=255) and replaces them with the pixel value of
// the mean of the neighbours which are not saturated..
// Works with 8bit images only.
// Needs the BinaryConnectivity plugin available at:
// http://www.dentistry.bham.ac.uk/landinig/software/software.html
t=getTitle();
if (bitDepth()==24){
  run("RGB Stack");
  setSlice(1);
  denoiser();
  selectWindow("Denoised");
  rename("red");
  selectWindow(t);
  setSlice(2);
  denoiser();
  selectWindow("Denoised");
  rename("green");
  selectWindow(t);
  setSlice(3);
  denoiser();
  selectWindow("Denoised");
  rename("blue");
  run("RGB Merge...", "red=red green=green blue=blue");
  rename("Denoised");
  selectWindow(t);
  run("RGB Color");
}
else {
  if (bitDepth()==8) denoiser();
}

function denoiser () {
  run("Duplicate...", "title=Denoised");
  setBatchMode(true);
  xe=getWidth();
  ye=getHeight();
  cycle=true;

  while (cycle){
    selectWindow("Denoised");  
    run("Duplicate...", "title=pixels");
    setThreshold(255, 255);
    run("Threshold", "thresholded remaining");
    selectWindow("Denoised");
    run("Duplicate...", "title=median");
    // use average of the 8 neighbours
    run("Convolve...", "text1=[1 1 1 1 0 1 1 1 1] normalize");
    imageCalculator("AND create", "pixels","median");
    imageCalculator("Subtract", "Denoised","pixels");
    imageCalculator("Add", "Denoised","Result of pixels");
    selectWindow("median");
    close();
    selectWindow("Result of pixels");
    close();
    selectWindow("pixels");
    run("BinaryConnectivity ", "white");
    cycle=false;

    for (x=0;x<xe;x++){
      for (y=0;y<ye;y++){
        n=getPixel(x,y);
        if (n>1){
          if(n<9){
            selectWindow("Denoised");
            putPixel(x,y,(getPixel(x,y)*8 - ((n-1)*255))/(9-n));
            updateDisplay();
            selectWindow("pixels");
          }
          else cycle=true;
        }
      }
    }
    selectWindow("pixels");
    close();
  }
  setBatchMode(false);
  updateDisplay();
}
////////////////////////////////////////////////////////////////////////////

Cheers,

Gabriel