How to set fixed threshold for a stack?


#1

Hey,
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!


#2

@muffa

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:

Stack.getDimensions(width,height,channels,slices,frames);
for (i = 1; i < slices+1; i++) {
	Stack.setSlice(i);
	//set your threshold here
} 

Does that speed it up at all?

eta


#3

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?


#4

@muffa

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();
Stack.getDimensions(width,height,channels,slices,frames);
//maskStack = newImage("HyperStack", "8-bit color-mode label", 256, 254, 1, slices, 1);
for (i = 1; i < slices+1; i++) {
	Stack.setSlice(i);
	setThreshold(10, 200);
	run("Create Mask");
	if (i == 1) {
		mask = getImageID();
		selectImage(mask);
		rename("maskStack");
	} else if (i == 2) {
		selectImage(mask);
		run("Concatenate...", "  title=[Concatenated Stacks] image1=maskStack image2=mask image3=[-- None --]");
	} else {
		run("Concatenate...", "  title=[Concatenated Stacks] image1=[Concatenated Stacks] image2=mask image3=[-- None --]");
	}
	selectImage(originalStack);
} 

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

eta


#5

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!