Hi, I'm developing a very simple plug-in. I'm not very familiar with Java or ImageJ, so there is a problem in my code:
I have writter a median filterting function, however, the type of data cannot be recognized (Img ).
package com.HIT.Weisong;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ops.OpService;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.type.numeric.RealType;
import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This example illustrates how to create an ImageJ {@link Command} plugin.
* <p>
* The code here is a simple Gaussian blur using ImageJ Ops.
* </p>
* <p>
* You should replace the parameter fields with your own inputs and outputs,
* and replace the {@link run} method implementation with your own logic.
* </p>
*/
@Plugin(type = Command.class, menuPath = "Plugins>Gauss Filtering")
public class SparseHessianReconstruction<T extends RealType<T>> implements Command {
//
// Feel free to add more parameters here...
//
@Parameter
private Dataset currentData;
@Parameter
private UIService uiService;
@Parameter
private OpService opService;
@Override
public void run() {
final Img<T> image = (Img<T>)currentData.getImgPlus();
//
// Enter image processing code here ...
// The following is just a Gauss filtering example
//
List<RandomAccessibleInterval<T>> results = new ArrayList<>();
results.add(medianFiltering(image,3,3));
// display result
for (RandomAccessibleInterval<T> elem : results)
{
uiService.show(elem);
}
}
public static int[] medianFiltering(int pix[], int w, int h) {
int newpix[] = new int[w*h];
int[] temp = new int[9];
ColorModel cm = ColorModel.getRGBdefault();
int r=0;
for(int y=0; y<h; y++) {
for(int x=0; x<w; x++) {
if(x!=0 && x!=w-1 && y!=0 && y!=h-1) {
//g = median[(x-1,y-1) + f(x,y-1)+ f(x+1,y-1)
// + f(x-1,y) + f(x,y) + f(x+1,y)
// + f(x-1,y+1) + f(x,y+1) + f(x+1,y+1)]
temp[0] = cm.getRed(pix[x-1+(y-1)*w]);
temp[1] = cm.getRed(pix[x+(y-1)*w]);
temp[2] = cm.getRed(pix[x+1+(y-1)*w]);
temp[3] = cm.getRed(pix[x-1+(y)*w]);
temp[4] = cm.getRed(pix[x+(y)*w]);
temp[5] = cm.getRed(pix[x+1+(y)*w]);
temp[6] = cm.getRed(pix[x-1+(y+1)*w]);
temp[7] = cm.getRed(pix[x+(y+1)*w]);
temp[8] = cm.getRed(pix[x+1+(y+1)*w]);
Arrays.sort(temp);
r = temp[4];
newpix[y*w+x] = 255<<24 | r<<16 | r<<8 |r;
} else {
newpix[y*w+x] = pix[y*w+x];
}
}
}
return newpix;
}
/**
* This main function serves for development purposes.
* It allows you to run the plugin immediately out of
* your integrated development environment (IDE).
*
* @param args whatever, it's ignored
* @throws Exception
*/
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.ui().showUI();
// ask the user for a file to open
final File file = ij.ui().chooseFile(null, "open");
if (file != null) {
// load the dataset
final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());
// show the image
ij.ui().show(dataset);
// invoke the plugin
ij.command().run(SparseHessianReconstruction.class, true);
}
}
}