Crop image between two lines

Hi everyone,

I have a series of images like this one:

and I need to crop them, so I can after analyse the content of the drops.
Is there a way to find the position of the two horizontal lines?

This should be approximately the result of the crop:

Thanks for the help!

Good day Gabriele,

finding the dark lines, i.e. the y-coordinates of the desired ROI is easy:

Do a horizontal projection and in the resulting one-dimensional function search for the two lowest local minima.

For the x-coordinates you leave us alone with a suitable criterion.

Best

Herbie

2 Likes

Thanks you very much for the answer!
I get the idea, now I’m struggling to implement it.

I’ve applied a threshold, to reduce the noise:


so the vertical profile plot is something like this:

I still can’t find the way to find the coordinates of the two maxima values. Do I have to use a plugin?

Thank you again

Good day Gabriele,

evidently, you didn’t get the idea …

No need to threshold the image and why do you use the vertikal profile when I suggested to project horizontally?

Here is a little macro that does what you wish. Open a new macro text window via

Plugins > New > Macro

and paste the following code to this window. Then run the code by typing

Cmd-R or Ctrl-R

(The macro assumes that the image has been opened im ImageJ.)

//macro begin////////////////////////////////////////
setBatchMode( true );

run( “Select All” );
setKeyDown( “alt” );
projection = getProfile();
min = Array.findMinima( projection, 0 );
setKeyDown( “none” );
if ( min[0] < min[1] ) { y = min[0]; } else { y = min[1]; }
makeRectangle( 0, y, getWidth(), abs( min[1] - min[0] ) );
run( “Crop” );
run( “Select None” );

setBatchMode( false );
exit();
//macro end/////////////////////////////////////////

If you need to crop the image also with respect to the x-coordinate, you need to specify how.

HTH

Herbie

2 Likes

Hi Herbie,

I’m sorry, I am new to the subject and I thought I could use the vertical profile in the same way.

The script worked perfectly on the given image, thank you very much.

For the x-coordinates I have a brightest spot at a certain point of the tube, so I’ve used the same script to find it and draw the rectangle around the area.
The images have more or less the same pattern, so it was fine to just set manually the width of the rectangle. I’ll post the macro as it might be useful to someone else:

setKeyDown( “none” );
projection = getProfile();
max = Array.findMaxima( projection, 10 );
x = max[0];
makeRectangle( x-500, 0,getHeight(),getHeight() );
run( “Crop” );

Thank you again for your help!

Gabriele