gpt4 book ai didi

c++ - OpenCV,围绕特定点旋转

转载 作者:行者123 更新时间:2023-11-28 03:13:30 25 4
gpt4 key购买 nike

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

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;

最佳答案

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

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;

只需删除 *

关于c++ - OpenCV,围绕特定点旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17655877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com