Snapping/rotating the image vertically

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
mjamal
Posts: 56
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Snapping/rotating the image vertically

Post by mjamal » 2019-01-24T03:25:51-07:00

Hello Team,

Is there a way to snap/rotate the image vertically if it my image went out angled? Please see the attached image for more details.
Ie we need like, if I have an cropped image which is angled then system will automatically snap the image to make the longest line a vertical line, and snap the image accordingly from ImageMagick.

Image URL: https://www.dropbox.com/s/s2wa1wmm9u8he ... p.png?dl=0

I am using IM 7.0.8 version and OS is CentOS 7.6.
Please let me know if any one have any idea.

Thanks in advanced.

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

Re: Snapping/rotating the image vertically

Post by snibgo » 2019-01-24T06:26:21-07:00

This can be done with "-hough-lines", output to mvg. See viewtopic.php?f=4&t=25476 . From the coordinates of the endpoints of the longest line, a simple calculation gives the angle.

You need some pre-processing, and the image you supply isn't ideal for that. Do you have a version with a clearer foreground/background distinction?
snibgo's IM pages: im.snibgo.com

mjamal
Posts: 56
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: Snapping/rotating the image vertically

Post by mjamal » 2019-01-25T02:19:51-07:00

Please see the below url for the image which I need to snap/rotate vertically. Please let me know if that will be done from IM command?

https://www.dropbox.com/s/mc8axjfa865zj ... e.png?dl=0

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

Re: Snapping/rotating the image vertically

Post by snibgo » 2019-01-25T08:44:03-07:00

Code: Select all

magick angeledImage.png -alpha extract -threshold 50% -canny 0x1+10%+30% -hough-lines 9x9+400 mvg:-

# Hough line transform: 9x9+400
viewbox 0 0 983 2102
line 278.7,0 -16.7169,2102  # 521
The single line found is from (278.7,0) to (-16.7169,2102).

Trigonometry: angle = atan2 ( (278.7+16.7169), 2102) ) = 8 degrees.

Code: Select all

magick angeledImage.png -rotate -8 +repage out.png
snibgo's IM pages: im.snibgo.com

Post Reply