Login  Register

Re: Quick Help? Pixel Subtraction

Posted by Gabriel Landini on Dec 13, 2005; 8:33pm
URL: http://imagej.273.s1.nabble.com/Quick-Help-Pixel-Subtraction-tp3704239p3704244.html

On Tuesday 13 December 2005 18:13, Henry Barwood wrote:

> I tried to distinguish "hot spots" from "hot pixels", apparently
> unsuccessfully. My samples are petrographic slides that are contaminated
> with particles of alumina polish. The particles luminesce a brilliant,
> bright green, but are of irregular size and dimension. I only want to
> modify the "hot Spots" not the remaining image, hence, I'm reluctant to use
> any other noise reduction or background subtraction filters.
>
> When I remove the hot spots using the threshold technique it leaves a
> completely black spot in place of the bright green spot. I would like to
> replace the removed area with a less conspicuous area that more accurately
> matches the surrounding image. Don't know if I explained that properly, but
> I'm sort of struggling for terminology here!

I wonder if you are using a black foreground on a white background...
Try these versions instead. (They set IJ colours as I have them here).
First macro deletes only single white pixels, the other does the same with
white areas.

Cheers,
Gabriel

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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("Colors...", "foreground=white background=black selection=yellow");
run("Options...", "iterations=1 black count=1");
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);


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

run("Colors...", "foreground=white background=black selection=yellow");
run("Options...", "iterations=1 black count=1");
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();
}
//////////////////////////////////////////////////////////////////