Threshold macro gives different result on same pic every time

macro
fiji
thresholding
Tags: #<Tag:0x00007fd541e7d718> #<Tag:0x00007fd541e7d510> #<Tag:0x00007fd541e7d330>

#1

Hi everybody,

I am new to imageJ and currently working to develop a way to detect areas (in this case jellyfish) from picture frames in order to count them. I am using the color threshold method and this macro:

run("Invert");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-61/1.51n
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=113;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=242;
filter[1]="pass";
min[2]=15;
max[2]=180;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Colour Thresholding-------------

run("Color Threshold...");
run("Create Selection");
roiManager("Add");
roiManager("Split");

roiManager("Measure");

Now, more just by chance, I discovered that every time I run this macro I get a different result. It’s not repeatable and I have different values or number of results every time, even if I close everything and start again. How is that possible if I’ve set the parameters, they should be working the same every time or am I missing something here?

Thanks so much in advance!

Ado


#2

@ado

Welcome to ImageJ and the Forum!

Would you mind attaching an image that you use to run this macro? That way - we can try to reproduce your results exactly as you are doing…

eta


#3

Why do you invert the image?
Why do you run the Colour Threshold macro 3 times in your macro?


#4

It seems to work better to pick out the JF with the threshold if the image is inverted. I just used the autogenerated macro, but even if I cut out that (maybe) useless “run: color threshold” at the bottom I still receive a different result every time I run the macro.


#5

Please do not take this the wrong way, but your code is not correct.
If you generate the macro code with the “Macro” button, (between the two commented lines) then that is the only part you need. The other “invert” and the two calls to the Colour Threshold should not be there. I am not sure why you are also inverting the image before thresolding.


#6

Gabriel, thank you so much for tip! I cut out the rest and now it works fine and gives me the same results every time I run the macro.

ado