0

我正在尝试围绕它们的中点旋转四个点(矩形)。但结果看起来很奇怪。我使用在这里和维基百科上找到的公式

p.x'=p.x*cos(alpha) + p.y*sin(alpha);

p.y'=(-1)p.x*sin(alpha) + p.y*cos(alpha);

这个公式是否适用于我的目的(旋转矩形)?

这里是源码,我试着逆时针旋转33度

非常感谢您

Mat img(480, 800, CV_8UC4, Scalar(255,255,255,255));
Mat dst(480, 800, CV_8UC4, Scalar(255,255,255,255));

Point p1, p2, p3, p4;
Point center;

p1.x=501; p1.y=247;
p2.x=429; p2.y=291;
p3.x=388; p3.y=222;
p4.x=451; p4.y=186;

circle(img, p1, 3, Scalar(255, 0, 0, 255), 3, 8, 0 );
circle(img, p2, 3, Scalar(0, 255, 0, 255), 3, 8, 0 );
circle(img, p3, 3, Scalar(0, 0, 255, 255), 3, 8, 0 );
circle(img, p4, 3, Scalar(255, 255, 0, 255), 3, 8, 0 );

center.x = (p1.x+p2.x+p3.x+p4.x)/4;
center.y = (p1.y+p2.y+p3.y+p4.y)/4;


double alpha = -33 * 0.0174532925;
double s = sin(alpha);
double c = cos(alpha);

p1.x = (+c*(p1.x-center.x)* + s*(p1.y-center.y)) + center.x;
p1.y = (-s*(p1.x-center.x) + c*(p1.y-center.y)) + center.y;
p2.x = (+c*(p2.x-center.x)* + s*(p2.y-center.y)) + center.x;
p2.y = (-s*(p2.x-center.x) + c*(p2.y-center.y)) + center.y;
p3.x = (+c*(p3.x-center.x)* + s*(p3.y-center.y)) + center.x;
p3.y = (-s*(p3.x-center.x) + c*(p3.y-center.y)) + center.y;
p4.x = (+c*(p4.x-center.x)* + s*(p4.y-center.y)) + center.x;
p4.y = (-s*(p4.x-center.x) + c*(p4.y-center.y)) + center.y;

circle(dst, p1, 3, Scalar(255, 0, 0, 255), 3, 8, 0 );
circle(dst, p2, 3, Scalar(0, 255, 0, 255), 3, 8, 0 );
circle(dst, p3, 3, Scalar(0, 0, 255, 255), 3, 8, 0 );
circle(dst, p4, 3, Scalar(255, 255, 0, 255), 3, 8, 0 );

imshow("src", img);
imshow("dst", dst);
cvMoveWindow("dst", 0, img.cols+50);

waitKey(0);
return 0;
4

2 回答 2

1

你的公式是正确的。你的数学看起来不错。您的问题可能只是额外的*符号:

p1.x = (+c*(p1.x-center.x)* + s*(p1.y-center.y)) + center.x;
                          ^

这被评估为:

p1.x = ((+c*(p1.x-center.x))* (+ s*(p1.y-center.y))) + center.x;

只需删除*

于 2013-07-15T14:03:38.490 回答
1

所以,我找到了解决方案。我菜鸟用新的计算值覆盖以前的 x 值这是解决方案

for(int i = 0; i<360; i++)
{
    Mat img(480, 800, CV_8UC4, Scalar(255,255,255,255));
    Mat dst(480, 800, CV_8UC4, Scalar(255,255,255,255));

    Point p1, p2, p3, p4;
    Point center;

    p1.x=501; p1.y=247;
    p2.x=429; p2.y=291;
    p3.x=388; p3.y=222;
    p4.x=451; p4.y=186;

    circle(img, p1, 3, Scalar(255, 0, 0, 255), 3, 8, 0 );
    circle(img, p2, 3, Scalar(0, 255, 0, 255), 3, 8, 0 );
    circle(img, p3, 3, Scalar(0, 0, 255, 255), 3, 8, 0 );
    circle(img, p4, 3, Scalar(255, 255, 0, 255), 3, 8, 0 );

    center.x = (p1.x+p2.x+p3.x+p4.x)/4;
    center.y = (p1.y+p2.y+p3.y+p4.y)/4;

    double alpha = -i * 0.0174532925;
    double s = sin(alpha);
    double c = cos(alpha);
    int x = 0, y = 0;

    x=p1.x; y=p1.y;
    p1.x = (c*(x-center.x) - s*(y-center.y)) + center.x;
    p1.y = (s*(x-center.x) + c*(y-center.y)) + center.y;
    x=p2.x; y=p2.y;
    p2.x = (c*(x-center.x) - s*(y-center.y)) + center.x;
    p2.y = (s*(x-center.x) + c*(y-center.y)) + center.y;
    x=p3.x; y=p3.y;
    p3.x = (c*(x-center.x) - s*(y-center.y)) + center.x;
    p3.y = (s*(x-center.x) + c*(y-center.y)) + center.y;
    x=p4.x; y=p4.y;
    p4.x = (c*(x-center.x) - s*(y-center.y)) + center.x;
    p4.y = (s*(x-center.x) + c*(y-center.y)) + center.y;


    circle(dst, p1, 3, Scalar(255, 0, 0, 255), 3, 8, 0 );
    circle(dst, p2, 3, Scalar(0, 255, 0, 255), 3, 8, 0 );
    circle(dst, p3, 3, Scalar(0, 0, 255, 255), 3, 8, 0 );
    circle(dst, p4, 3, Scalar(255, 255, 0, 255), 3, 8, 0 );

    imshow("src", img);
    imshow("dst", dst);
    cvMoveWindow("dst", 0, img.cols+50);

    waitKey(20);
于 2013-07-16T08:55:57.177 回答