Page 1 of 2
extracting biggest square from a rotated image
Posted: 2018-08-12T06:43:26-07:00
by manit
hi ,
Suppose I have a black canvas of 512x512 size
convert -size 512x512 canvas:black black.png
Now I rotated it to get another images
convert black.png -rotate 30 black-rotated-30degree.png
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 ?
Images are not being shown in the post
Please see .
http://ibb.co/cLsbo9
http://ibb.co/nmkJ1U
http://ibb.co/b2tMMU
Re: extracting biggest square from a rotated image
Posted: 2018-08-12T07:42:03-07:00
by snibgo
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.
Re: extracting biggest square from a rotated image
Posted: 2018-08-12T08:23:51-07:00
by GeeMack
manit wrote: ↑2018-08-12T06:43:26-07:00Now 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.
Re: extracting biggest square from a rotated image
Posted: 2018-08-12T10:27:12-07:00
by fmw42
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.
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
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
Re: extracting biggest square from a rotated image
Posted: 2018-08-15T06:39:09-07:00
by manit
sorry , did not have internet access so could not reply earlier.
Code: Select all
I am using imagemagick on linux desktop
$ display --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-07-10 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
$ uname -a
Linux lxuser-OptiPlex-7040 4.15.0-24-generic #26~16.04.1-Ubuntu SMP Fri Jun 15 14:35:08 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
To fmw42 ,
512/(sin 30 + cos 30) = 374.810013475 (all angles in degrees)
So , your solution gives 374 pixel square , which looks most appropriate.
Can you point me to the linux version of this script (autotrim).
Also , my black square will be actually be a fingerprint with shades of grey in it.
Hope that does not create a problem.
To GeeMack ,
Let's say my 512x512 image is a fingerprint.
How can I pass that image as input to bash command you have mentioned ?
Thanks.
Re: extracting biggest square from a rotated image
Posted: 2018-08-15T08:29:52-07:00
by fmw42
My script is at my link in my signature below. Be sure to read the Pointers for Use on the home page. The script needs to threshold the image to get a good square or rectangle separated from the background in order to work. It does not work on arbitrary shapes.
Re: extracting biggest square from a rotated image
Posted: 2018-08-15T08:42:52-07:00
by GeeMack
manit wrote: ↑2018-08-15T06:39:09-07:00Let's say my 512x512 image is a fingerprint. How can I pass that image as input to bash command you have mentioned ?
As long as your input image is square, you can just put its name in the command where I used "-size ${squ}x${squ} xc:green" in my example, and where I used the variable "${squ}" to calculate the output dimensions, you should use "w" to represent the width of the input image. If you know for sure your input image is 512x512, you can just use "512" instead of "w" in the formula.
Code: Select all
image=input.png
deg=33
convert ${image} \
-set option:leg "%[fx:w/(cos(${deg}*(pi/180))+sin(${deg}*(pi/180)))]" \
-rotate ${deg} \
-set option:distort:viewport "%[leg]x%[leg]" \
\( -clone 0 -distort srt 0 \) -gravity center +swap -composite output.png
Learn more about using FX expressions in IM commands
at THIS link.
EDITED: Fixed typo in command example.
Re: extracting biggest square from a rotated image
Posted: 2018-08-15T08:48:28-07:00
by fmw42
GeeMack,
I think you are assuming he knows the rotation angle and is starting with a normally oriented image and rotating it. I am assuming that was only and example an that he has some image with an unknown rotation angle and wants basically an inner crop. I think the OP will need to clarify this situation.
Re: extracting biggest square from a rotated image
Posted: 2018-08-16T07:08:09-07:00
by manit
Actually , I know the dimension of original image and angle by which it has to be rotated .
Re: extracting biggest square from a rotated image
Posted: 2018-08-16T08:31:18-07:00
by manit
to GeeMack ,
$ file f0001_01.png
f0001_01.png: PNG image data, 512 x 512, 8-bit grayscale, non-interlaced
$ convert f0001_01.png -rotate 15 -set option:leg "%[fx:512/(cos(15*(pi/180))+sin(15*(pi/180)))]" -set option:distort:viewport "%[leg]x%[leg]" \( -clone 0 -distort srt 0 \) -gravity center +swap -composite output.png
Here I have rotated by 15 degrees .But
$ file output.png
output.png: PNG image data, 418 x 418, 8-bit grayscale, non-interlaced
I think 374 pixel square was expected .
What am I doing wrong?
To fmw42 ,
Can you suggest a simple command to run using your script that takes a 512x512 image and rotates it by d degrees then gives the biggest possible square image ?
Here you can assume that 512x512 is foreground and has nothing in background.
I tried ./autotrim -m inner -f 1 -C white -p save rotatedby30degree.png gogreat.png
and got
Crop Box: 374x337+164+183
Which has little white in lower left corner (see
https://ibb.co/eEZkse)
Here is the original image 512x512 which was rotated then used with autotrim -
https://ibb.co/g9wC5z
Thanks.
Re: extracting biggest square from a rotated image
Posted: 2018-08-16T09:20:33-07:00
by GeeMack
manit wrote: ↑2018-08-16T08:31:18-07:00
Code: Select all
$ file f0001_01.png
f0001_01.png: PNG image data, 512 x 512, 8-bit grayscale, non-interlaced
$ convert f0001_01.png -rotate 15 -set option:leg "%[fx:512/(cos(15*(pi/180))+sin(15*(pi/180)))]" -set option:distort:viewport "%[leg]x%[leg]" \( -clone 0 -distort srt 0 \) -gravity center +swap -composite output.png
Here I have rotated by 15 degrees .But
Code: Select all
$ file output.png
output.png: PNG image data, 418 x 418, 8-bit grayscale, non-interlaced
I think 374 pixel square was expected .
What am I doing wrong?
Nothing wrong. The 374x374 output dimensions came from the 512x512 square rotated 30 degrees. In your example above with a rotation of 15 degrees, the expected output square will be about 418x418.
Re: extracting biggest square from a rotated image
Posted: 2018-08-16T10:41:31-07:00
by fmw42
If you know the size and rotation angle, then use GeeMack's solution. My autotrim assumes that the whole rotated image is from which you want to get the inner bounding rectangle. You want the inner area of just your fingerprint after rotating. These are two different situations.
Re: extracting biggest square from a rotated image
Posted: 2018-08-17T06:08:09-07:00
by manit
GeeMack wrote: ↑2018-08-16T09:20:33-07:00
manit wrote: ↑2018-08-16T08:31:18-07:00
Code: Select all
$ file f0001_01.png
f0001_01.png: PNG image data, 512 x 512, 8-bit grayscale, non-interlaced
$ convert f0001_01.png -rotate 15 -set option:leg "%[fx:512/(cos(15*(pi/180))+sin(15*(pi/180)))]" -set option:distort:viewport "%[leg]x%[leg]" \( -clone 0 -distort srt 0 \) -gravity center +swap -composite output.png
Here I have rotated by 15 degrees .But
Code: Select all
$ file output.png
output.png: PNG image data, 418 x 418, 8-bit grayscale, non-interlaced
I think 374 pixel square was expected .
What am I doing wrong?
Nothing wrong. The 374x374 output dimensions came from the 512x512 square rotated 30 degrees. In your example above with a rotation of 15 degrees, the expected output square will be about 418x418.
got it .
resolution is ok .
But , there was triangular cut in all four corners . see
https://ibb.co/gkjVtK .
Re: extracting biggest square from a rotated image
Posted: 2018-08-17T11:46:29-07:00
by GeeMack
Looks like you rotated your image 45 degrees, but the calculation in your command is for a square rotated 15 degrees.
Re: extracting biggest square from a rotated image
Posted: 2018-08-17T12:30:06-07:00
by snibgo
manit wrote:But , there was triangular cut in all four corners .
Please link to your input file, and show the command you used. Otherwise we can only guess.