Skip to content Skip to sidebar Skip to footer

Image Straightening In Android

I am working on a project where I need to implement Image Straightening. I have got an idea to do this. I am rotating the Image on SeekBar as -10 to +10 degrees. It is working by t

Solution 1:

In the diagram below the green rectangle is the valid part of the rotated image. What we need to determine is the scaling factor which will make the green region the same size as the original image. We can see from the figure that this scaling factor is the ratio of len2 to len1.

enter image description here

Using the diagram and some basic trigonometry we can find len1 and len2. The following c-like pseudo code describes the solution.

// theta  : the angle of rotation of the image// width  : the width (number of columns) of the image// height : the height (number of rows) of the image

a = atan(height/width);

// the length from the center to the corner of green region
len1 = (width/2)/cos(a-abs(theta));
// the length from the center to the corner of original image
len2 = sqrt(pow(width/2,2) + pow(height/2,2));
// compute the scaling factor
scale = len2 / len1;

That's it. Assuming all the transformations are done with regard to the center of the image then simply scale the image by the value of scale after performing the rotation.

Note: the equations provided assume height > width. Otherwise replace width with height in the len1 equation.

Update: Amulya Khare has posted an example implementation here

Solution 2:

Based on the solution from jodag, here a method to compute the straightening for iOS / OS X:

CG_INLINECGAffineTransformCGAffineTransformMakeStraightening(CGSize size, CGFloat rotation)
{
    CGAffineTransform transform = CGAffineTransformIdentity;

    // Apply the rotation
    transform = CGAffineTransformRotate(transform, rotation);

    // theta  : the angle of rotation of the image// minSide: the min side of the sizeCGFloat a = atan(size.height/size.width);        
    CGFloat minSide = MIN(size.width, size.height);

    // the length from the center to the corner of the greenCGFloat len1 = (minSide/2)/cos(a-fabs(rotation));

    // the length from the center to the corner of the blackCGFloat len2 = sqrt(pow(size.width/2, 2) + pow(size.height/2, 2));

    // compute the scaling factorCGFloat scale = len2 / len1;

    // Apply the scale
    transform = CGAffineTransformScale(transform, scale, scale);

    return transform;
}

Post a Comment for "Image Straightening In Android"