How to count automatically the number of capacitors on this image?



I’m a newbie with ImageJ but I would like to know if it is possible to count the number of capacitors (small brown rectangles) on this electronic board using ImageJ ?
How to do it ?
Thank you very much :slight_smile:


Hey @Wilw15

So I just gave a try using the Fiji plugin Trainable Weka Segmentation (TWS). This is the result I got just quickly testing and using the default parameters:

This tool is macro-compatible… so will help to apply the trained classifier to multiple images. :slight_smile: TWS gives you a probability map that you can use to auto-threshold and then analyze particles… to count them, get their sizes, etc.

You can also try using the built-in Color Threshold tool - which is also macro-recordable.

Just play around a bit and if you have more specific questions - feel free to ask. We are here to help.

eta :slight_smile:

Too - since you said you are new to using ImageJ, here are some links you might find helpful:

For Scripting - ie. writing macro code - here are some other helpful links:


Hi Etarena,
Thank you very much for your answer but even with the explaination page of the TWS, I don’t succeed to count one type of component. I don’t understand how to use it.

Do you know if it could be possible to train FIJI/TWS to count each type of component, for example:

  • the small resistors
  • the big resistors
  • the small capacitors
  • the big capacitors
  • the transistors (3 pins)
  • the integrated circuits

Thank you,



That should work in training different objects - yes. To get a start with TWS - go through the Segmentation Workshop linked above… there is a step-by-step example presented that you should find helpful for getting started with this plugin.

eta :slight_smile:


Hi Wil,

Please find below a small macro that uses the Lab color space to find the brown/yellow pixels.
This will work only for your first question, not for detecting the different component .

For this second part, you could have a look to Feature Extraction by using small pictures of each element you want to find (I didn’t check if it works on RGB images)

setOption("BlackBackground", true);

title = getTitle();
// conver to Lab color space
run("Duplicate...", "title=Lab duplicate");
run("Lab Stack");
// using b component
run("Duplicate...", "title=b");
// conversion to 16-bit because auto threshold behave a bit weirdly with 32-bit
setAutoThreshold("Yen dark");
run("Convert to Mask");
// clear dust
run("Median...", "radius=5");
// find object, adapt size if needed
run("Analyze Particles...", "size=100-Infinity summarize add");

roiManager("Show All with labels");