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:
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
Square inner crop
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Square inner crop
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.
snibgo's IM pages: im.snibgo.com
Re: Square inner crop
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Square inner crop
From your image:
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.
Code: Select all
convert sqCirc.png -format %@ info:
384x384+115+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.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Square inner crop
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.