Re: macro: convert calibrated value to raw pixel value?

Posted by Herbie on
URL: http://imagej.273.s1.nabble.com/macro-convert-calibrated-value-to-raw-pixel-value-tp5022651p5022665.html

Good day Kenneth,

something like this code should do as well:

///////////////////////////////////////////
requires("1.52r");
if (bitDepth() !=8 ) exit("8bit image required.");
run("Duplicate...", "title=Copy");
run("16-bit");
changeValues( 41, 255, 250 );
changeValues( 31,   40, 200 );
changeValues( 21,   30, 150 );
changeValues( 11,   20, 100 );
changeValues(   1,   10,   50 );
getHistogram(0, counts, 6, 0, 250 );
run("8-bit");
intrv=newArray("v≤0","1≤v≤10","11≤v≤20","21≤v≤30","31≤v≤40","41≤v≤255" );
Table.setColumn("Interval", intrv);
Table.setColumn("Pixel Count", counts);
exit();
///////////////////////////////////////////

Please watch for possible line breaks introduced by the mailer.

Regards

Herbie

:::::::::::::::::::::::::::::::::::::::::::
Am 12.11.19 um 18:13 schrieb Kenneth Sloan:

> Achieving closure.  Just in case the OP prefers a macro...  Here is my final macro version.  It is acceptably speedy, with the use of BatchMode (thanks to Michael for suggesting this).  This version appears to accept any source image type and produces an 8-bit banded image plus counts of pixels in a set of bins based on intensity, as specified by the OP.
>
> Aside from my dinosaur (as in pre K&R C) bracketing style - I welcome comments and suggestions for improvement.
>
> ========================================================================
> // File: Six_Bins.ijm
> // Author: K R Sloan
> // Last Modified: 12 November 2019
> // Purpose: convert an image to 8-bit
> //          map values:
> //              in <=  0.0 -> out =   0
> //        0.0 < in <= 10.0 -> out =  50
> //       10.0 < in <= 20.0 -> out = 100
> //       20.0 < in <= 30.0 -> out = 150
> //       30.0 < in <= 40.0 -> out = 200
> //       40.0 < in         -> out = 250
> function sixBins(pixelIn,counts)
>   {
>    if(pixelIn <=  0.0) {counts[0]++; return   0;}
>    if(pixelIn <= 10.0) {counts[1]++; return  50;}
>    if(pixelIn <= 20.0) {counts[2]++; return 100;}
>    if(pixelIn <= 30.0) {counts[3]++; return 150;}
>    if(pixelIn <= 40.0) {counts[4]++; return 200;}
>    counts[5]++; return 250;
>   }
> source = getImageID();
> title = getTitle();
> w = getWidth();
> h = getHeight();
> setBatchMode(true);
> newImage(title+"_SixBins","8-bit",w,h,1);
> target = getImageID();
> counts = newArray(0,0,0,0,0,0);
> for(y=0;y<h;y++)
>   {
>    for(x=0;x<w;x++)
>     {
>      selectImage(source); pixelIn = getValue(x,y);
>      pixelOut = sixBins(pixelIn,counts);
>      selectImage(target); setPixel(x,y,pixelOut);
>     }
>   }
> setBatchMode("exit and display");
> print("low\thigh\tcount");
> print("-inf\t 0.0\t" + counts[0]);
> print(" 0.0\t10.0\t" + counts[1]);
> print("10.0\t20.0\t" + counts[2]);
> print("20.0\t30.0\t" + counts[3]);
> print("30.0\t40.0\t" + counts[4]);
> print("40.0\t+inf\t" + counts[5]);
> ========================================================================
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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