Android中的图像拉直

San*_*ush 5 android image image-processing

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

在此输入图像描述

在此输入图像描述

示例代码

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);
Run Code Online (Sandbox Code Playgroud)

jod*_*dag 11

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

在此输入图像描述

使用图表和一些基本的三角函数,我们可以找到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;
Run Code Online (Sandbox Code Playgroud)

而已.假设所有变换都是关于图像的中心完成的,那么简单地按照scale执行旋转后的值来缩放图像.

注意:提供的等式假设height > width.否则可以更换widthheightlen1方程.

更新:Amulya Khare在此发布了一个示例实现

  • @SoH你需要将你的学位转换为弧度theta =(float)Math.toRadians(theta); (2认同)