Posted by
Giorgio Cadoro on
May 22, 2009; 11:31pm
URL: http://imagej.273.s1.nabble.com/Bet-way-to-get-mean-value-of-ROI-in-a-plugin-tp3692428p3692430.html
I have written this file to calculate the statistics of the image
(stack) taking the rois stored in the roi manager.
For example it can be called by this method:
public HistogramWindow(String title, ImagePlus imp, ImageStatistics
stats) in public class HistogramWindow package ij.gui
new HistogramWindow ("title",new MyStat(imp,RoiManager.getInstance());
Giorgio
MyStat.java :
import ij.ImagePlus;
import ij.gui.Roi;
import ij.plugin.frame.RoiManager;
import ij.process.ImageProcessor;
import ij.process.ImageStatistics;
import java.awt.Rectangle;
public class MyStat extends ImageStatistics{
public MyStat(byte[]pixels,int[]hist){
super();
histogram=hist;
int minThreshold=0,maxThreshold=255;
getRawStatistics(minThreshold,maxThreshold);
getRawMinAndMax(minThreshold,maxThreshold);
calculateMoments(pixels, minThreshold, maxThreshold);
}
public MyStat(ImagePlus imp,RoiManager rm){
super();
histogram = new int[256];
for(int i=0;i<256;i++)
histogram[i]=0;
float v;
double dv, dv2, sum1=0.0, sum2=0.0, sum3=0.0, sum4=0.0;
if(rm!=null){
ImageProcessor ip = imp.getProcessor();
for(int index=0;index<rm.getCount();index++){
Roi roiCurr =rm.getRoisAsArray()[index];
int indexImg=rm.getSliceNumber(rm.getList().getItem(index));
imp.setSlice(indexImg);
imp.setRoi(roiCurr);
byte[] mask = ip.getMaskArray();
int width, height;
int rx, ry, rw, rh;
double pw, ph;
width = ip.getWidth();
height = ip.getHeight();
Rectangle roi = ip.getRoi();
if (roi != null) {
rx = roi.x;
ry = roi.y;
rw = roi.width;
rh = roi.height;
} else {
rx = 0;
ry = 0;
rw = width;
rh = height;
}
pw = 1.0;
ph = 1.0;
roiX = rx*pw;
roiY = ry*ph;
roiWidth = rw*pw;
roiHeight = rh*ph;
for (int y=ry, my=0; y<(ry+rh); y++, my++) {
int i = y * width + rx;
int mi = my * rw;
for (int x=rx; x<(rx+rw); x++) {
if (mask==null || mask[mi++]!=0) {
if(imp.getRoi().contains(x,y)){
v = ip.getPixelValue(x,y);
dv = v+Double.MIN_VALUE;
dv2 = dv*dv;
sum1 += dv;
sum2 += dv2;
sum3 += dv*dv2;
sum4 += dv2*dv2;
histogram[(int) v]++;
if (v<min) min= v;
if (v>max) max= v;
}
}
i++;
}
}
// ip.reset(ip.getMask());
}
int count;
double sum = 0.0;
for (int i=0; i<256; i++) {
count = histogram[i];
pixelCount += count;
sum += (double)i*count;
if (count>maxCount) {
maxCount = count;
mode = i;
}
}
area = pixelCount;
mean = sum/pixelCount;
umean = mean;
dmode = mode;
histMin = 0.0;
histMax = 255.0;
double mean2 = mean*mean;
double variance = sum2/pixelCount - mean2;
stdDev=Math.sqrt(variance);
skewness = ((sum3 - 3.0*mean*sum2)/pixelCount +
2.0*mean*mean2)/(variance*stdDev);
kurtosis = (((sum4 - 4.0*mean*sum3 + 6.0*mean2*sum2)/pixelCount -
3.0*mean2*mean2)/(variance*variance)-3.0);
}
}
void getRawStatistics(int minThreshold, int maxThreshold) {
int count;
double value;
double sum = 0.0;
double sum2 = 0.0;
for (int i=minThreshold; i<=maxThreshold; i++) {
count = histogram[i];
pixelCount += count;
sum += (double)i*count;
value = i;
sum2 += (value*value)*count;
if (count>maxCount) {
maxCount = count;
mode = i;
}
}
area = pixelCount;
mean = sum/pixelCount;
umean = mean;
dmode = mode;
calculateStdDev(pixelCount, sum, sum2);
histMin = 0.0;
histMax = 255.0;
}
void getRawMinAndMax(int minThreshold, int maxThreshold) {
int min = minThreshold;
while ((histogram[min] == 0) && (min < 255))
min++;
this.min = min;
int max = maxThreshold;
while ((histogram[max] == 0) && (max > 0))
max--;
this.max = max;
}
void calculateMoments(byte[] pixels, int minThreshold, int maxThreshold) {
int v, i;
double dv, dv2, sum1=0.0, sum2=0.0, sum3=0.0, sum4=0.0;
for (i=0;i<pixels.length;i++){
v = pixels[i]&255;
if (v>=minThreshold&&v<=maxThreshold) {
dv = v+Double.MIN_VALUE;
dv2 = dv*dv;
sum1 += dv;
sum2 += dv2;
sum3 += dv*dv2;
sum4 += dv2*dv2;
}
double mean2 = mean*mean;
double variance = sum2/pixelCount - mean2;
double sDeviation = Math.sqrt(variance);
skewness = ((sum3 - 3.0*mean*sum2)/pixelCount +
2.0*mean*mean2)/(variance*sDeviation);
kurtosis = (((sum4 - 4.0*mean*sum3 + 6.0*mean2*sum2)/pixelCount -
3.0*mean2*mean2)/(variance*variance)-3.0);
}
}
void calculateStdDev(int n, double sum, double sum2) {
//ij.IJ.write("calculateStdDev: "+n+" "+sum+" "+sum2);
if (n>0) {
stdDev = (n*sum2-sum*sum)/n;
if (stdDev>0.0)
stdDev = Math.sqrt(stdDev/(n-1.0));
else
stdDev = 0.0;
}
else
stdDev = 0.0;
}
public String toString() {
return "stats[count="+pixelCount+", mean="+mean+",
skwness="+skewness+",
kurtosis="+kurtosis+"pixelcount="+pixelCount+"]";
}
}
2009/5/22 hekmat <
[hidden email]>:
> Hi
> I am wondering that is there any way to get the each pixel value of the roi
> drawn on the image, so that I can use it for histogram? I usually get the
> mean of the roi if I draw a roi on the image, but I would like to get each
> pixel value for region I specify.
> Thanks
> Shah
>
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:
[hidden email]] On Behalf Of Jean
> Pierre CLAMME
> Sent: Thursday, May 21, 2009 7:16 PM
> To:
[hidden email]
> Subject: Bet way to get mean value of ROI in a plugin?
>
> Hello,
>
> I need to get the mean value of the intensity in a ROI to use in a
> plugin. Right now, I'm using the measure function, then I go and retrieve
> the value in the result table.
> I was wondering if there would be a cleaner / easier /better way to
> retrieve this value ?
>
> Thanks
>
> JP
>