Straighten image and crop within predefined rectangle

Hello all,

I would like to do particle count analysis and would like to crop to a rectangle within the original image.

I found a discussion titled “Crop image between two lines” which has helped me somewhat. In this case the image was only cropped vertically. I would like to crop in the horizontal direction as well. I’m new to imageJ and don’t quite understand the coding enough to pull this off. In addition, many of my images will require some straightening before the crop. (A simple rotation would be sufficient, but I need to automate this).

Any help would be appreciated!

Hi Tom,

Welcome to the forums!

This is definitely a good task to begin on. Use the links below to educate yourself a little bit on ImageJ macro programming and I’m sure you can tackle this problem with ease. It also requires knowing what to do to process the images as well, but you have a really good example here.

  • Introduction to Macro Programming in ImageJ - You can use the macro recorder (Plugins > Macros > Record…) to find out how to code the actions that you are doing in ImageJ, then you can write a simple script.
  • Built-in Macro Functions - All of the pieces of code for other useful functions in ImageJ that you might not be able to find from the macro recorder.

With regards to your problem, as the rectangular border and particles are very well contrasted to the rest of the image, we can ‘threshold’ the image to leave us with just the black objects. To do this, I converted the image to 8-bit (from RGB; Image > Type > 8-bit). Then, I used the default threshold (Image > Adjust > Threshold…) to make the image black and white (binary) where the objects are black on a white background.

Next, we can use this binary image to obtain the rectangular border. Using the analyze particles plugin (Analyze > Analyze Particles…) with default settings (see below) we can obtain all of the objects as discrete selections, which are sent to the ROI manager.

The rectangular border is one giant selection, much larger than the rest. This can be identified in a macro, and maybe I’ll leave you to figure out how you might code this. Once the rectangular border ROI is identified, we can select it and clear the outside of the border from the image to remove unwanted particles from detection (Edit > Clear Outside).

Now we are left with just the particles inside or on the border, using the analyze particles plugin again we can obtain all of the particles as ROIs and measure their shape/area or just count them.

Some things to note:

  • The particles on the border will not be counted this way
  • Your images do not look uniformly lit, so you may find that the threshold sometimes doesn’t work as intended. This can be tweaked to best fit all images, but may need adjusting for multiple different images.
  • You have some objects that are very close, this would require some advanced segmentation to separate them - you should research watershed segmentation

Automation by scripting this process should be very easy, have a go using the macro recorder and the links provided and come back to us with any problems.




Hi Rob,

Thank you very much for the detailed response! My issue is not so much the segmentation - I’ve experimented with multiple techniques and what you’ve demonstrated shows that I’m on the right track. The bigger challenge for me is the process for identifying the rectangular border ROI in a way that can be automated. Each of my stacks is likely to have a slight variation in the shape/size of the rectangular ROI.

Hi Tom,

As long as the rectangular border is always continuous and is the largest object in the image, it will have the largest size of any particle found using the Analyze Particles plugin. Therefore, you could adjust the Analyze Particles settings to only capture the largest ROI (i.e. set the size filter to be large so it doesn’t pick up your objects of interest, but just the rectangular border). Take that ROI and then Edit > Clear Outside and carry on with segmentation and measuring.

Do you have any examples of other images that are drastically different from the one you’ve shown? Are all of the borders roughly the same size/shape?


1 Like

Hi Rob,

Thank you - that’s brilliant! Much simpler & robust than what I had in mind. The borders will all be roughly rectangular - with slight variations in position/size due to set-up conditions. I think it should work!

Best regards!

1 Like