Fix distortion on top part of an image

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?".
Post Reply
icarusin
Posts: 5
Joined: 2014-09-25T00:49:12-07:00
Authentication code: 6789

Fix distortion on top part of an image

Post by icarusin »

Hi,

I am trying to remove distortion from part of an image taken from an iPhone camera.

http://postimg.org/image/cqvtbjnkp

There can be text on the images as well, distorted the way the lines are. I am trying to straighten the top part of the image without affecting the bottom part.

I have tried the following:

- 'distort SRT' // This tilted the image upward or downward by an angle.

and

- ''distort Barrel' // This modified the curves around the image but did not help much.

The main issue for me is that only some part of the image has the distortion (usually top part), while the commands above work on the whole image.

Is there a way to specify removal of distortion to be limited to only a certain part of the image in a generic manner so that it can be applied to a batch of images?
The images are taken by holding camera in one hand and pointing it at image held in other hand. So distortions would always be at the top right side of the image.

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

Re: Fix distortion on top part of an image

Post by snibgo »

What distortion? This image shows two kinds: heavy perspective distortion, and very slight pincushion distortion.

The perspective distortion will depend on how you photograph (or hold) the object, of course, so will be different for every image. Pincushion distortion will be the same for every image (unless the camera can change focus).
snibgo's IM pages: im.snibgo.com
icarusin
Posts: 5
Joined: 2014-09-25T00:49:12-07:00
Authentication code: 6789

Re: Fix distortion on top part of an image

Post by icarusin »

Hi snibgo,

I have not applied any distortion. The example shown is the original image which will have the curved lines shown in the example. I want to cancel these distortions using distort with negative coordinates so that the final image appears with straight lines (and straight text).

As per your comments, I should look to cancel the heavy perspective distortion and the slight pincushioin distortion. Is there a way to cancel distortions only on a part of an image instead of entire image ? I looked at Fred's Scripts for Unperspective () but it mentions
This technique is limited and relies upon the ability to isolate the outline or boundary of the distorted quadrilateral in the input image from its surrounding background
The images can have different width and no exact coordinates for the distortion, I cannot hardcode a boundary. Any suggestions on a generic solution ?

Thanks,
Arun
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Fix distortion on top part of an image

Post by Bonzo »

I mentioned on similar thread using javascript to pick points on the image and feed those points into Imagemagick.
icarusin
Posts: 5
Joined: 2014-09-25T00:49:12-07:00
Authentication code: 6789

Re: Fix distortion on top part of an image

Post by icarusin »

Hi Bonzo,

This is a backend operation and using Javascript is not an option. Do you have a link to the post for the logic you used ?

Thanks,
Arun
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Fix distortion on top part of an image

Post by Bonzo »

Hello Arun

I was passing the information to the imagemagick distort functions using JavaScript and php and was a simpler process than you need.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fix distortion on top part of an image

Post by snibgo »

To correct the distortion on this image probably needs (a) de-pincushion, which you would do with "-distort barrel" and (b) de-perspective, which you would do with "-distort perspective".

You would need to find the barrel parameters A,B,C and D. I suppose this is a common camera, so someone has probably already done this.

Correcting a perspective distortion (ie making the lines more parallel) might be automated with a Hough line detector. See viewtopic.php?f=4&t=25476 and http://www.imagemagick.org/Usage/transform/#hough . This would give coordinates that could be given to "-distort perspective", but I haven't worked out the details.
snibgo's IM pages: im.snibgo.com
icarusin
Posts: 5
Joined: 2014-09-25T00:49:12-07:00
Authentication code: 6789

Re: Fix distortion on top part of an image

Post by icarusin »

Hi Snibgo,

Thanks for the suggestions. I will try it out and update here. The generic problem is how to remove non linear geometric distortion and also do a text line correction. To add more details am including couple of white papers that discuss the same.

http://tkuir.lib.tku.edu.tw:8080/dspace ... ection.pdf
http://www.cfar.umd.edu/~daniel/daniel_ ... tif-06.pdf

Thanks,
Arun
icarusin
Posts: 5
Joined: 2014-09-25T00:49:12-07:00
Authentication code: 6789

Re: Fix distortion on top part of an image

Post by icarusin »

Hi Snibgo,

I have had some success with the houghlines. Thanks for mentioning that. I am using the coordinates from the mvg file generated by houghlines to split the image such that I can then distort each one separately. I feel that I may have better results if the white space in the image were used as demarcation.

Is there something similar to houghlines for white spaces in a page ? That way the strength of the lines (thickness) would be proportional to the thickness of white space and I would use that to split the text.

Thanks,
Arun
Post Reply