Android中的图像校正
我正在从事一个项目,我需要实施图像校正。我有个主意要做这件事。我将SeekBar上的图像旋转为-10到+10度。当我旋转时,它在白色背景下工作。所以,我们还需要实现缩放功能,使其看起来像下图所示的图像拉直。 请提出建议 示例代码Android中的图像校正,android,image,image-processing,Android,Image,Image Processing,我正在从事一个项目,我需要实施图像校正。我有个主意要做这件事。我将SeekBar上的图像旋转为-10到+10度。当我旋转时,它在白色背景下工作。所以,我们还需要实现缩放功能,使其看起来像下图所示的图像拉直。 请提出建议 示例代码 float a = (float) Math.atan(bmpHeight/bmpWidth); // the length from the center to the corner of the green float len1 = (float) ((bmpW
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);
在下图中,绿色矩形是旋转图像的有效部分。我们需要确定的是使绿色区域与原始图像大小相同的比例因子。从图中可以看出,该比例因子是
len2
与len1
的比率
使用该图和一些基本的三角函数,我们可以找到len1
和len2
。以下类似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
的值缩放图像即可
注:提供的方程式假定高度>宽度。否则,在len1
公式中,将width
替换为height
更新:Amulya Khare根据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;
}
感谢您的宝贵努力和回复。当我试图用这个比例创建位图时,应用程序崩溃了。我不太熟悉Android编程,你用什么软件包来执行图像旋转/转换?当我说使用矩阵变换进行比例时,可能我的想法太低了,如果您使用的工具已经具有缩放功能,则需要放大,以便将像素放大
比例
确定。我已将您的算法转换为android编程。但我仍然不能得到正确的结果。图像正在变得扭曲和完全改变。没有得到正确的图像。@SoH您需要将度数转换为弧度θ=(浮点)数学。toRadians(θ);我有同样的要求。我尝试了很多,但没有成功。你会提供你的源代码吗?这是非常需要的,请帮忙。。。