Batch processing and artifacts


Developing script to batch process large amounts of files. Objective is to quantify surface pore sizes. See attached function. For the most part this work flow (histogram stretch, median filter, auto local threshold, etc.) has worked fine.

However, sometimes the SEM images will pick up artifacts (#2) in attached picture and is perceived as open pore like #1. If I bump up the median filter radius > 2, the artifact will diminish, but some legitimate pore structures also suffer.

Is there a more robust flow that I should consider to minimize picking up the artifacts?


function analysis(img_filename,fil_num) {
		// stripping off extensions to start with base file name //
		saveAs("tiff", dir2 + baset + "_a" + fil_num); 								// saving cropped image
		// 16 color map and image filter (Bandpass) //
		run("Enhance Contrast...", "saturated=0.0 normalize");	
		run("Median...", "radius=1");						
		//run("Bandpass Filter...", "filter_large=1000 filter_small=1 suppress=None tolerance=5 autoscale saturate");
		saveAs("jpeg",dir2 + baset + "_a" + fil_num + "_16col_filt"); 	
		// Thresholding - Specific Only //
		//setThreshold(0, 60);
		run("Auto Local Threshold", "method=Phansalkar radius=15 parameter_1=.5 parameter_2=.5 white");
		setOption("BlackBackground", false);
		run("Convert to Mask");
		run("Remove Outliers...", "radius=1 threshold=50 which=Bright");
		saveAs("tiff",dir2 + baset + "_a" + fil_num + "__bin"); 						// saving binary image

Perhaps someone will have some straightforward answer, but I would say that, without an original image to test your code on (and adjust said code, or stop it at different points to see the results of intermediate steps), it will be difficult to advise where something has gone wrong.

The artifacts are not caused by image processing but by the preparation of the specimen?

If so, can you mathematically/statistically formulate the difference between artifacts and proper pores?
If not, how can a machine distiguish the structures?

1 Like

It looks like the seam indicated in 2 is “white” while the pores are “black.” I suspect if ROIs are being created, adding measurements from the original image and checking the mean grayscale intensity might work as a later step, but I am not sure.

No issues.

Attached is the tif file. Again most of the time the work flow does work. It just gets to be more of an issue when the surface is mostly smooth with few pore structures. samp1_a2.tif (4.4 MB)


The surface of the specimen is more of the issue. At the beginning of the work flow I would do a histogram stretch with 0.3 pix saturation which made the error worse. Eliminating saturation gave a better result. Short of machine learning, I was hoping to see if there were other avenues I have not encountered.

I think the white line is elevated area, and the dark area is a"shadow." Not as dark as the actual pore. I can manually threshold, and that helps. But common practice for image analysis is to invoke a defensible auto threshold. In this case a local threshold that will treat all my images the same.

Right, my point was that this difference might give you the ability to exclude incorrectly found ROIs using the mean gray intensity within the ROI (in the original image).

Using analyze particles on your final image, I can see that 458 has a mean gray value of 63.79, while something like 728 has a mean gray value of 17.79, much darker.

Many thanks and I agree.

Right now as the script stands, post threshold, the image is processed by the “local thickness” plugin. So I do not set up any ROIs (via particle counter). So I’m unsure how to go about checking the mean gray aras.

I am not so great with all of the coding, unfortunately, but the rough steps are making sure “Set Measurements…” includes the mean gray intensity (this might be on by default). Run Analyze Particles with settings that include “Add to Manager” (this creates the ROIs), then re-open the original image, and “somehow” use the ROIs generated by the masked image created after Despeckling in the original image. It might be as simple as selecting that image, and then running Measure, or there might be additional steps.

I recall there are a few forum posts about transferring ROIs back to original images, at least.

The final steps would be to remove the ROIs with gray values above some set threshold, and then re-save a Masked image of the remaining ROIs.

Doable… but it will take some coding I am afraid.

1 Like

Yep I agree.

Many thanks for your valuable time. I’ll look into the coding specifics.

1 Like

to me, it looks like a case of undersampling; many of the black areas (the pores, I guess) are only one or two pixels wide. You need much more magnification (at least 5 to 10x as much) to get reasonable pore size statistics. Also try to avoid the scratches, by better polishing and by illumination mainly along the scratches.



Many thanks for the input. I’m actually only considering pores starting from 4 pixels and larger. I’ve been working on striking a balance in trying to get the largest FOV without degrading the desired pixel resolution.

These samples are from a process where the random “scratches” are inherent.