Posted by
Alex Chekholko on
Aug 16, 2005; 8:34pm
URL: http://imagej.273.s1.nabble.com/how-to-copy-ROI-to-new-image-tp3705016.html
Hello,
I have written a small plug-in to calculate the relative optical density of
selected pixels. However, I am having a problem understanding the API. What
is the easiest way to create a new image from a selection in an existing
image?
I include the code and the easiest way to test it. If you make three new
images: one filled white, one filled dark, and one in between, and then run
the following code, it only works if there is no ROI selected. If I select
an ROI, the resulting new image is blank. What am I doing wrong?
/*
* Optical Density plugin
*
* Copyright (c) 2005 by Alex Chekholko (
[hidden email])
*
* This plugin is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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 plugin; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
import ij.*;
import ij.plugin.PlugIn;
import ij.process.*;
import ij.gui.*;
import ij.measure.*;
import ij.plugin.frame.PlugInFrame;
import java.awt.*;
import java.util.*;
import java.math.*;
/**
* This algorithm is an implementation of the optical density measurement
* from Image Pro v4.
*
* This is version 1.0; 2005-08-16
**/
public class OpticalDensity_ implements PlugIn {
private ImagePlus inputImage;
private ImagePlus whiteImage;
private ImagePlus blackImage;
public void run(String arg) {
if (arg.equals("about"))
showAbout();
if(arg.equals("")) {
if (!showDialog())
return;
}
DoProcessing();
}//run
void showAbout() {
IJ.showMessage("About OpticalDensity_...",
"This plug-in calculates the optical density of a ROI of an 8-bit image.\n"
);
}//showAbout
public boolean showDialog() {
String title = "Optical Density setup";
int[] wList = WindowManager.getIDList();
if (wList==null || wList.length<1) {
IJ.showMessage(title, "There must be at least three windows open.");
return false;
}
Vector pictures = new Vector();
String[] titles = new String[wList.length];
for (int i=0; i<wList.length; i++) {
ImagePlus imp = WindowManager.getImage(wList[i]);
if (imp!=null) {
titles[i] = imp.getTitle();
pictures.add(imp);
}
else {
titles[i] = "";
pictures.add(null);
}
}
GenericDialog gd = new GenericDialog(title);
gd.addChoice("White picture :", titles, titles[0]);
gd.addChoice("Black picture :", titles, titles[0]);
gd.addChoice("Target: ", titles, titles[0]);
gd.showDialog();
if (gd.wasCanceled())
return false;
int whiteIndex = gd.getNextChoiceIndex();
int blackIndex = gd.getNextChoiceIndex();
int targetIndex = gd.getNextChoiceIndex();
inputImage = (ImagePlus)pictures.get(targetIndex);
whiteImage = (ImagePlus)pictures.get(whiteIndex);
blackImage = (ImagePlus)pictures.get(blackIndex);
return true;
}
void DoProcessing() {
int whitelevel = (int)whiteImage.getStatistics().mean;
int blacklevel = (int)blackImage.getStatistics().mean;
ImageProcessor imp = inputImage.getProcessor();
Rectangle r = imp.getRoi();
ImagePlus sel = NewImage.createByteImage("selected region", r.width,
r.height, 1, NewImage.FILL_BLACK);
ImageProcessor imp2 = sel.getProcessor();
for (int i = r.x; i<(r.x+r.width); i++) {
for (int j = r.y; j<(r.y+r.height); j++) {
int pix = imp.getPixel(i,j);
//IJ.showMessage("pixel: "+pix+" x: "+i+" y:"+j);
imp2.putPixel(i,j,pix);
}
}
int targetmean = (int)sel.getStatistics().mean;
double OD = -Math.log
(((double)targetmean-blacklevel)/(whitelevel-blacklevel))/Math.log(10);
sel.show();
IJ.showStatus("tmean: "+targetmean+" white:"+whitelevel+" black:
"+blacklevel+" OD: "+OD);
}
}//class
Thanks, Alex