extracting biggest square from a rotated image

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
manit
Posts: 76
Joined: 2009-01-30T22:31:26-07:00

extracting biggest square from a rotated image

Post by manit » 2018-08-12T06:43:26-07:00

hi ,
Suppose I have a black canvas of 512x512 size
convert -size 512x512 canvas:black black.png
Image
Now I rotated it to get another images
convert black.png -rotate 30 black-rotated-30degree.png
Image
Now my aim is to cut out biggest black square from this rotated image .
How can I do that ?

It tried to take image center and cut square around it but am not sure what should be the size of biggest black square or whether it is correct way ?
Image

Images are not being shown in the post
Please see .
http://ibb.co/cLsbo9
http://ibb.co/nmkJ1U
http://ibb.co/b2tMMU

snibgo
Posts: 10485
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: extracting biggest square from a rotated image

Post by snibgo » 2018-08-12T07:42:03-07:00

manit wrote:...but am not sure what should be the size of biggest black square...
My high-school geometry says that:

W' = W / (sin(A) + cos(A))

where W is the width and height of the unrotated square, W' is the width and height of the new square contained inside, and A is the angle of rotation.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 604
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: extracting biggest square from a rotated image

Post by GeeMack » 2018-08-12T08:23:51-07:00

manit wrote:
2018-08-12T06:43:26-07:00
Now my aim is to cut out biggest black square from this rotated image . How can I do that ?
Using IM 6.8.9 from a bash shell I can cut out the largest square from a larger rotated square with a command like this...

Code: Select all

squ=853
deg=33

convert -size ${squ}x${squ} xc:green -rotate ${deg} \
   -set option:leg "%[fx:${squ}/(cos(${deg}*(pi/180))+sin(${deg}*(pi/180)))]" \
   -set option:distort:viewport "%[leg]x%[leg]" \
   \( -clone 0 -distort srt 0 \) -gravity center +swap -composite output.png
Set the variables "squ" and "deg" to the width of the original square and the number of degrees of rotation. The output image should be the largest possible square without going outside the bounds of the rotated input image.

User avatar
fmw42
Posts: 23600
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: extracting biggest square from a rotated image

Post by fmw42 » 2018-08-12T10:27:12-07:00

I have a bash unix Imagemagick script called autotrim that will do an inner trim. See my link below. No guarantees that it is optimal.

I ran it on your image to do the crop and to show where the crop occurred.

Image

Code: Select all

autotrim -m inner -f 1  -C white -p save black_rotated_30.png black_rotated_30_innercrop.png
Crop Box: 374x374+164+164

Image

Image


See also

https://stackoverflow.com/questions/167 ... 7#16778797


There is also a magick.NET version of this for Windows users. See https://github.com/dlemstra/FredsImageMagickScripts.NET

Post Reply