Login  Register

Re: User defined ROI

Posted by George Patterson on Oct 25, 2013; 1:53pm
URL: http://imagej.273.s1.nabble.com/User-defined-ROI-tp5005318p5005323.html

Hi Lee,
The link listed by Vytas will help you a bunch.
Below is a macro we use to measure and fit plot profiles.  It might help to
see another example.  You could probably even edit the appropriate parts
you need for your ROI measurements and calculations.
Alternatively, could you have your users add ROIs to the ROI manager and
edit your macro to select these in sequence?
Best,
George




/*Macro to plot profile a line and fit with a Gaussian function

For this macro, open the image you want to analyze and run the macro.
You will be prompted to pick a directory into which to save the results.
You'll be prompted to choose a new ROI or move an existing ROI (a line,
this macro is assuming).
Once you choose, click the OK button.  You'll see a plot and a fit of the
plot.
The prompt is asking if the fit is OK (OK for your purposes).
If it's not, uncheck the box and move the line to another.  The data from
this fit will not be included in the final text file(see below).
If you feel it necessary, we can make it so you may change the initial
guesses for each fit.
It also asks if you are finished analyzing this image.  If you check this
box, the macro aborts and you can open another image to analyze.
A running mean and sd of the Guassian FWHM is displayed at the bottom.

The "Move your ROI" and "Fitting OK" prompts will repeat until you Cancel
or check the "finished analyzing this image box".

The fit data as well as the plot profile values are saved in the directory
you picked at the beginning.
This is a text file labeled "filename"+_PlotProfileData.txt which you can
open in Excel and perform further analysis if necessary.

Hopefully the column headings will be self explanatory.  The last one "Plot
Profile Values" is the start of the profile values.


*/






//print("\\Clear");

pixelSize=0.130; //�m
Sigma=2;

dir1=getDirectory("Where to store the data?");

imageToPlot=getImageID();
imageTitle=getInfo("image.filename");

PlotProfileData=File.open(dir1+imageTitle+"_PlotProfileData.txt");
print(PlotProfileData,"FWHM"+","+"offset"+","+"amplitude"+","+"Peak
position"+","+"sigma"+","+"R2"+","+"Plot Profile Values");

FWHMArray=newArray();
ROINum=1;

stopAnalysis=false;
while(stopAnalysis==false){

waitForUser("Please make a new ROI or move your present ROI");
profileArray=getProfile();
selectImage(imageToPlot);
run("Plot Profile");
setLocation(100,100);
PlotProfileTitle=getTitle();
Array.getStatistics(profileArray, profileMin, profileMax, profileMean,
profilestdDev);
ProfileValuesToPrint=",";
xPixelArray=newArray(profileArray.length);
for(i=0;i<profileArray.length;i++){
ProfileValuesToPrint=ProfileValuesToPrint+profileArray[i]+",";
xPixelArray[i]=i;//*pixelSize;
}


initialGuesses=newArray(profileMin,profileMax,(xPixelArray.length/2),Sigma);
 //offset, amplitude,x position,sigma
x=xPixelArray;
y=profileArray;
//Fit.showDialog;
Fit.doFit("Gaussian",x,y,initialGuesses);
Fit.plot;
//run("Save", "save="+dir1+"Plot_profile_fit.txt");
a=Fit.p(0);//offset
b=Fit.p(1);//Amplitude
c=Fit.p(2);//Peak position
d=Fit.p(3);//Sigma
R2=Fit.rSquared;
setLocation(100,500);

ProfileFitTitle=getTitle();
FWHM=2.35482*d;   //FWHM=2(sqrt(2*ln2))*sigma

FWHMArray=appendToArray(FWHM,FWHMArray);
Array.getStatistics(FWHMArray,FWHMMin,FWHMMax,FWHMMean,FWHMSD);


Dialog.create("Fits Ok, continue?");
Dialog.addCheckbox("This fit OK?",true);
Dialog.addMessage("");
Dialog.addCheckbox("Finished analyzing this image?",false);
Dialog.addMessage("");
Dialog.addMessage("FWHM measurements thus far: "+FWHMMean+"±"+FWHMSD+"
 n="+ROINum);
Dialog.show();

FitOK=Dialog.getCheckbox();
stopAnalysis=Dialog.getCheckbox();

if(FitOK==true){
print(PlotProfileData,FWHM+","+a+","+b+","+c+","+d+","+R2+","+ProfileValuesToPrint);
ROINum=ROINum+1;
}
selectWindow(PlotProfileTitle);
close();
selectWindow(ProfileFitTitle);
close();

if(stopAnalysis==true)
File.close(PlotProfileData);
}


function appendToArray(value, array) {
temparray=newArray(lengthOf(array)+1);
for(i=0;i<lengthOf(array);i++){
temparray[i]=array[i];
}
temparray[lengthOf(temparray)-1]=value;
array=temparray;
return array;
}



On Fri, Oct 25, 2013 at 6:51 AM, Lee Hampson <[hidden email]> wrote:

> Hello
>
> I am hoping someone can help me with what I assume is a fairly simple
> problem!
>
> I have a simple macro that places a ROI on an image using the makeOval
> command and then extracts the pixel number, mean...etc and performs some
> calculations.  I'd like to amend it so that the user draws the ROI on the
> image which is then fed into the macro for the statistics to be extracted
> as before but unfortunately I don't know how to achieve this and searching
> on the mailbase has drawn a blank.
>
> Any help would be greatly appreciated!
>
> Lee
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html