I can not segment all the desired elements



I’m trying to create a macro to count and characterize the porosity of a sample. I apply medium filter and sharppen. then I try to apply the threshold by selecting the first value in the histogram. the best point I could find.

but there are still many pores that I can not capture through these procedures. I’ve marked some pores in blue that I can not select. any tips?


This looks like a perfect fit for trainable segmentation. Have you tried it on your data?


I’m new here. I do not know weka.


For tasks such as classification of pixels/segmentation it is desirable and more flexible in most cases for your computer to learn what is foreground and background (or more classes if needed) from examples that you provide (in this case by painting labels) instead of hard-coding features and manually finding an appropriate threshold. I recommend you read the wiki article I linked to above.


Good day,

the main problem with your image and thresholding is the uneven background.

But there are other problems with your data and approach:

  1. Never ever use JPG-compressed images, because they introduce artifacts that cannot be removed. Use images preferably in TIF-format and for posting on the Forum in PNG-Format. (There is no sense in converting JPG-compressed images to one of these formats!)
  2. Do not use hand-set thresholds for scientific analyses, because the aren’t reproducible. Use one of the automatic threshold schemes and if none works for you, invent one of your own or improve the image quality.

Here is a way to make the background more even:

  1. With your image open in ImageJ go to “Process >> FFT >> Bandpass Filter…”
  2. In the dialog enter “[…] down to” 50 pixels and “[…] up to” 0 pixels and uncheck the three options below.
  3. Click OK
  4. Apply the automatic threshold scheme “Li” to get the binarized image

Here is what I get with this approach:




thank you very much herbir.
I believe that leaving the uniform background helps to segment the image in a better way.

to caluculate the threshold I created this algorithm. in the post initially it had placed the value manually that the algorithm returned, only in order to present the value of the first valley of the histogram.

public int calculeThreshold(ImagePlus imp) {

		ImageProcessor ip = imp.getProcessor();
		int[] h = ip.getHistogram();

		int tValue = 0;
		int t = 0;
                int Limite_Operacoes = 8;

		if (h != null) {
			boolean condicoes = false;
			tValue = h[0];

			for (int i = 0; i < 255; i++) {			
				condicoes = false;

				if (i > 6 && i < 100) {
					for (int x = 1; x < Limite_Operacoes; x++) {
						if (h[i] < h[i - x] && (h[i] < h[i + x])) {							
							condicoes = true;
						} else {						
							condicoes = false;
					if (condicoes) {						
						t = i;
						tValue = h[i];
						System.out.println("Threshold: " + t);
						return t;


		return 0;


Isn’t the automatic threshold scheme “Li” doing a good job?

I think there is no real need to do a threshold scheme of your own …

You may also increase the value for “[…] down to” 200 pixels or even more.
This gives better results near the image borders.