Removing/ changing background automatically for thousands of images

Hello,

I am very new to FiJi/ ImageJ but I cant seem to find a suitable answer for my problem.

I need to remove the background (or change the colour) for thousands of images. An example of the photos is below.

Currently I have tried altering the color threshold and using the wand tool to select the wing and then “Edit > Clear Outside”. However, the wing remains red and I cannot seem to get it back to the original colour.

I have also tried to automate it by making a macro that makes the selection after setting the colour threshold to an ROI and adding it to a ROI manager etc. The problem is that I think the threshold is different for each image.

I have over 2,000 images and so do not have time to do this all manually. Is there a method I can use that will automate this for me?

Any help, would be much appreciated. I don’t want to have to pay for some online tool that automatically removes background from images - surely imageJ has a way to do this? - any plugins?

Thank you all!

Hi @EveTC,

Something like that would be ok ?

nameImg = "CCM1_D_calibrated.jpg";
selectImage(nameImg);
run("Duplicate...", "title=greylev");
run("8-bit");
setAutoThreshold("Otsu");
run("Convert to Mask");
run("Fill Holes");
run("Create Selection");
selectImage(nameImg);
run("Restore Selection");
setBackgroundColor(255, 0, 255); //choose the color you want
run("Clear Outside");
selectImage("greylev"); close();

Nico

1 Like

Oh my goodness - thank you for such a speedy response and exactly what I was looking for!

Do you know if there is a way to auotmatically save to another file?
Something along the lines of this?:

function rm_bkg(input, output, filename) {
        open(input + filename);
        run("Duplicate...", "title=greylev");
		run("8-bit");
		setAutoThreshold("Otsu");
		run("Convert to Mask");
		run("Fill Holes");
		run("Create Selection");
		run("Restore Selection");
		setBackgroundColor(255, 0, 255); //choose the color you want
		run("Clear Outside");
		selectImage("greylev"); close();
        saveAs("Jpeg", output + filename);
        close();
}
	
input = "path_to_input_file";
output = "path_to_ouput_file/";

setBatchMode(true); 
list = getFileList(input);
for (i = 0; i < list.length; i++)
        rm_bkg(input, output, list[i]);
setBatchMode(false);

Although I have just run it and it doesn’t do the process - I think its because I need the

nameImg = “CCM1_D_calibrated.jpg”;
selectImage(nameImg);

but I am not sure how to set this in my method?

Thank you so much for your help

I did it! :smiley: :smiley: :smiley:

I inserted

selectImage(filename);

where your selectImage(namelmg) was

Thank you so much!!!

indeed : you need nameImg = getTitle();
I thing your program is ok (haven’t testest it, but seems ok).

There is an other option.
In imageJ, in Process>Batch>Macro… you can paste my few lines (replacing the first one with nameImg = getTitle():wink: and select an input and output directory.
It will process every image in the input directory and save it in the output directory. You won’t have to bother with getFileList.

Nico

1 Like

Congrats @EveTC ! :smiley:

Thank you for your help @VirtualSlide :smiley: