Distortion

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?".
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Distortion

Post by alireza »

So I am using this command to convert.exe" c:\kodak-cma.JPG -distort barrel "0.0301 -.0733 0" d:\corrected.jpg to create undistorted image.
and it works fine. except I don't want the cropping as it tries to keep the image resolution the same. Is there anyway to avoid that?
Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Distortion

Post by snibgo »

What cropping? The output should be the same size as the input, with no cropping.

Perhaps you mean you want to scale the result down, so the output image is contained with the same dimensions. If so, then supply a fourth number, eg 1.1.
snibgo's IM pages: im.snibgo.com
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

so we know that d=1-(a+b+c) will normalize based on smaller side of the image if I use 1.1 for d it will affect the distortion. Isn't that true?
I just want not to loose any data from my original image after removing distortion. your method works but it's affecting the solution.
in another word if I use d=1 and d=1.1 and overlay the result in photoshop and scale the images they should match but they are not.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Distortion

Post by snibgo »

alireza wrote:so we know that d=1-(a+b+c) will normalize based on smaller side of the image
Yes. More precisely, when d=1-(a+b+c) pixels at a radius from the centre r=1 won't move.

Changing any one of the four parameters a, b, c or d will change the function. For a pure scale, you can multiply all four parameters by the same factor.
snibgo's IM pages: im.snibgo.com
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

I appreciate it. You have been very helpful Sir.
Thank you.
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

one more thing.let's forget about scaling.
I have values for a kodak camera as a=0.0301 b=0.0733 c=0 and d=1-(a+b+c)
I ran the distorted image thru ptlens and convert.exe (imagemagik) and I am getting the same exact undistorted image result and as I expected the corner of image are pulling outward after removing distortion.

Now I tried to create a grid in excel and show this visually in excel
so I am using r'=a.r^4+b.r^3+c.r^2+(1-a-b-c).r
but instead of the corner image moving outward it's actually moving inward and when I change the sign of a,b,c seems like I am getting the correct result.
Can you think of a reason for that?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Distortion

Post by snibgo »

With those parameters in IM's convert, I would expect the middle of long side to remain in the same position, and the corners to move inwards.

The formula you show is correct, where r is the destination radius and r' is the source radius. Pixels that start at a given source radius will be distorted (moved) to the destination radius.

For example, my "toes" image is 267x233 pixels, so the diagonal is approx 354.37 and the ratio diagonal/short_dim is 1.52. The radius at the corners is 1.52.

Image

When the destination radius is 1.52, the formula tells us the source radius (where the pixel comes from).

0.0301*1.52^4 + 0.0733*1.52^3 + (1-0.0301-0.0733)*1.52 = 1.7809

So the source for each corner is further out than the corner. The corners will move inwards. IM will use virtual pixels for the new corners.

Code: Select all

%IM%convert toes.png -virtual-pixel Black -distort barrel 0.0301,0.0733,0 toes_barrel.jpg
Image

If you are getting the opposite result, please show your command, and the input and output images.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Distortion

Post by fmw42 »

except I don't want the cropping as it tries to keep the image resolution the same. Is there anyway to avoid that?
Use +distort rather than -distort.

see
http://www.imagemagick.org/Usage/distor ... rt_bestfit
http://www.imagemagick.org/Usage/distor ... t_viewport
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

OH. Thanks again I didn't realize r' is source and r is the destination I was using it backward.
Interesting that when I changed the signs of a,b,c I got a visually correct result.
I have to try to solve for r in excel and re-plot.
Thanks again you have been very helpful.
r'=a.r^4+b.r^3+c.r^2+(1-a-b-c).r
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Post by alireza »

Can excel solve the polynomial degree 4 equations?!
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

snibgo wrote:For a pure scale, you can multiply all four parameters by the same factor.
Is this really right?
let's say a=0.0301 b=-0.0733 and c=0 therefore d=1-(a+b+c)=1.0432
so I multiplied everything by 1.1
a=0.03311
b=-0.08063
c=0
d=1.14752

for a uniform scale I used this command:
convert source -distort barrel "0.03311 -0.08063 0 1.14752"
and overlayed this image with the image I got from convert source -distort barrel "0.0301 -0.0733 0" and scaled to see if they match and they are not matching.
How can Use +distort command?
any other method that after distortion removal process I still have all my pixels from original photo (no cropping).
Thanks,
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

fmw42 wrote:
except I don't want the cropping as it tries to keep the image resolution the same. Is there anyway to avoid that?
Use +distort rather than -distort.

see
http://www.imagemagick.org/Usage/distor ... rt_bestfit
http://www.imagemagick.org/Usage/distor ... t_viewport



when I use +distort barrel, there's still cropping. Is there any additional switches I should use to keep all the pixels in my original image without cropping?
Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Distortion

Post by snibgo »

No, it's an over-simplification to simply multiply ABCD by the same number.

As far as I know, "+distort" doesn't work with barrel. I suspect the reason is that the maths is very complex. I don't know the maths to do what is wanted.

But it can be done, with viewports. For example:

Code: Select all

%IM%convert toes.png -virtual-pixel Black -define distort:viewport=750x750-250-250 -distort barrel -0.0301,-0.0733,0 -quality 40 toes_debarrel2.jpg
Image
(Note that the output has the input more than once.)
snibgo's IM pages: im.snibgo.com
alireza
Posts: 14
Joined: 2016-10-07T09:14:21-07:00
Authentication code: 1151

Re: Distortion

Post by alireza »

a=0.0301 (positive not negative)
b=-0.0733 (negative)
c=0

can you apply these parameters to your toes.png and get an output without loosing pixels?
Thanks,
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Distortion

Post by snibgo »

Yes, like this:

Code: Select all

%IM%convert toes.png -virtual-pixel Black -define distort:viewport=800x800-300-300 -distort barrel 0.0301,-0.0733,0 -trim +repage p.png
snibgo's IM pages: im.snibgo.com
Post Reply