Histogram of Line Selection

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

Histogram of Line Selection

Peter van Loon
Hi all,

I like to get the Histogram of a line selection in an 8bit image.
When I use the histogram function I get the histogram of the whole image instead of the selection. Is there a easy way to get the greyvalue distribution of a line selection. By using Plot Profile I get the greyvalues per pixel but not as an histogram.

Kind regards,
Peter

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Histogram of Line Selection

Volker Baecker
Maybe you could use an area selection of width one instead of the line
selection. For area selections the histogram can be calculated. You can
use "Edit>Selection>Line to Area" to create an area selection from a
line selection.
Volker

On 10/01/13 12:01, Peter van Loon wrote:

> Hi all,
>
> I like to get the Histogram of a line selection in an 8bit image.
> When I use the histogram function I get the histogram of the whole image instead of the selection. Is there a easy way to get the greyvalue distribution of a line selection. By using Plot Profile I get the greyvalues per pixel but not as an histogram.
>
> Kind regards,
> Peter
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Histogram of Line Selection

Peter van Loon
Hi Volker,

Thanks for your reply!

I already use this kind of selection with one pixel width if the 'line' is horizonal or vertical. I use 'makeRectangle' in my macros for this. But if the line is rotated I need to rotate it afterwards. So I have to do some calculations to get the selection on the position I want. It is possible but I hope there is a simpler solution.

Kind regards,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: Histogram of Line Selection

George Patterson-3
Hi Peter,
This isn't really more simple, but I needed a similar process recently and couldn't find the proper way to do it.
So I wrote the little makeHistogram function below to make a histogram from data in an array.  I edited a macro a bit to fit your needs.....I think.  
The getProfile() command returns an array of the Plot Profile values without displaying the Plot profile.  However, I included a command to show the plot profile.
To use this macro, you just need to open your image, make your line selection, and run.
You'll get a dialog asking for the number of bins (sorry, no bin size option right now) and if you'd like to save the histogram values.  If you check the box you'll be prompted at the end for a directory into which a comma-delimited text file will be saved.
I included another little function which will plot the histogram as columns since again I was never able to figure how to do it with real ImageJ command.
Disclaimer here.  It's not the prettiest nor most efficient coding and might have a bug or two I've over-looked.  If you use the macro and notice any bugs or problems, please let me know so I can fix them.
Thanks,
George






//start of macro

print("\\Clear");
imageToPlot=getImageID();
imageTitle=getTitle();
print(imageTitle);

profileValues=getProfile();
selectImage(imageToPlot);
run("Plot Profile");

Dialog.create("Make Histogram");
Dialog.addNumber("Number of bins for histogram", 50);
Dialog.addCheckbox("Save histogram values?", false);
Dialog.show();

numBins=Dialog.getNumber();
saveHistogramValues=Dialog.getCheckbox();

makeHistogram(numBins,profileValues);

if(saveHistogramValues==1){
dir1=getDirectory("Where should the histogram values be stored?");
selectWindow("Log");
saveAs("Text",dir1+imageTitle+"_HistogramValues");
}



//********************functions below here

function makeHistogram(nBins, ArrayName){
histogramBinArrayMin=newArray(nBins);
histogramBinArrayMax=newArray(nBins);
histogramBinArray=newArray(nBins);
histogramPlotXArray=newArray(nBins);
histogramPlotFreqArray=newArray(nBins);
Array.getStatistics(ArrayName, ArrayMin, ArrayMax, ArrayMean, ArraystdDev);
binSize=(ArrayMax-ArrayMin)/(nBins+1);

//set the bin values
for(i=0;i<nBins;i++){
histogramBinArrayMin[i]=ArrayMin+(i*binSize);
histogramBinArrayMax[i]=ArrayMin+((i+1)*binSize);
}

//detrmine the number plot profile values in each bin
for(i=0;i<lengthOf(ArrayName);i++){
chkE=ArrayName[i];
for(j=0;j<nBins;j++){
if(chkE>=histogramBinArrayMin[j] && chkE<histogramBinArrayMax[j]){
chkBinCount=histogramBinArray[j];
histogramBinArray[j]=chkBinCount+1;
}
if(chkE==ArrayMax){
chkBinCount=histogramBinArray[nBins-1];
}
}
}

//generates the values for plotting
for(i=0;i<nBins;i++){
histogramPlotXArray[i]=(histogramBinArrayMin[i]+histogramBinArrayMax[i])/2;//for x axis center in the middle of the bin
histogramPlotFreqArray[i]=histogramBinArray[i]/(lengthOf(ArrayName)); //for y axis is number in each bin divided by the total number of plot values
}

//print to the log window in case you want to save the histogram info
print("\\Clear");
for(i=0;i<histogramPlotXArray.length;i++){
print(histogramPlotXArray[i]+","+histogramPlotFreqArray[i]);
}

//make a plot of the histogram
PlotAsColumns("Histogram", "Bin values", "Frequency", histogramPlotXArray, histogramPlotFreqArray,binSize);
Plot.addText(ArrayMean+" ± "+ArraystdDev,0,0.1);
Plot.show();
}//end of makeHistogram

