0

それらの中間点を中心に4つの点(長方形)を回転させようとしています。しかし、結果は奇妙に見えます。こことウィキペディアにある式を使用します

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 に答える