28. 图像旋转

本文介绍了一种简单的图像旋转方法,通过创建新图像并遍历每个像素,利用极坐标计算源位置来实现旋转。针对顺时针旋转140度的情况给出了具体步骤及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

28.Image Rotation

This post is a quick diversion, on request from a user on the Greenfoot site. They asked how to rotate an image, which is actually quite a simple task, given what we have already covered about rotation andpolar coordinates.

这篇帖子是一个小插曲,为了回答Greenfoot网站一个用户的提问。他们问到如何旋转一个图像,它实际上这是一个非常简单的任务,因为我们已经讨论了旋转和极坐标

The idea behind rotating an image is this: create a new image, and go through every pixel on the new image. For that destination pixel, calculate a source position by rotating the position backwards. Then pick out that pixel from the original image, and use that colour to fill in the new pixel.

旋转图像背后的思路是:创建一个新图像,遍历新图像的每一个像素。对于目标像素,通过反向旋转其位置来计算源位置。接着从源图像取出那个像素,并使用它的颜色去填充新的像素。

Assume you want to rotate an image clockwise by 140 degrees. You go through the pixels: for example, the coloured pixel on the new image on the left, below. To work out what colour to use, you convert the centre of that pixel into polar coordinates, relative to the centre of the image (assuming you want to rotate around the centre). Then subtract 140 degrees from the rotation, but keep the distance from the centre the same. Wherever this new coordinate is (once you’ve converted back to cartesian coordinates), use that pixel as the colour:

假设你希望将一个图像顺时针旋转140度。你检查像素:比如,下图中左边新图像的彩色像素。为了算出要使用什么颜色,你将那个像素的中点转换为极坐标,相对于图像的中心而言(假设你希望围绕中点旋转)。接着让角度减去140度,而距离中点的距离保持不变。无论这个新的坐标在哪(一旦你已经转换回笛卡尔坐标),使用那个像素作为颜色。

That leads to the following source code:

这可以用如下代码表示:

    public void rotateImage(int rotateBy)
    {
        GreenfootImage rotated = new GreenfootImage(original.getWidth(), original.getHeight());
        
        double centreX = (double)original.getWidth() / 2;
        double centreY = (double)original.getHeight() / 2;
        
        for (int x = 0; x < rotated.getWidth(); x++)
        {
            for (int y = 0; y < rotated.getHeight(); y++)
            {
                double dir = calculateDirection(x - centreX, y - centreY);
                double mag = calculateMagnitude(x - centreX, y - centreY);
                
                dir = dir - rotateBy;
                
                int origX = (int)(centreX + calculateX(dir, mag));
                int origY = (int)(centreY + calculateY(dir, mag));
                
                if (origX >= 0 && origX < original.getWidth() &&
                    origY >= 0 && origY < original.getHeight())
                {
                    rotated.setColorAt(x, y, original.getColorAt(origX, origY));
                }
            }
        }
        
        setImage(rotated);
    }

This is what is known as a nearest-neighbour rotation: for each pixel we are picking the single closest pixel to use as the colour. Better rotation algorithms (especially for small images) do a blend of pixels from the original image. But you canhave a play with this simple image rotation code via the scenario on the Greenfoot site.

这是被称为最近相邻旋转:对于每个像素我们获取单一最接近的像素去作为颜色使用。更好地旋转算法(特别对于小图像)实行了源图像像素的混合。但是你可以通过Greenfoot网站的游戏剧本玩一下这个简单的图像旋转代码

Exercises

Our image rotation isn’t perfect — here’s some challenge exercises for you to improve it:

  1. The image has its corners cut off when it is rotated. Think about why this is, and work out how large you need to make the destination image to avoid this ever happening (hint: the longest dimension across a rectangle is from one corner to the other)?
  2. Rather than make the image as large as possible, can you work out exactly how big it needs to be, given the degrees to rotate by?
  3. Currently the image rotates around the centre of the original. Can you modify the code to rotate around an arbitrary point in the original image? (Note: this will mess with the previous adjustments to make the destination image be large enough!)
资源下载链接为: https://pan.quark.cn/s/67c535f75d4c 在机器人技术中,轨迹规划是实现机器人从一个位置平稳高效移动到另一个位置的核心环节。本资源提供了一套基于 MATLAB 的机器人轨迹规划程序,涵盖了关节空间和笛卡尔空间两种规划方式。MATLAB 是一种强大的数值计算与可视化工具,凭借其灵活易用的特点,常被用于机器人控制算法的开发与仿真。 关节空间轨迹规划主要关注机器人各关节角度的变化,生成从初始配置到目标配置的连续路径。其关键知识点包括: 关节变量:指机器人各关节的旋转角度或伸缩长度。 运动学逆解:通过数学方法从末端执行器的目标位置反推关节变量。 路径平滑:确保关节变量轨迹连续且无抖动,常用方法有 S 型曲线拟合、多项式插值等。 速度和加速度限制:考虑关节的实际物理限制,确保轨迹在允许的动态范围内。 碰撞避免:在规划过程中避免关节与其他物体发生碰撞。 笛卡尔空间轨迹规划直接处理机器人末端执行器在工作空间中的位置和姿态变化,涉及以下内容: 工作空间:机器人可到达的所有三维空间点的集合。 路径规划:在工作空间中找到一条从起点到终点的无碰撞路径。 障碍物表示:采用二维或三维网格、Voronoi 图、Octree 等数据结构表示工作空间中的障碍物。 轨迹生成:通过样条曲线、直线插值等方法生成平滑路径。 实时更新:在规划过程中实时检测并避开新出现的障碍物。 在 MATLAB 中实现上述规划方法,可以借助其内置函数和工具箱: 优化工具箱:用于解决运动学逆解和路径规划中的优化问题。 Simulink:可视化建模环境,适合构建和仿真复杂的控制系统。 ODE 求解器:如 ode45,用于求解机器人动力学方程和轨迹执行过程中的运动学问题。 在实际应用中,通常会结合关节空间和笛卡尔空间的规划方法。先在关节空间生成平滑轨迹,再通过运动学正解将关节轨迹转换为笛卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值