Threshold Color Peak as a variable



Hello all. Is that possible for an ImageJ macro to get the peak value in the threshold color as a variable? I’m developing a macro that measures the area of pink in a picture and somehow, some color adjustment bar need to be set just near the right side of the peak (I will manually set how much). Thank you~


Hi @Fijipuff,

honestly your question is a little bit cryptic.
Could you potentially post an example image which indicated what you try to do and which value you try to extract.

  • what exactly you refer to when you write “threshold color”. Is it a manual color threshold set on an RGB image?
  • what do you refer to as peak value? should it be the mode value (most appearing value in the histogram)?

Because I am pretty sure you can get the value you need once we understand your objective a little clearer


Sorry for the confusion. Here’s an example:
Most of the pictures I’m working with look like this:

My goal is to measure the areas of the 4 triangular areas in the inner part and the light pink area in the outer part. I usually measure the inner part first, then the total area and let excel do the subtraction.

I use the Threshold Color window (settings are included in the pic) to manually adjust the area I can select:


  1. Fix the bars that have blue arrows on the side to their current value shown in the picture. I tried recording macro, but it failed–the macro I recorded somehow made everything black and white.
  2. For the 2 bars without blue arrows, move them slightly to the right side of the peaks in the red circles respectively. Since different pictures have different peaks, my thought is to assign the peak value to a variable, and then I will input a number to adjust how “slightly” it is to the right. And that is how I came to this question.

Hope these clarify what I want to do. My code so far can only do the Gaussian blur and open the Threshold Color window for me.


Good day,

I highly recommend to convert your images from RGB to CMYK (there is an excellent plugin for ImageJ that may even be part of the Fiji-distribution). Then do

“Image >> Stacks >> Stack to images”

and use the Magenta (M) slice for your analyses. (No trouble with “Threshold Color” …)

BTW, the idea of thresholding is to get binary images.




Hey Herbie,

Thank you for your reply! Your method is pretty mind refreshing. I followed your steps and I surely got the Magenta slice of the picture, which has a more distinct pink area now. The only problem is that I have not used CMYK to do measurement before and after I converted the picture from RGB to CMYK, ImageJ forbids me to use Threshold Color to do the area selection. I also searched online and didn’t get anything valuable so far. Could you be a bit more specific about how to do the measurement?

Also, one of my concern is that since in the Magenta slice, everything is almost the same color now, can ImageJ still measure the outer part and the inner part separately? I need to measure the area of them respectively.


Dear @Fijipuff,

I second @Herbie that the CMYK conversion is a good idea towards the extraction. The result of this conversion is not a color image anymore. If you look at the top of the image it actually will state 32-bit, refering to a grayscale image holding only intensity values (not color). In such cases you will need to apply a “normal” intensity threshold. I would suggest, that you first get familiar with the topics on image segmentation and thresholding. There are numerous great resources on the ImageJ page. Here an excerpt:

Thus, you could try starting e.g. from the magenta channel of the CMYK color space or similarly convert your image into LAB and look into the L* and the a* channel and try the thresholding there.

Alternatively, for the beginning it might be easier to go via the Trainable WEKA Segmentation. See the following test segmentation on the image you provided:

The corresponding colored areas you can extract for example by clicking on “Get Result” and set a manual threshold to the respective index of the colored output (you see this by hovering your mouse over the color in the output image and checking the ImageJ main window. This will display the index of the respective color).
The remaining small non-specific areas could for example be eliminated by size and shape exclusion using the ►Analyze ►Analyze Particles… function in ImageJ.
Alternatively, you can then threshold the outer part (in the upper image shown in green) and take this as sample area to calculate e.g. the %Area of the reddish staining (if this is what you need as an output.

Those steps can also be recorded with the macro recorder (►Plugins ►Macros ►Record…)

Hope this helps you for getting started


Good day,

most of what had to be replied has been stated by Jan. I should like to add that it is obvious from your questions that you don’t really understand what you are doing and this means that you have to learn more about ImageJ. The “ImageJ User Guide” is a good start for your studies:

Of course the result from color separation is a monochrome image and you can apply all methods of shape analysis that are applicable to gray-valued images.

What I don’t really support is to use classifiers (such as WEKA) for such basic tasks.




Hi Herbie,

I talked with my mentor about the CMYK method, but he said I might think too much on this issue. So he suggested me to stick on the “threshold color” panel and adjust the setting there. Thus, I still want to ask whether it’s possible to fix those adjusting bars on that panel after I open it with a macro?

Thank you very much!


Good day,

best greetings to your mentor and I would really like to hear his arguments.