Looping commands using set number

Dear ImageJ and Fiji community,

I am currently trying to do a thresholding of different circular objects segmented using Hough circle transformation using a user set of loop, because the image has different colors and it will be used for large number of detection in the future.

myProcess2(5);
function myProcess2(Eggs){
	Dialog.create("Test Run");
	Dialog.addNumber("How many eggs ?",Eggnumber);
	Dialog.show();	
	Eggnumber = Dialog.getNumber();
   x="+Eggnumber+";
   i = 1;
   do {
   	run("Threshold...");
	setAutoThreshold("Default dark");
	setThreshold(i, i); 
      run("Analyze Particles...", "size=0-Infinity pixel add");
      i = i + 1;
   }  while (i <= x)
}

Egg map.tif (994.3 KB)
(The image is bugged, but can be detected using ImageJ)

My problem is, the thresholding and particle analysis part did not loop as I intended.
I am currently using ImageJ 1.53c

Many thanks in advance for any tips that you can share.
Best Regards,
Kevin

Hi @Kevin13 ,

I think instead of

x = Eggnumber;

could do the job…

Furthermore, you can remove the line with auto-threshold as you are changing the threshold in the line after anyway.

Was there any error message?

Best,
Robert

1 Like

Seconding

Based on the loop in your code, you might be able to skip the dialog request entirely, and get the max pixel value.

Assuming 0 is the background, you would then increment up to and including the max value in the image. In your case, the max pixel is 5. That could make batch processing faster.
image
Note the max of 5 in Measurements if Min and Max gray values are chosen.

It would help if more specifics were given :slight_smile:

Thank you for your reply @haesleinhuepf and @Research_Associate.

I have made adjustments by changing my macro according to @haesleinhuepf and it works !

Furthermore, i also made adjustments as suggested by @Research_Associate.
This is my version for the looping command to threshold several different colored objects

{
   run("Threshold...");
   setThreshold(1, 65535);
   run("Measure");
   x=getResultString("Max", 0);;
   i = 1;
   do {
	setThreshold(i, i);
      run("Analyze Particles...", "size=500-Infinity pixel add");
      i = i + 1;
   }  while (i <= x)
}

Is there any possibility of improving the macro further ?

Best Regards,
Kevin

2 Likes