/*************
function PlotAsColumns(PlotName,xAxisText,yAxisText,xArray,yArray,columnWidth){

Array.getStatistics(xArray,xmin,xmax,xmean,xstdDev);
Array.getStatistics(yArray,ymin,ymax,ymean,ystdDev);

Plot.create(PlotName, xAxisText, yAxisText);
Plot.setLimits(xmin-columnWidth/2,xmax+columnWidth/2,0,ymax);
for(i=0;i<xArray.length;i++){
x1=xArray[i]-columnWidth/2;
x2=xArray[i]+columnWidth/2;
y1=yArray[i];
y2=y1;
Plot.drawLine(x1,y1,x2,y2);
Plot.drawLine(x1,y1,x1,0);
Plot.drawLine(x2,y2,x2,0);
}
}//end of PlotAsColumns

//end of macro








On Jan 10, 2013, at 7:26 AM, Peter van Loon wrote:

> Hi Volker,
>
> Thanks for your reply!
>
> I already use this kind of selection with one pixel width if the 'line' is
> horizonal or vertical. I use 'makeRectangle' in my macros for this. But if
> the line is rotated I need to rotate it afterwards. So I have to do some
> calculations to get the selection on the position I want. It is possible but
> I hope there is a simpler solution.
>
> Kind regards,
> Peter
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Histogram-of-Line-Selection-tp5001353p5001355.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

George H. Patterson
Building 13 3E33
13 South Drive
Biophotonics Section
National Institute of Biomedical Imaging and Bioengineering
National Institutes of Health
Bethesda, MD 20892
Office: 301-443-0241
Fax: 301-496-6608
[hidden email]

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Histogram of Line Selection

Peter van Loon
Hi George,

Thank you for the answer.
Your code contains all relevant information I want.

The getProfile() and Array.getStatistics are the essential parts for my macro combined with some loops.

Regards,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: Histogram of Line Selection

Unruh, Jay-2
In reply to this post by George Patterson-3
I have a plugin (Plugins>Trajectory Tools>histogram traj jru v1) that does this on my website (http://research.stowers.org/imagejplugins).  Of course it isn't the built in histogram but rather a custom one built by myself, but I consider that a positive thing.

Jay

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of George Patterson
Sent: Thursday, January 10, 2013 11:52 AM
To: [hidden email]
Subject: Re: Histogram of Line Selection

Hi Peter,
This isn't really more simple, but I needed a similar process recently and couldn't find the proper way to do it.
So I wrote the little makeHistogram function below to make a histogram from data in an array.  I edited a macro a bit to fit your needs.....I think.  
The getProfile() command returns an array of the Plot Profile values without displaying the Plot profile.  However, I included a command to show the plot profile.
To use this macro, you just need to open your image, make your line selection, and run.
You'll get a dialog asking for the number of bins (sorry, no bin size option right now) and if you'd like to save the histogram values.  If you check the box you'll be prompted at the end for a directory into which a comma-delimited text file will be saved.
I included another little function which will plot the histogram as columns since again I was never able to figure how to do it with real ImageJ command.
Disclaimer here.  It's not the prettiest nor most efficient coding and might have a bug or two I've over-looked.  If you use the macro and notice any bugs or problems, please let me know so I can fix them.
Thanks,
George






//start of macro

print("\\Clear");
imageToPlot=getImageID();
imageTitle=getTitle();
print(imageTitle);

profileValues=getProfile();
selectImage(imageToPlot);
run("Plot Profile");

Dialog.create("Make Histogram");
Dialog.addNumber("Number of bins for histogram", 50); Dialog.addCheckbox("Save histogram values?", false); Dialog.show();

numBins=Dialog.getNumber();
saveHistogramValues=Dialog.getCheckbox();

makeHistogram(numBins,profileValues);

if(saveHistogramValues==1){
dir1=getDirectory("Where should the histogram values be stored?"); selectWindow("Log"); saveAs("Text",dir1+imageTitle+"_HistogramValues");
}



//********************functions below here

function makeHistogram(nBins, ArrayName){ histogramBinArrayMin=newArray(nBins);
histogramBinArrayMax=newArray(nBins);
histogramBinArray=newArray(nBins);
histogramPlotXArray=newArray(nBins);
histogramPlotFreqArray=newArray(nBins);
Array.getStatistics(ArrayName, ArrayMin, ArrayMax, ArrayMean, ArraystdDev); binSize=(ArrayMax-ArrayMin)/(nBins+1);

//set the bin values
for(i=0;i<nBins;i++){
histogramBinArrayMin[i]=ArrayMin+(i*binSize);
histogramBinArrayMax[i]=ArrayMin+((i+1)*binSize);
}

//detrmine the number plot profile values in each bin for(i=0;i<lengthOf(ArrayName);i++){
chkE=ArrayName[i];
for(j=0;j<nBins;j++){
if(chkE>=histogramBinArrayMin[j] && chkE<histogramBinArrayMax[j]){ chkBinCount=histogramBinArray[j]; histogramBinArray[j]=chkBinCount+1;
}
if(chkE==ArrayMax){
chkBinCount=histogramBinArray[nBins-1];
}
}
}

//generates the values for plotting
for(i=0;i<nBins;i++){
histogramPlotXArray[i]=(histogramBinArrayMin[i]+histogramBinArrayMax[i])/2;//for x axis center in the middle of the bin histogramPlotFreqArray[i]=histogramBinArray[i]/(lengthOf(ArrayName)); //for y axis is number in each bin divided by the total number of plot values }

//print to the log window in case you want to save the histogram info print("\\Clear"); for(i=0;i<histogramPlotXArray.length;i++){
print(histogramPlotXArray[i]+","+histogramPlotFreqArray[i]);
}

//make a plot of the histogram
PlotAsColumns("Histogram", "Bin values", "Frequency", histogramPlotXArray, histogramPlotFreqArray,binSize); Plot.addText(ArrayMean+" ± "+ArraystdDev,0,0.1); Plot.show(); }//end of makeHistogram

/*************
function PlotAsColumns(PlotName,xAxisText,yAxisText,xArray,yArray,columnWidth){

Array.getStatistics(xArray,xmin,xmax,xmean,xstdDev);
Array.getStatistics(yArray,ymin,ymax,ymean,ystdDev);

Plot.create(PlotName, xAxisText, yAxisText); Plot.setLimits(xmin-columnWidth/2,xmax+columnWidth/2,0,ymax);
for(i=0;i<xArray.length;i++){
x1=xArray[i]-columnWidth/2;
x2=xArray[i]+columnWidth/2;
y1=yArray[i];
y2=y1;
Plot.drawLine(x1,y1,x2,y2);
Plot.drawLine(x1,y1,x1,0);
Plot.drawLine(x2,y2,x2,0);
}
}//end of PlotAsColumns

//end of macro








On Jan 10, 2013, at 7:26 AM, Peter van Loon wrote:

> Hi Volker,
>
> Thanks for your reply!
>
> I already use this kind of selection with one pixel width if the
> 'line' is horizonal or vertical. I use 'makeRectangle' in my macros
> for this. But if the line is rotated I need to rotate it afterwards.
> So I have to do some calculations to get the selection on the position
> I want. It is possible but I hope there is a simpler solution.
>
> Kind regards,
> Peter
>
>
>
> --
> View this message in context:
> http://imagej.1557.n6.nabble.com/Histogram-of-Line-Selection-tp5001353
> p5001355.html Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

George H. Patterson
Building 13 3E33
13 South Drive
Biophotonics Section
National Institute of Biomedical Imaging and Bioengineering National Institutes of Health Bethesda, MD 20892
Office: 301-443-0241
Fax: 301-496-6608
[hidden email]

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

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