Adjust background intensity

Dear all,
I would like to adjust the intensity of my Z stack.
I would like to measure the mean, the minimum and the maximum intensity of each slide. I would like to adjuste the min and max intensity depending on the mean intensity. I wrote a macro:

adjustBackground();
function adjustBackground(){
	run("Set Measurements...", "area mean standard min display redirect=None decimal=3");
	slices=nSlices();
	for(i=1 ; i<=slices ; i++) { //function to adjust the bright field background according with the initial mean intensity (cutoff 8).
		setSlice(i);
		List.setMeasurements();
		background = List.getValue("Mean");
		print(background);
		min = List.getValue("Min");
		print(min);
		max = List.getValue("Max");
		print(max);
		if(background<8) {
		setMinAndMax(min, 80); // for dark bright field
		}
		if(background>8) {
		setMinAndMax(min/1.2, max);// for bright bright field
	}
	}
}

So, ideally, I would like that on those slides where the intensity is below 8, the minAndMax intensity were adjusted to (min, 80). If the mean intensity of the slide was below 8, then I would like that the intensity was adjusted to (min/1.2, max).

Unfortunately, the script adjust the intensities of the slides according with the last slide which is a black slide.

Can someone help me to understand what is not working here?

Thank you.

Hi,

Brightness & Contrast

The macro command setMinAndMax is the same and changing the brightness and contrast. This changes how the whole channel (ie all the Z slices and Time points) are displayed on your screen. It does not change the values of the pixels - just how they are displayed.

So when your script runs it steps through get slice and changes the disply of the whole Z stack, one slice at a time. The last setting comes from the final Z slice so this is all you see.

You can see this by putting a wait(2000); command in your macro.

Multiply and Add

To change each slice separately you will need to change the values of the pixels on each slice. To do this you need to stretch and scale the pixel values. Use the Multiply and Add commands from the Process > Math menu.

Here’s a script does this. It makes a copy of the original as you will be changing the pixels.

Cheers,

Chris

run("Duplicate...", "title=copy duplicate");
adjustBackground();

function adjustBackground(){
	run("Set Measurements...", "area mean standard min display redirect=None decimal=3");
	slices=nSlices();
	for(i=1 ; i<=slices ; i++) { //function to adjust the bright field background according with the initial mean intensity (cutoff 8).
		setSlice(i);
		List.setMeasurements();
		background = List.getValue("Mean");
		print(background);
		min = List.getValue("Min");
		print(min);
		max = List.getValue("Max");
		print(max);
		if(background<8) {
			//setMinAndMax(min, 80); // for dark bright field
	        new_min = min;		
            rng = 80 - new_min;
			
		}
		if(background>8) {
			//setMinAndMax(min/1.2, max);// for bright bright field
			new_min=min/1.2;
			rng = max-new_min;
		}

		// Rescale the pixels and move the minimum
		fact = 255./rng;
		print(new_min,fact);
		run("Multiply...", "value="+fact+   " slice");
		run("Add...",      "value="+new_min+" slice");
		//wait(100);
	}
}