Stack macro - progressively changing minimum value (B&C) of slices

Hello,
I am struggling with a seemingly simple macro. It should generate a stack then extract the minimum and maximum values (B&C) of the current slice, proceed to the next slice and increase the minimum value by a fixed value (e.g. 25). By adding a “for loop” to the macro, every slice would have its minimum value updated as minimum (of the previous slice) plus 25. Any idea about what is missing or wrong in the macro ?
Thanks in advance.

macro "darkening microtubules" {
run("microtubule.tif (0.1M)");
for (i = 0; nImages < 10; i++) {
run("Duplicate...", " ");
}
run("Images to Stack");
for (i = 0; i <= nSlices-1; i++) {
setSlice(i+1);
getMinAndMax(min, max);
parseInt(min, max);
run("Next Slice [>]");
setMinAndMax(min+25, max);
run("Apply LUT", "slice");
}
}

Hi @luriCV,

…Silly mistake in part of contribution deleted…

I don’t have the microtubuli file, so I tested this macro:

macro "darkening microtubules" {
	run("MRI Stack (528K)");
	for (i = 0; i <= nSlices-1; i++) {
		setSlice(i+1);
		getMinAndMax(min, max);
		parseInt(min, max);
		run("Next Slice [>]");
		setMinAndMax(min+25, max);
		run("Apply LUT", "slice");
	}
}

But I guess this is not really working with the example stack has [0…255] min…max for every slice…

1 Like

Hello, @eljonco. Thank you very much for your answer.
Actually, the

macro "darkening microtubules" {
run("microtubule.tif (0.1M)");
for (i = 0; nImages < 10; i++) {
run("Duplicate...", " ");
}
run("Images to Stack");

part of the code worked just fine. I am assuming that because a stack with 10 images was generated, and that is what I expected from it.
Problem seems to rely on the second part of the code:

for (i = 0; i <= nSlices-1; i++) {
setSlice(i+1);
getMinAndMax(min, max);
parseInt(min, max);
run("Next Slice [>]");
setMinAndMax(min+25, max);
run("Apply LUT", "slice");
}
}

By running it, second slice gets updated min max of [25…255]. All slices from 4th to 9th remain with unaltered min max values, while 10th slice gets updated min max of [50…255], which would be expected in the 3rd slice.
I hope I can make myself clear this time. Sorry for the incovenience.

What values do you expect to see in the pixels that are below the minimum? What you do currently is redistribute values [25…255] to [0…255] in slice 2 (assuming 0 is in the minimum of the first slice.

1 Like

I don`t expect values below the minimum. I expect that at every slice since the 2nd the minimum value will get increased by 25 relative to previous slice, so: slice 1(min=0), slice 2 (min=25), slice 3 (min=50), …,slice n (min=25*(n-1)).
With the code I posted this seems to work for the second slice only.

Suppose the minimum is 10.
So what do you want to do with pixels with values that are under 10?
Do you all want them to be at 35?
Do you want to remap the histogram to [10…255] -> [35…255]?
Do you want all values under min to be increased by 25 (e.g. if min = 10, 10->35, 11->36 etc)?

1 Like

Yes, that would be it.

changeValues(v1, v2, v3)
Changes pixels in the image or selection that have a value in the range v1 - v2 to v3 . For example, changeValues(0,5,5) changes all pixels less than 5 to 5, and…

That way you can change all pixel values in a range to a constant (v3) so you will have to walk the values you want to change. You need to do the mapping.

But then, if you really want to linearly re-map pixel values into a new range, the solution is even simpler all using Process > Math > commands.
Subtract the offset (ie. your Min value), Divide by the old range (255-Min), multiply by the new range (255-Min-25) and add the offset and your increment (ie. Min+25) should get your pixel values in the desired range.

Of course, you can do the multiply/divide in separate steps but for an 8-bit image you will create overflow, so you better first change Mode to 16-bit, and later back to 8-bit. Alternatively, you calculate the effect of the divide/multiply step (255-Min-25)/255-Min) and apply that to the 8-bit image after subtracting Min. Then add Min+25 and you’re done.

Or am I using too many words for something you don’t mean?

run("microtubule.tif (0.1M)");
inIm=getImageID();
run("Histogram");selectImage(inIm);
Min=10;Shift=25;
run("Subtract...", "value=" + Min);
Multiplier=(255-Min-Shift)/(255-Min);
run("Multiply...", "value=" + Multiplier);
Shift = Shift + Min;
run("Add...", "value=" + Shift);
run("Histogram");
1 Like

Thank you very much for your help, @eljonco.
I’ll add that to the macro
:smiley: