2D counting not working after specific filtering step

The first code simply do a 2D counting using Particle Analyzer after segmenting the image. It works fine and I can see the normal counting as expected,

imagePlusfiltered = new Duplicator().run(imagePlus);
IJ.run(imagePlusfiltered, "Make Binary", "method=Default background=Dark calculate");

ParticleAnalyzer particleAnalyzer = new ParticleAnalyzer(
ParticleAnalyzer.INCLUDE_HOLES +
ParticleAnalyzer.STACK_POSITION , 
-1, rt, 5, Double.MAX_VALUE, 0, 1);

for (int imgSliceindx=0 ; imgSliceindx < imagePlusfiltered.getStackSize(); imgSliceindx++) {
            imagePlusfiltered.setSliceWithoutUpdate(imgSliceindx+1);
            particleAnalyzer.analyze(imagePlusfiltered);
            particleAnalyzer.setRoiManager(roi);
            particleAnalyzer.setResultsTable(rt);
}
  1. The result without inversion line before 2D counting analyzing line (white background)
  2. The result with inversion line (black background)

image image

In the second case I am doing the same steps just added on it a subtraction step the original from a median filtered one and this time the counting is showing 1 per channel as if the image is inverted. Inverting the image doesn’t solve the problem.

ImagePlus imagePlusDuplicate = new Duplicator().run(imagePlus);

// filtering the original image and subtracting the result from the original
IJ.run(imagePlusDuplicate, "Median...", "radius=15 stack");
ImageCalculator imageCalculator = new ImageCalculator();
imagePlusfiltered = imageCalculator.run("Subtract create stack", imagePlus, imagePlusDuplicate);

// applying segmentation and post processing
IJ.run(imagePlusfiltered, "Make Binary", "method=Default background=Dark calculate");
IJ.run(imagePlusfiltered, "Dilate", "stack");
IJ.run(imagePlusfiltered, "Erode", "stack");

// 2D Counting for all slices
ParticleAnalyzer particleAnalyzer = new ParticleAnalyzer(
ParticleAnalyzer.INCLUDE_HOLES +
ParticleAnalyzer.STACK_POSITION , 
-1, rt, 5, Double.MAX_VALUE, 0, 1);

for (int imgSliceindx=0 ; imgSliceindx < imagePlusfiltered.getStackSize(); imgSliceindx++) {
            imagePlusfiltered.setSliceWithoutUpdate(imgSliceindx+1);
            particleAnalyzer.analyze(imagePlusfiltered);
            particleAnalyzer.setRoiManager(roi);
            particleAnalyzer.setResultsTable(rt);
}

The result same as before

image image

Can anyone guess what would be the problem or what approach I can take to get close to where the problem is.

Now I got the reason behind this but I still can’t understand why this is happening. The problem was dilation and erosion. The resulted image of dilatation then erosion was not counted as Counter needs directly the image to be the output of make binary. I tested with Fiji and found the counting is taking place normally after the result of dilation and erosion.

Any one can know why?
I would also appreciate if there is a suggestion to do the same steps in different way in Java as currently I omitted the erosion/dilation steps.