how to copy ROI to new image

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

how to copy ROI to new image

Alex Chekholko
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
Reply | Threaded
Open this post in threaded view
|

Re: how to copy ROI to new image

Wayne Rasband
> 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?

The easiest way to do this is to run the Image>Duplicate command:

     IJ.run("Duplicate...", "title=[selected region]");

If the existing image is not the current image you would need to make
it the current image:

     WindowManager.setTempCurrentImage(inputImage);
     IJ.run("Duplicate...", "title=[selected region]");

You can also get the image's ImageProcessor, crop it, then open it as a
new image:

     new ImagePlus("selected region",
inputImage.getProcessor().crop()).show();

-wayne
Reply | Threaded
Open this post in threaded view
|

how to write inside an image

kiran kumar-2-3
Hello Group,
 
    I am writing a plugin in which I created an FFT of an image and drawn multiple circular ROI's and now i require to write the radius of circles in the FFT image window. I couldnt find out any command which does such kind of writing. Can anyone let me know how to proceed.
 
Thanks,
 
Regards,
 
Kiran Kumar

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

Re: how to write inside an image

Zummy
Can you use Graphics or Graphics2D?  I am a newbie to ImageJ.

John

At 02:10 PM 8/17/2005, you wrote:

>Hello Group,
>
>     I am writing a plugin in which I created an FFT of an image and drawn
> multiple circular ROI's and now i require to write the radius of circles
> in the FFT image window. I couldnt find out any command which does such
> kind of writing. Can anyone let me know how to proceed.
>
>Thanks,
>
>Regards,
>
>Kiran Kumar
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: how to write inside an image

Wayne Rasband
In reply to this post by kiran kumar-2-3
>      I am writing a plugin in which I created an FFT of an image and
> drawn multiple circular ROI's and now i require to write the radius of
> circles in the FFT image window. I couldnt find out any command which
> does such kind of writing. Can anyone let me know how to proceed.

Something like this should work:

     ImageProcessor ip = imp.getProcessor();
     ip.drawString("Some text", 20, 20);
     imp.updateAndDraw();

where imp is the ImagePlus object corresponding to the FFT image. You
will probably also need to use other ImageProcessor methods such as
setColor() and setFont().
Reply | Threaded
Open this post in threaded view
|

Re: how to write inside an image

kiran kumar-2-3
Thanks for ur reply but how do i write a variable as i need to write radius of the circles which are generated randomly......
 
Regards,
 
Kiran Kumar

Wayne Rasband <[hidden email]> wrote:
>     I am writing a plugin in which I created an FFT of an image and
> drawn multiple circular ROI's and now i require to write the radius of
> circles in the FFT image window. I couldnt find out any command which
> does such kind of writing. Can anyone let me know how to proceed.

Something like this should work:

ImageProcessor ip = imp.getProcessor();
ip.drawString("Some text", 20, 20);
imp.updateAndDraw();

where imp is the ImagePlus object corresponding to the FFT image. You
will probably also need to use other ImageProcessor methods such as
setColor() and setFont().

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

Re: how to write inside an image

Antoinette
Crop image with following method prorgammed within visual basic.net project. I am a vb.net developer, if you program using c sharp, you can simply convert this api using a c sharp code conversion tool.

Public Shared Function ApplyCrop(img As REImage, x As Integer, y As Integer, width As Integer, height As Integer) As Integer
Reply | Threaded
Open this post in threaded view
|

Re: how to copy ROI to new image

sankar.lp.gym
In reply to this post by Alex Chekholko
In C# also you have the method like VB.NET


Java Training in Chennai