How to automatically crop an image with drum distortion using ImageMagick?

Using ImageMagick convert

to distort the image to correct the highly visible pad distortion, I provide a positive a, b, or c (from the database for my lens + focal length). This corrects the image, has the original width and height, but includes a non-rectangular, curved / distorted border as the image is corrected to its center. Simplified example:

convert rose: -virtual-pixel black -distort Barrel '+0.0 +0.1 +0.0' out.png

      

How can I automatically crop the black, folded border to the largest possible rectangle in the original aspect ratio in rose?

enter image description here

ImageMagick's website states that it automatically calculates a "d" parameter that can do this (resulting in the linear distortion effectively scales in the image and pushes the curved border right out of the image), but the calculated value of imagemagick seems to be targeting something else (v6.6.9 on ubuntu 12.04). If I guess and manually specify "d", I can get the expected output:

convert rose: -virtual-pixel black -distort Barrel '+0.0 +0.1 +0.0 +0.6' out.png

      

The given form a + b + c + d = 1 does not seem to be the proper d for my cropping case. Also, d seems to depend on the aspect ratio of the image, not just a / b / c. How do I get ImageMagick to crop the image, or how do I calculate the correct d?

Update

I found a Fred ImageMagick script innercrop

( http://www.fmwconcepts.com/imagemagick/innercrop/index.php ) that does a bit of what I need but has drawbacks and is not a solution for me. It accepts arbitrary outer regions, so you need to find the clipping rectangle. It doesn't work on Unix pipes and doesn't keep the original aspect ratio.

Update 2

Contemplation of the problem makes me think that calculating "d" is not a solution, as changing d introduces more or less bending and seems to do more than just increase. D = 1- (a + b + c), which is computed with imagemagick, results in a curved image touching the top / bottom boundaries (for landscape images) or left / right boundaries (for portrait images). So I believe the correct solution is to calculate where one of the new 4 corners will be assigned to a / b / c / d and then trim those new corners.

+3


source to share


1 answer


Way I understand the docs, you don't use commas to separate the parameters for the barrel distortion operator.

Here's a sample image, along with the output of the two commands you gave:

convert o.png -virtual-pixel black -distort Barrel '+0.0 +0.1 +0.0' out0.png
convert o.png -virtual-pixel black -distort Barrel '+0.0 +0.1 +0.0 +0.6' out1.png

      

dY6X1.pngkTjgc.pngHwfS5.png

I've created a sample image to better visualize what you might want to achieve.

However, I do not see what you stated about the automatically computed parameter 'd', and I do not see the effect you have stated regarding the use of 'd = + 0.6' ...

I'm not sure if I understood your desired output correctly, so I am assuming you want the area marked with the yellow rectangle to be clipped.

The image on the left out0.png

was created by the first command above. To guess the required coordinates, you first need to determine the dimensions of the image:

identify out0.png
 out0.png PNG 700x700 700x700+0+0 8-bit sRGB 36KB 0.000u 0:00.000

      

The image in the center is highlighted with a white rectangle. There is a rectangle so you can look at it and tell me that this is the region you want to crop. The image on the right is a cropped image (without scaling it to its original size).



kTjgc.pngVFInQ.pngr8gEd.png

Is this what you want? If so, I can update the answer to automatically detect the required trim coordinates. (At the moment, I did it based on guesswork.)


Update

I think you may have misunderstood the purpose of the barrel distortion operation. It is designed to correct barrel (light) distortion created by camera lenses. Your photographic EXIF ​​data can specify 3 parameters a

, b

and c

, which will be used for any particular combination of camera, lens and current magnification. The formula was a+b+c+d = 1

intended to be used when the new distortion-corrected image needs to be the same dimensions as the original (distorted) image.

So, to mimic the trunk corrections, we should probably use the second image from the last line above as our input:

convert out3.png -virtual-pixel gray -distort barrel '0 -0.2 0' corrected.png

      

Result:

VFInQ.pngX6phN.png

0


source







All Articles