How to set fixed threshold for a stack?



I’m trying to set a fixed threshold for an image stack, but don’t know how do that since imagej sets different thresholds for each single image automatically.
At the end I want to binarize the images on the basis of the set threshold for the entire stack.
When using “stacks to images” in a macro with a following “setThreshold(a,b)” imagej crashes every time - due to more than 1000 images in one stack. So I want to avoid to do it this way.
Any idea how to manage this?
Thanks in advance!



Welcome to the Forum!

Perhaps you can just write a loop statement to move from slice-to-slice in your image stacks, setting the threshold as you go?

Something like:

for (i = 1; i < slices+1; i++) {
	//set your threshold here

Does that speed it up at all?



Hey etarena,
thanks for your help :slight_smile: it really speeds up the process. Now I have the set threshold for every image in the stack. Now the next step would be to binarize the whole stack. But when using “make binary” imagej will calculate a new threshold. I still don’t know how to keep the set threshold and doing a binarization (based on the threshold) for the whole stack.
Am I overlooking something?



Fair point! Ok… this is a bit of a mess at the moment… but it works on my test images.

You will have to play and edit it a bit - but at least gives you a sense of what needs to happen:

originalStack = getImageID();
//maskStack = newImage("HyperStack", "8-bit color-mode label", 256, 254, 1, slices, 1);
for (i = 1; i < slices+1; i++) {
	setThreshold(10, 200);
	run("Create Mask");
	if (i == 1) {
		mask = getImageID();
	} else if (i == 2) {
		run("Concatenate...", "  title=[Concatenated Stacks] image1=maskStack image2=mask image3=[-- None --]");
	} else {
		run("Concatenate...", "  title=[Concatenated Stacks] image1=[Concatenated Stacks] image2=mask image3=[-- None --]");

I really hope this helps! For sure - there are more elegant solutions out there…



Thanks a lot for this! Yes, it works, but it concatenates the images in reverse order. You have any idea, why it could be like this? Well, I’m still a beginner with programming and have difficulties in understanding your loop. :slight_smile: But anyway thanks for your help!