extracting biggest square from a rotated image
extracting biggest square from a rotated image
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
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: extracting biggest square from a rotated image
My high-school geometry says that:manit wrote:...but am not sure what should be the size of biggest black square...
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: extracting biggest square from a rotated image
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: extracting biggest square from a rotated image
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.
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
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
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
sorry , did not have internet access so could not reply earlier.
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.
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
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: extracting biggest square from a rotated image
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.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: extracting biggest square from a rotated image
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
EDITED: Fixed typo in command example.
Last edited by GeeMack on 2018-08-16T09:09:48-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: extracting biggest square from a rotated image
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.
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
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
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.
$ 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.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: extracting biggest square from a rotated image
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.manit wrote: ↑2018-08-16T08:31:18-07:00Here I have rotated by 15 degrees .ButCode: 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
I think 374 pixel square was expected .Code: Select all
$ file output.png output.png: PNG image data, 418 x 418, 8-bit grayscale, non-interlaced
What am I doing wrong?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: extracting biggest square from a rotated image
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
got it .GeeMack wrote: ↑2018-08-16T09:20:33-07:00Nothing 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.manit wrote: ↑2018-08-16T08:31:18-07:00Here I have rotated by 15 degrees .ButCode: 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
I think 374 pixel square was expected .Code: Select all
$ file output.png output.png: PNG image data, 418 x 418, 8-bit grayscale, non-interlaced
What am I doing wrong?
resolution is ok .
But , there was triangular cut in all four corners . see https://ibb.co/gkjVtK .
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: extracting biggest square from a rotated image
Looks like you rotated your image 45 degrees, but the calculation in your command is for a square rotated 15 degrees.manit wrote: ↑2018-08-17T06:08:09-07:00... there was triangular cut in all four corners . see https://ibb.co/gkjVtK
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: extracting biggest square from a rotated image
Please link to your input file, and show the command you used. Otherwise we can only guess.manit wrote:But , there was triangular cut in all four corners .
snibgo's IM pages: im.snibgo.com