Square inner crop

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
voo
Posts: 2
Joined: 2015-03-31T03:09:02-07:00
Authentication code: 6789

Square inner crop

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Square inner crop

Post 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.
snibgo's IM pages: im.snibgo.com
voo
Posts: 2
Joined: 2015-03-31T03:09:02-07:00
Authentication code: 6789

Re: Square inner crop

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Square inner crop

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Square inner crop

Post 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.
Post Reply