Posted by
Kenneth Sloan-2 on
URL: http://imagej.273.s1.nabble.com/macro-convert-calibrated-value-to-raw-pixel-value-tp5022651.html
I decided to learn a bit about the macro language (usually I write everything in Java).
To start with, I thought I would write something simple. Note that this is an exercise in
techniques.
I've hit a snag. I was trying to convert a calibrated 16-bit image to an 8-bit version, by processing it
pixel-by-pixel. Motivated by a previous question here, I wanted to set all pixels < 0.0 to 0,
all pixels in [0.0,10.0) to 50, those in [10.0, 20.0) to 100 ....etc. Finally, all pixels with calibrated
values > 40.0 should be set to 255.
First, I tried going from a source image to a target image. But...this seemed painfully slow because of the need(?)
to constantly selectImage() on every getPixel() and putPixel().
So...I switched to doing it in place. This was speedy enough, but I ran into an issue with calibration.
To categorize the pixels, I wanted calibrated values. OK - I found getValue(x,y) and that was fine. But...now
I need to WRITE a calibrated value. I can't find a function to do that. putPixel(x,y,value) seems to accept raw pixel values.
I found a method to convert raw pixel values to calibrated values, but not the other way around.
Below is the current state of my floundering. All hints gratefully accepted. But please - don't try to tell me how to
perform the high-level task easier. It's just a warm-up exercise to become familiar with all the tools.
Note that I have commented out some lines at the end - these show what I intend to do eventually. But, right now I can't get correct calibrated values written to the image.
I would also be interested in a reasonable way to do this without modifying (or copying) the original source image). It seems to me to be cleaner to write the new pixels to a new target image. But, that appears to be r e a l l y s l o w. (or, I don' know how to do it correctly)
Be kind...
====================================================================================
function sixBins(pixelIn){
if(pixelIn <= 0.0)
pixelOut = 0*5;
else if(pixelIn <= 10.0)
pixelOut = 10*5;
else if(pixelIn <= 20.0)
pixelOut = 20*5;
else if(pixelIn <= 30.0)
pixelOut = 30*5;
else if(pixelIn <= 40.0)
pixelOut = 40*5;
else if(pixelIn > 40.0)
pixelOut = 255.0;
return pixelOut;
}
orig = getImageID();
title = getTitle();
w = getWidth();
h = getHeight();
print(title + " is " + w + "x" + h);
for(row=0;row<h;row++){
for(col=0;col<w;col++) {
pixelIn = getValue(col,row); // gives correct calibrated values!
pixelOut = sixBins(pixelIn); // appears to work fine
setPixel(col,row,pixelOut); // does not work if the image is 16-bit calibrated
print(pixelIn + " -> " + pixelOut); // really verbose debugging
}
}
run("Histogram");
// selectImage(orig);
// setMinAndMax(0,255);
// run("8-bit");
// selectImage(orig);
// run("Histogram");
====================================================================================
--
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