Transform Individual Pixels in an Image

Hi all,
I am trying to transform each individual pixel in a 16 bit image based off of its value. For example, if it is less than 2000, I would add 1281 to its value. if it is >= 2000, I would subtract 1381. I have written a script that goes through each individual pixel and queries the pixel using the “measure” function. However, within my if/else statement, I am only able to transform the entire image, not the individual pixel.

I have copied and pasted my for loop below. (I have the print statements in there to help debug). Are there any methods to only transform a single pixel? Also, as I am new to doing this particular task, is there a method to query each individual pixel in a more efficient method for my intended use? I could not find any other method other than this brute force approach.

Thanks for your help.

var xpix = 10;
var ypix = 5;
var x = 0;
var y= 0;
var pixelvalue = 0;

for (x=0; x<xpix; x++) {
for (y=0; y<ypix; y++) {
pixelvalue = getResult(“Mean”);
if (pixelvalue <2000){
run(“Add…”, “value=1231”);
print (pixelvalue);
} else{
run(“Subtract…”, “value=1381”);

Hi @rlangsner, welcome to the forum

Your method could be ok, but it would last a long long time to compute. You should prefer global method instead of pixel by pixel methods. They will use more memory, but will compute faster.
For example here, you should use the tresholding and the math functions.

  1. You threshold your original image [0-2000]. You get a mask.
  2. You duplicate your original image and add 1231 to it. Then you apply your mask on it.
  3. You substract 1381 to your original image and use the inverse of your mask on it.
  4. Then you add your two images to get the final result.

(Of course, this can also be done with ROIs)


Hi Nico,
Thank you for your help. That works really well. I had tried the thresholding earlier, but was not sure how to create a mask. Thanks for the tip. This worked for me.


For reference sake, should others end up here using a forum search, there is the setPixel(x,y,value) and the two functions to obtain values at (x,y) are getPixel(x,y) for the raw uncalibrated value and getValue(x,y) for calibrated values. Note to self: requires 1.52p