在坐标变换中迭代旋转矩形中的点

huangapple go评论64阅读模式
英文:

Iterating over points in a rotated rectangle using coordinate transformation

问题

我正在尝试迭代旋转矩形内的点,这个矩形基本上是一个包含特定像素值的图像矩阵,并根据某些条件修改它们。但是循环计数器不能正确填充矩形,矩形中有一些"空白空间"没有被跟踪。

我的方法:

使用嵌套的for循环轻松迭代非旋转矩形内的各个点。我的方法是生成非旋转矩形内的点,并使用旋转矩阵进行坐标变换,将这些点映射到旋转角度上。旋转角度可以很容易地找到。

我使用的代码如下-

% 定义矩形顶点的坐标
x1 = 100;  % 顶点1的X坐标
y1 = 200;  % 顶点1的Y坐标
x2 = 400;  % 顶点2的X坐标
y2 = 300;  % 顶点2的Y坐标
x3 = 300;  % 顶点3的X坐标
y3 = 500;  % 顶点3的Y坐标
x4 = x1 + (x3 - x2);  % 顶点4的X坐标
y4 = y1 + (y3 - y2);  % 顶点4的Y坐标

% 计算旋转角度
angle = -atan2(y2 - y1, x2 - x1);  

% 计算矩形的中心
centerX = (x1 + x2 + x3 + x4) / 4;
centerY = (y1 + y2 + y3 + y4) / 4;

% 定义矩形的尺寸
width = (x2 - x1);
height = (y4 - y1);

xv = [x1 x2 x3 x4 x1];
yv = [y1 y2 y3 y4 y1];
plot(xv, yv, 'r', 'LineWidth', 2)

% 在矩形边界内迭代点
for i = round(centerY - height/2):10: round(centerY + height/2)
    for j = round(centerX - width/2):10: round(centerX + width/2)

        % 应用反向旋转变换
        x = (cos(angle) * (j - centerX)) + (sin(angle) * (i - centerY)) + centerX;
        y = (-sin(angle) * (j - centerX)) + (cos(angle) * (i - centerY)) + centerY;
        
        x11 = round(x);
        y11 = round(y);

        hold on
        plot(x11, y11, 'b*')
    end
end

输出和问题:

我已经展示了如何使用反正切函数计算旋转角度。

显然,我绘制的点没有正确填充矩形。请注意:出于清晰起见,我在每次迭代中将循环计数器设置为5个点,但如黄色箭头所示,有一些未填充的空白区域。紫色箭头也显示了一些点超出了预期的矩形范围。我应该怎么做来纠正这个错误?显然,旋转角度可能存在一些错误,但我不确定。

提前感谢!

英文:

I am trying to iterate over the points inside a rotated rectangle, which is basically an image matrix containing certain pixel values and modify them according to certain conditions. But the loop counter does not fill up rectangle properly and there are 'empty spaces' in the rectangle that are not being tracked.

My approach:

It is easy to iterate using a nested for loop over the individual points inside a non-rotated rectangle. My approach is to generate the points in a non-rotated rectangle and using a coordinate transformation using the rotation matrix to map those points into the rotated angle. The angle of rotation can be found easily.

The code I used is as follows-

% Define the coordinates of the vertices of the rectangle
x1 = 100;  % X-coordinate of vertex 1
y1 = 200;  % Y-coordinate of vertex 1
x2 = 400;  % X-coordinate of vertex 2
y2 = 300;  % Y-coordinate of vertex 2
x3 = 300;  % X-coordinate of vertex 3
y3 = 500;  % Y-coordinate of vertex 3
x4 = x1 + (x3 - x2);  % X-coordinate of vertex 4
y4 = y1 + (y3 - y2);  % Y-coordinate of vertex 4

% Calculate the rotation angle
angle = -atan2(y2 - y1, x2 - x1);  


% Calculate the center of the rectangle
centerX = (x1 + x2 + x3 + x4) / 4;
centerY = (y1 + y2 + y3 + y4) / 4;

% Define the dimensions of the rectangle
width = (x2 - x1);
height = (y4 - y1);

xv = [x1 x2 x3 x4 x1];
yv = [y1 y2 y3 y4 y1];
plot(xv,yv,'r','LineWidth',2)

% Iterate over points within the rectangle boundaries
for i = round(centerY - height/2):10: round(centerY + height/2)
    for j = round(centerX - width/2):10: round(centerX + width/2)
        

        % Apply inverse rotation transformation
        x = (cos(angle) * (j - centerX)) + (sin(angle) * (i - centerY)) + centerX;
        y = (-sin(angle) * (j - centerX)) + (cos(angle) * (i - centerY)) + centerY;
        
        x11=round(x);
        y11=round(y);

        hold on
        plot(x11,y11,'b*')
    end
end

The output and the problem:
在坐标变换中迭代旋转矩形中的点

I have shown how I have calculated the angle of rotation using the inverse tangent function.

Clearly, the points I plotted are not really filling up the rectangle properly. Note: I have set the loop counter at 5 points at each iteration for clarity but as shown by the yellow arrows, there are empty places that are not being filled. The purple arrows also show some points that just slip outside the intended rectangle. What should I do to correct this fault? Apparently it looks like there is some error in the rotation angle but I am not sure.

Thanks in advance!

答案1

得分: 1

您的坐标 x1y1.. 不描述矩形,而是平行四边形(您可以检查两个相邻边之间的角度不是 Pi/2)。

因此,这个变换的仿射矩阵不仅仅是简单的旋转,而包括剪切,您的应用逆旋转变换是不正确的。

您有两个选择 - 要么精确计算旋转后的矩形顶点,要么使用给定的平行四边形和相应的仿射矩阵。

您可以获取正确的仿射变换矩阵如此描述这里有现成的公式

(对于矩形,由于某些坐标相等,源表达式变得更简单一些,但在Matlab中,您可以使用内置方法求逆并乘以矩阵)。

英文:

Your coordinates x1y1.. don't describe rectangle but parallelogram (you can check that angle between two neighbor sides in not Pi/2).

So affine matrix of this transformation is not simple rotation but includes shear, and your Apply inverse rotation transformation is not correct.

You have a choice - or calculate rotated rectangle vertices exactly, either use given parallelogram with corresponding affine matrix.

You can get correct matrix of affine transform as described here or here with ready formulas

(for rectangle source expressions become a bit simpler due to some equal coordinates, but in Matlab you can just inverse and multuply matrices with inbuilt methods)

huangapple
  • 本文由 发表于 2023年6月22日 03:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定