Re: get 3 max and min values

Posted by Christopher Coulon-2 on
URL: http://imagej.273.s1.nabble.com/get-3-max-and-min-values-tp3692913p3692914.html

> I know that you can get the max and min of a selection with the getMinAndMax
> function (or getStatistics). Is there a way to get e.g. the 3 maximum or
> minimum values. I was searching for a function to list all the values in a
> selection, and then sorting those and getting X maximal or minimal values.
> However, I cannot find a way of listing all the values in my selection. Also,
> I don't know if it possible to then easily sort them to get the highest
> values.
>
I finally got a bit of time to write a demonstration macro to illustrate how
this can be done.  It is a bit crude, but it works.  :-)

Chris


run("HeLa Cells (1.3M, 48-bit RGB)");

w = getWidth;
h = getHeight;

maxPix1 = 0;
maxPix2 = 0;
maxPix3 = 0;

maxPix = 0;
minPix = 0;

minPix1 = 100000000.00;
minPix2 = 100000000.00;
minPix3 = 100000000.00;

for (slice = 1; slice <= 3; slice ++) {
    setSlice(slice);
    for (x = 0; x < w; x++) {
        for (y = 0; y < h; y++) {
            pix = getPixel(x, y);
            if (pix > maxPix1) {
                maxPix = maxPix1;
                maxPix1 = pix;
            }
            if (pix > maxPix2 && pix < maxPix1 || maxPix2 < maxPix && maxPix
< maxPix1) {
                if (pix > maxPix) {
                    if (pix < maxPix1) {
                        maxPix = maxPix2;
                        maxPix2 = pix;
                    }
                    else maxPix2 = maxPix;
                }
                else maxPix2 = maxPix;
            }
            if (pix > maxPix3 && pix < maxPix2 || maxPix3 < maxPix && maxPix
< maxPix2) {
                if (pix > maxPix) {
                    if (pix < maxPix2) {
                        maxPix = maxPix3;
                        maxPix3 = pix;
                    }
                    else maxPix3 = maxPix;
                }
                else maxPix3 = maxPix;
            }

            if (pix < minPix1) {
                minPix = minPix1;
                minPix1 = pix;
            }
            if (pix < minPix2 && pix > minPix1 || minPix2 > minPix && minPix
> minPix1) {
                if (pix < minPix) {
                    if (pix > minPix1) {
                        minPix2 = pix;
                    }
                    else minPix2 = minPix;
                }  
                else minPix2 = pix;
            }
            if (pix < minPix3 && pix > minPix2 || minPix3 > minPix && minPix
> minPix2) {
                if (pix < minPix) {
                    if (pix > minPix2) {
                        minPix = minPix3;
                        minPix3 = pix;
                    }
                    else minPix3 = minPix;
                }  
                else minPix3 = pix;
            }
        }
    }
}

print("max 1 = " + maxPix1 + " min 1 = " + minPix1);
print("max 2 = " + maxPix2 + " min 2 = " + minPix2);
print("max 3 = " + maxPix3 + " min 3 = " + minPix3);

       

The GAIA Group
 Global Automated Image Analysis

We help Researchers help themselves!
Find out about Online Tutoring for ImageJ

Christopher Coulon, Ph.D., Founder
http://gaiag.net
[hidden email]