Skip to content

Commit

Permalink
Add missing plugin dependencies
Browse files Browse the repository at this point in the history
See the following forum thread for discussion:
    http://forum.imagej.net/t/nucleus-counter-in-cookbook/2802

The Cookbook plugins here are an adapted subset of the ones from
the old MBF ImageJ collection (http://imagej.net/MBF). It turns out
that some plugins necessary for certain functionality were not
migrated over to the Cookbook repository, because they were not
explicit compile-time references of the calling plugins. This is
because IJ.run invokes plugins by name, not class, so there is no
type safety: if the plugin is missing, we don't know until runtime.

To prevent future problems, I did a thorough check of all places
where IJ.run was used to call a plugin. Here is the complete list:

    $ git grep -h 'IJ.run("' | sed 's/.*IJ.run(\("[^",]*"\).*/\1/' | sort -u
    "8-bit"
    "Adapative3DThreshold "
    "Add..."
    "Analyze Particles..."
    "Convert to Mask"
    "Duplicate..."
    "Entropy Threshold"
    "Fill"
    "Grays"
    "Image Calculator..."
    "Invert"
    "Mean..."
    "Median..."
    "Mixture Modeling threshold"
    "Multi Measure"
    "Multiply..."
    "OtsuThresholding 16Bit"
    "OtsuThresholding 8Bit"
    "RGB Color"
    "Red"
    "Rename..."
    "Scale Bar..."
    "Spectrum"
    "Subtract Background..."
    "Watershed"
    "k-means Clustering"

Of the above commands, the following are not part of IJ1 core:

    "Adapative3DThreshold "
    "Entropy Threshold"
    "Mixture Modeling threshold"
    "Multi Measure"
    "OtsuThresholding 16Bit"
    "OtsuThresholding 8Bit"
    "k-means Clustering"

Of those, the following were easily extractable from the
old MBF ImageJ plugins archive:

    "Adapative3DThreshold "
    "Entropy Threshold"
    "OtsuThresholding 16Bit"
    "OtsuThresholding 8Bit"
  • Loading branch information
ctrueden committed Sep 19, 2016
1 parent 5f1774a commit bb02b66
Show file tree
Hide file tree
Showing 6 changed files with 831 additions and 0 deletions.
185 changes: 185 additions & 0 deletions src/main/java/sc/fiji/cookbook/Adapative3DThreshold_.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* $Id: Adapative3DThreshold_.java,v 1.19 2005/06/15 09:27:36 perchrh Exp $
*
* Copyright (C) 2005 Per Christian Henden
* Copyright (C) 2005 Jens Bache-Wiig
*
* 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 2 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, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* The authors can be contacted by email to
* perchrh [at] pvv.org (Per Christian Henden) or
* bachewii [at] stud.ntnu.no (Jens Bache-Wiig) or
* by mail to
* Per Christian Henden, Rogalandsgata 167, 5522 Haugesund, Norway.
*/

package sc.fiji.cookbook;

import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.gui.GenericDialog;
import ij.gui.StackWindow;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;


public class Adapative3DThreshold_ implements PlugInFilter {

ImagePlus imRef;
private boolean noGo;
private int baseThreshold;
private int radius;
private double localWeight;

private int width;
private int height;
private int depth;

public int setup(String arg, ImagePlus imp) {
imRef = imp;

if (arg.equals("about")) {
showAbout();
return DONE;
}

getParams();

return DOES_8G;
}

private void getParams() {

//set defaults
baseThreshold = 127;
localWeight = 5;
int diameter = 3;

GenericDialog gd = new GenericDialog("3D adaptive threshold configuration");

gd.addNumericField("Base threshold", baseThreshold, 0);
gd.addNumericField("Mask diameter (pixels)", diameter, 0);
gd.addNumericField("Local weight (percent)", localWeight, 1);

gd.showDialog();

if (gd.wasCanceled()) {
if (imRef != null) imRef.unlock();
noGo = true;
}

baseThreshold = (int)gd.getNextNumber();
radius = ((int)gd.getNextNumber() )/2;
localWeight = gd.getNextNumber()/100;
}


public void run(ImageProcessor ip) {

if(noGo) return;

width = ip.getWidth();
height = ip.getHeight();
depth = imRef.getStackSize();

//create variable to store modificated image
byte[][] imageCopy = new byte[depth][width*height];

//do the thresholding
int value;
int localValue;
long sum;
long count;
int localThreshold;
double localAverage;
for (int z = 0; z < depth; z++){
IJ.showProgress(z+1,depth-1);
for (int y = 0; y < height; y++){
for(int x = 0; x < width; x++){

value = 0xff & ((byte[]) imRef.getStack().getPixels(z +1))[x + y * width];

sum = 0;
count = 0;
localAverage = 0;
for(int i=-radius; i<radius; i++){
for(int j=-radius; j<radius; j++){
for(int k=-radius; k<radius; k++){
localValue = safeGet(z+k+1,x+i,y+j);

if(localValue >= 0 ){ //if not outside mask
sum+=(localValue-baseThreshold);
count++;
}
}
}
}

if(count > 0) localAverage = sum/count;

localThreshold = (int) ( (1-localWeight)*baseThreshold - localWeight*localAverage +0.5 );

if (value >= localThreshold){
imageCopy[z][x+y*width] = (byte)255;
}else{
imageCopy[z][x+y*width] = (byte)0;
}
}
}

}

ImageStack newStack = new ImageStack(width, height);

for (int i = 0; i < depth; i++) {
byte[] newPixels = imageCopy[i];
newStack.addSlice("Slice " + i, newPixels);
}

IJ.showProgress(1,1); //set to finished

ImagePlus newImage = new ImagePlus("Adaptive3DThreshold - ", newStack);
new StackWindow(newImage);
//IJ.run("Invert");
IJ.setThreshold(1,255);
}


private int safeGet(int z, int x, int y){

//Gets the value from the image, or if outside image return -1

int retval;

try{
retval = 0xff & ((byte[]) imRef.getStack().getPixels(z +1))[x + y * width];
}
catch (Exception e){
retval = -1;

}

return retval;

}

void showAbout() {
IJ.showMessage( "About Adaptive 3D Threshold..",
"This plugin thresholds a stack according to the threshold "
+ "T = (1-w)*base - w*avg(radius^3 neighbour-base)");
}

}
111 changes: 111 additions & 0 deletions src/main/java/sc/fiji/cookbook/Entropy_Threshold.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package sc.fiji.cookbook;

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import ij.*;
/**
* Automatic thresholding technique based on the entopy of the histogram.
* See: P.K. Sahoo, S. Soltani, K.C. Wong and, Y.C. Chen "A Survey of
* Thresholding Techniques", Computer Vision, Graphics, and Image
* Processing, Vol. 41, pp.233-260, 1988.
*
* @author Jarek Sacha
*/
public class Entropy_Threshold implements PlugInFilter {
/*
*
*/
public int setup(String s, ImagePlus imagePlus) {
return PlugInFilter.DOES_8G | PlugInFilter.DOES_STACKS;
}

/*
*
*/
public void run(ImageProcessor imageProcessor) {
int[] hist = imageProcessor.getHistogram();
int threshold = entropySplit(hist);
int intMax = (int)imageProcessor.getMax();
// imageProcessor.threshold(threshold);
IJ.setThreshold(threshold,intMax);
}

/**
* Calculate maximum entropy split of a histogram.
*
* @param hist histogram to be thresholded.
*
* @return index of the maximum entropy split.`
*/
private int entropySplit(int[] hist) {

// Normalize histogram, that is makes the sum of all bins equal to 1.
double sum = 0;
for (int i = 0; i < hist.length; ++i) {
sum += hist[i];
}
if (sum == 0) {
// This should not normally happen, but...
throw new IllegalArgumentException("Empty histogram: sum of all bins is zero.");
}

double[] normalizedHist = new double[hist.length];
for (int i = 0; i < hist.length; i++) {
normalizedHist[i] = hist[i] / sum;
}

//
double[] pT = new double[hist.length];
pT[0] = normalizedHist[0];
for (int i = 1; i < hist.length; i++) {
pT[i] = pT[i - 1] + normalizedHist[i];
}

// Entropy for black and white parts of the histogram
final double epsilon = Double.MIN_VALUE;
double[] hB = new double[hist.length];
double[] hW = new double[hist.length];
for (int t = 0; t < hist.length; t++) {
// Black entropy
if (pT[t] > epsilon) {
double hhB = 0;
for (int i = 0; i <= t; i++) {
if (normalizedHist[i] > epsilon) {
hhB -= normalizedHist[i] / pT[t] * Math.log(normalizedHist[i] / pT[t]);
}
}
hB[t] = hhB;
} else {
hB[t] = 0;
}

// White entropy
double pTW = 1 - pT[t];
if (pTW > epsilon) {
double hhW = 0;
for (int i = t + 1; i < hist.length; ++i) {
if (normalizedHist[i] > epsilon) {
hhW -= normalizedHist[i] / pTW * Math.log(normalizedHist[i] / pTW);
}
}
hW[t] = hhW;
} else {
hW[t] = 0;
}
}

// Find histogram index with maximum entropy
double jMax = hB[0] + hW[0];
int tMax = 0;
for (int t = 1; t < hist.length; ++t) {
double j = hB[t] + hW[t];
if (j > jMax) {
jMax = j;
tMax = t;
}
}

return tMax;
}
}
Loading

0 comments on commit bb02b66

Please sign in to comment.