如何计算矩形到四边形的三维变换矩阵

时间:2012-08-02 14:52:25

标签: xcode image cocoa transform catransform3d

希望有人可以提供帮助,我正在努力研究如何将图像从矩形变换为四边形,每个角都有给定的x,y屏幕坐标。

到目前为止,我已将图像放在CALayer上,但需要计算出CATransform3D以将矩形扭曲为所需的四边形。我想要实现的一个例子是(从a到b)。

Example Rect to Quad image

如果我错了并且无法使用CATransform3D完成,还有其他方法可以通过示例来实现。

我认为KennyTM的答案接近我所需要的......

iPhone image stretching (skew)

我已经尝试过但没有太多运气,他确实提到“你可能需要转置”但如果是这样的话我不知道该怎么做。

3 个答案:

答案 0 :(得分:4)

CATransform 3D绝对可以做你想要用它的东西。我测试了code you linked to,它对我来说很有效。请记住,像这样的变换矩阵只能定义到一个比例,因为它是齐次坐标。一旦用他的方程式生成矩阵,则将每个元素除以右下角元素。我能想到你需要转置的唯一原因是因为他给出的变换是按行主顺序排列的。如果要填充列主变换矩阵(我相信CATransform3D是),则需要在填充后对其进行转置。

这是我用来测试它的代码,它使用了openCV中的矩阵类,并且是c ++但是应该证明这一点

cv::Matx41d rect_tl(-10,-10,0,1);
cv::Matx41d rect_tr(10,-10,0,1);
cv::Matx41d rect_bl(-10,10,0,1);
cv::Matx41d rect_br(10,10,0,1);

cv::Matx41d quad_tl(2,2,0,1);
cv::Matx41d quad_tr(4,6,0,1);
cv::Matx41d quad_bl(2,-1,0,1);
cv::Matx41d quad_br(3,5,0,1);


double X = rect_tl(0);
double Y = rect_tl(0);
double W = 20;
double H = 20;

double x1a = quad_tl(0);
double y1a = quad_tl(1);

double x2a = quad_tr(0);
double y2a = quad_tr(1);

double x3a = quad_bl(0);
double y3a = quad_bl(1);

double x4a = quad_br(0);
double y4a = quad_br(1);



double y21 = y2a - y1a,
y32 = y3a - y2a,
y43 = y4a - y3a,
y14 = y1a - y4a,
y31 = y3a - y1a,
y42 = y4a - y2a;

double a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42);
double b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);
double c = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42) - H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43) - W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);

double d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a);
double e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42);
double f = -(W*(x4a*(Y*y2a*y31 + H*y1a*y32) - x3a*(H + Y)*y1a*y42 + H*x2a*y1a*y43 + x2a*Y*(y1a - y3a)*y4a + x1a*Y*y3a*(-y2a + y4a)) - H*X*(x4a*y21*y3a - x2a*y1a*y43 + x3a*(y1a - y2a)*y4a + x1a*y2a*(-y3a + y4a)));

double g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43);
double h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42);
double i = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42) + H*(X*(-(x3a*y21) + x4a*y21 + x1a*y43 - x2a*y43) + W*(-(x3a*y2a) + x4a*y2a + x2a*y3a - x4a*y3a - x2a*y4a + x3a*y4a));

cv::Matx44d matrix(a,b,0,c
                   ,d,e,0,f
                   ,0,0,1,0
                   ,g,h,0,i);
matrix = matrix*(1/matrix(15));
//You may need a transpose here

cv::Matx41d test_tl = matrix*rect_tl;
test_tl *= (1/test_tl(3));
cv::Matx41d test_tr = matrix*rect_tr;
test_tr *= (1/test_tr(3));
cv::Matx41d test_bl = matrix*rect_bl;
test_bl *= (1/test_bl(3));
cv::Matx41d test_br = matrix*rect_br;
test_br *= (1/test_br(3));

执行后,底部的所有测试变量都完美地匹配了它们的四个对应物。希望能够解决问题。

答案 1 :(得分:4)

感谢Hammers的回答,我能够完成所有工作,需要进行转置,并找到了关于如何转置矩阵的精彩博客......

Transpose Matrix

我创建的工作方法如下......

- (CATransform3D)rectToQuad:(NSRect)rect quadTLX:(double)x1a quadTLY:(double)y1a quadTRX:(double)x2a quadTRY:(double)y2a quadBLX:(double)x3a quadBLY:(double)y3a quadBRX:(double)x4a quadBRY:(double)y4a
{
    double X = rect.origin.x;
    double Y = rect.origin.y;
    double W = rect.size.width;
    double H = rect.size.height;

    double y21 = y2a - y1a;
    double y32 = y3a - y2a;
    double y43 = y4a - y3a;
    double y14 = y1a - y4a;
    double y31 = y3a - y1a;
    double y42 = y4a - y2a;

    double a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42);
    double b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);
    double c = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42) - H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43) - W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);

    double d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a);
    double e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42);
    double f = -(W*(x4a*(Y*y2a*y31 + H*y1a*y32) - x3a*(H + Y)*y1a*y42 + H*x2a*y1a*y43 + x2a*Y*(y1a - y3a)*y4a + x1a*Y*y3a*(-y2a + y4a)) - H*X*(x4a*y21*y3a - x2a*y1a*y43 + x3a*(y1a - y2a)*y4a + x1a*y2a*(-y3a + y4a)));

    double g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43);
    double h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42);
    double i = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42) + H*(X*(-(x3a*y21) + x4a*y21 + x1a*y43 - x2a*y43) + W*(-(x3a*y2a) + x4a*y2a + x2a*y3a - x4a*y3a - x2a*y4a + x3a*y4a));

    //Transposed matrix
    CATransform3D transform;
    transform.m11 = a / i;
    transform.m12 = d / i;
    transform.m13 = 0;
    transform.m14 = g / i;
    transform.m21 = b / i;
    transform.m22 = e / i;
    transform.m23 = 0;
    transform.m24 = h / i;
    transform.m31 = 0;
    transform.m32 = 0;
    transform.m33 = 1;
    transform.m34 = 0;
    transform.m41 = c / i;
    transform.m42 = f / i;
    transform.m43 = 0;
    transform.m44 = i / i;
    return transform;
}

此方法的示例调用如下...

NSImage *image = // load a image

CALayer *layer = [CALayer layer];
[layer setContents:image];
[view setLayer:myLayer];
[view setFrame:NSMakeRect(0, 0, image.size.width, image.size.height)];

view.layer.transform = [self rectToQuad:view.frame quadTLX:0 quadTLY:0 quadTRX:image.size.width quadTRY:20 quadBLX:0 quadBLY:image.size.height quadBRX:image.size.width quadBRY:image.size.height + 90];

答案 2 :(得分:2)

感谢@ Equinox2000的帮助!

请注意,在iOS CALayer上,默认的anchorPoint为(0.5,0.5)。如果您尝试应用所有值都相对于左上角坐标的变换,则需要将锚点更改为(0.0,0.0)。