Automating drawing + measuring lines based on colour change




I have been having a quick look around and not having much luck so thought i would try and get some advice here. What I want to do is to write a script that will allow image J to draw a vertical line every x amounts of pixels (between a start and end point i would enter), and then provide the measurement.

However, the issue i am having is it seems that alot of the tools available require knowing the co-ordinates you want to go to. Instead i would like the line to be drawn from a designated position (known x&y location) vertically down until it reaches a colour change. I have attached an image (hopefully) that better shows this.

Is this actually possible/ feasible?

I know this can be done manually using ROI manager and measure macro, however, I have a fair few samples to analyse, and automating it would allow for more lines per sample and hopefully better statistics



Good day,

here is a result obtained with a commercial plugin that does horizontal contour tracing (here with cliff-jump behavior):


The contours are drawn as overlays and their distance is easy to evaluate. For the provided sample image each line consists of 375 points. Consequently you get 375 height measures. Here is a results table that gives you the first 22 values at the corresponding x-positions of your sample image:


The contour tracing algorithm is described in this thread:

Mark describes a method and gives a reference:

Good luck



Hi Herbie

That looks like it would be ideal for the kind of thing i am after.
However, i think i will try some other solutions before resorting to buying a product, due to issues with publications etc.

Thanks for giving me the link to the other threads, the info seems very useful. I followed Mark’s method until the last step (e), and didn’t grasp how the blue line appeared.

Thanks for your help, it is much appreciated


Good day Alex!

[…] before resorting to buying a product, due to issues with publications etc.

Are publications of results that were obtained with commercial software a problem?
This would be new to me. Think e.g. of OSs, Office-applications etc.

Here is a free ImageJ-macro that does what you want but you need to cite that you’ve received it from here:

// imagej-macro "contourDistance" (Herbie G., 17. Oct. 2018)
requires( "1.52g" );
x = 90;
getBoundingRect(x, y, w, h);
setOption("BlackBackground", true);
setForegroundColor(0, 0, 0);
run("Duplicate...", "title=Excerpt.tif");
run("RGB to CMYK");
run("Stack to Images");
setAutoThreshold("Default dark no-reset");
run("Convert to Mask");
run("Analyze Particles...", "size=0-500 clear add");
close("ROI Manager");
run("Fill Holes");
setAutoThreshold("Default dark no-reset");
run("Convert to Mask");
run("Fill Holes");
run("Create Selection");
run("Make Inverse");
run("Restore Selection");
setForegroundColor(255, 255, 255);
run("Fill", "slice");
run("Select None");
run("Clear Results"); 
for ( i=0; i<w; i++ ) { 
   setResult("X", i, x+i);
   setResult("vertDistance", i, distance());
run("Select None");
run("Remove Overlay");
function distance() {
   p = getProfile();
   cnt = 0;
   j = 0;
   while (j<p.length) {
      if (p[j]>0) cnt++; else {if (cnt>0)  j = p.length;}
   return cnt;
// imagej-macro "contourDistance" (Herbie G., 17. Oct. 2018)

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it with your image open.

The macro shows again cliff-jump behavior.

The first two lines of the macro code define a rectangle selection that shows up in your image after you’ve run the macro. This rectangle selection was especially made for the provided sample image. You can comment out these two lines like this

//x = 90;

and drwa a rectangular selection yourself with the rectangle tool. After you’ve done this, you can run the macro. This approach allows you to always select the relevant part according to the individual image.

HTH and don’t forget to cite correctly (for details click below)



Hi Herbie

Sorry that may have been my misunderstanding. My understanding using commercial documentation is that it depends on the level of supporting documentation to go with it. If I do end up using your macro for data that then goes onto be published, you will definitely be cited for the code etc.

Thanks for all your help, it is much appreciated