Page 1 of 1

Square inner crop

Posted: 2015-03-31T03:20:38-07:00
by voo
Hello,

This is my first post here so I'd like to say hi to everyone. I've been using imagemagick for some time now, however recently I've encountered problem I cannot cope with alone. That's why I've decided to turn to you for help. Consider the image below: Image
Given the image with black border and circular area of interest (not necessarily white), my objective is to extract the biggest square subimage of the this area as shown by the red square. I will be grateful for your help.

Cheers

Re: Square inner crop

Posted: 2015-03-31T03:39:07-07:00
by snibgo
If it is always a circle, this is easy. Find its width and height. This gives you the centre, and the radius. A simple formula gives you the length of each side of the square.

Re: Square inner crop

Posted: 2015-03-31T04:01:51-07:00
by voo
Thanks for the input. I agree it may seem relatively easy from the analytical point of view. However, is it possible to perform the actions you described in imagemagic? If so, please share your knowledge. I aim to process a lot of such images and ideally wish to derive an automatic method to crop the squares.

Re: Square inner crop

Posted: 2015-03-31T04:26:53-07:00
by snibgo
From your image:

Code: Select all

convert sqCirc.png -format %@ info:

384x384+115+7
The width and height of the circle is 384 pixels. It starts at x=115, y=7.

All the rest is scripting, and depends on what language you use.

So the radius is 384/2 = 192 pixels. So the centre is x = 115+192 = 307, y = 7+192 = 197.

So the length of each side of the square (by Pythagoras) is sqrt(2) * 192 = 271.5 pixels. Half the length is 543/2 = 135.8.

So the square top-left is x = 307-135.8 = 171.2, y = 197-135.8 = 61.2.

Now we know the top-left coordinate of the square, and its width and height. You can probably calculate the bottom-right coord, if you need it.

Re: Square inner crop

Posted: 2015-03-31T09:07:45-07:00
by fmw42
You do not say what platform or version of Imagemagick you are using. If on Linux/MacOSX or Windows with Cygwin, see my script innercrop at the link below for doing this with arbitrary shapes. Snibgo's solution is simple and clever for a circle and can probably be extended to a an ellipse oriented with major and minor axes along the x and y directions.