Android中的图像拉直

时间:2013-09-18 07:04:27

标签: android image image-processing

我正在开展一个需要实施Image Straightening的项目。我有一个想法这样做。我在SeekBar上旋转图像为-10到+10度。当我旋转时,白色背景是可见的。因此,我们还需要实现缩放功能,使其看起来像图像拉直,如下所示。 请咨询您的建议。

enter image description here

enter image description here

示例代码

float a = (float) Math.atan(bmpHeight/bmpWidth);
// the length from the center to the corner of the green
float len1 = (float) ((bmpWidth/2)/Math.cos(a-Math.abs(curRotate)));
// the length from the center to the corner of the black (^ = power)
float len2 = (float) Math.sqrt((bmpWidth/2)^2 + (bmpHeight/2)^2);
// compute the scaling factor
curScale = len2 / len1;
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmaprotate, 0, 0, bmpWidth, bmpHeight, matrix, true);
mainImage.setImageBitmap(resizedBitmap);

2 个答案:

答案 0 :(得分:11)

在下图中,绿色矩形是旋转图像的有效部分。我们需要确定的是缩放因子,它将使绿色区域与原始图像的大小相同。从图中我们可以看出,这个比例因子是len2len1的比率。

enter image description here

使用图表和一些基本的三角函数,我们可以找到len1len2。以下类似c的伪代码描述了解决方案。

// 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;

就是这样。假设所有变换都是在图像中心完成的,那么在执行旋转后,只需将图像按scale的值进行缩放。

注意:提供的等式假设为height > width。否则,将width替换为height等式中的len1

更新:Amulya Khare发布了example implementation here

答案 1 :(得分:2)

根据 jodag 的解决方案,这里有一个计算iOS / OS X拉直的方法:

CG_INLINE CGAffineTransform CGAffineTransformMakeStraightening(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 size

    CGFloat a = atan(size.height/size.width);        
    CGFloat minSide = MIN(size.width, size.height);

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

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

    // compute the scaling factor
    CGFloat scale = len2 / len1;

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

    return transform;
}