Rotate Image

难度:1

需注意n为奇数时中间一列做特殊处理

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

class Solution 
{
public:
    void rotate(vector<vector<int> > &matrix) 
	{
		int n=matrix.size();
		for(int i=0;i<=n/2-1;i++)
		{
			for(int j=0;j<=n/2-1;j++)
			{
				int x=i,y=j;
				int tmp=matrix[x][y];
				matrix[x][y]=matrix[n-1-y][x];
				matrix[n-1-y][x]=matrix[n-1-x][n-1-y];
				matrix[n-1-x][n-1-y]=matrix[y][n-1-x];
				matrix[y][n-1-x]=tmp;
			}
		}
		if(n&1)
		{
			for(int i=0;i<n/2;i++)
			{
				int tmp=matrix[i][n/2];
				matrix[i][n/2]=matrix[n/2][i];
				matrix[n/2][i]=matrix[n-1-i][n/2];
				matrix[n-1-i][n/2]=matrix[n/2][n-1-i];
				matrix[n/2][n-1-i]=tmp;
			}
		}
    }
};


### Halcon中RotateImage函数的用法或相关文档 在Halcon中,`RotateImage` 函数并不直接存在,但可以通过仿射变换(Affine Transformation)来实现图像旋转的功能。具体来说,可以使用 `hom_mat2d_identity`、`hom_mat2d_rotate` 和 `affine_trans_image` 等函数组合完成图像旋转操作[^2]。 以下是实现图像旋转的具体方法: 1. 创建一个单位矩阵作为初始变换矩阵: ```cpp hom_mat2d_identity (HomMat2D); ``` 2. 为旋转矩阵赋值,指定旋转角度和旋转中心点: ```cpp hom_mat2d_rotate (HomMat2D, Phi, Px, Py, HomMat2DRotate); ``` - `Phi`:旋转角度(以弧度为单位)。 - `Px` 和 `Py`:旋转中心的 x 和 y 坐标。 - `HomMat2DRotate`:输出的旋转矩阵。 3. 使用旋转矩阵对图像进行仿射变换: ```cpp affine_trans_image (Image, ImageAffinTrans, HomMat2DRotate, 'constant', true); ``` - `Image`:输入图像。 - `ImageAffinTrans`:输出的旋转后图像。 - `HomMat2DRotate`:赋值后的旋转矩阵。 - `'constant'`:插值方式,默认为常量插值。 - `true`:判断是否调整输出图像的大小以适应旋转后的结果。 以下是一个完整的代码示例: ```cpp // 定义变量 HObject Image, ImageAffinTrans; double HomMat2D, Phi, Px, Py, HomMat2DRotate; // 读取图像 read_image (Image, 'example_image'); // 创建单位矩阵 hom_mat2d_identity (HomMat2D); // 设置旋转参数 Phi = deg_to_rad(45); // 将角度转换为弧度 Px = 256; // 旋转中心x坐标 Py = 256; // 旋转中心y坐标 // 计算旋转矩阵 hom_mat2d_rotate (HomMat2D, Phi, Px, Py, HomMat2DRotate); // 对图像进行仿射变换 affine_trans_image (Image, ImageAffinTrans, HomMat2DRotate, 'constant', true); ``` 此外,关于图像的基本信息,如通道数和深度等,可以参考 Halcon 的图像处理基础[^3]。这有助于理解图像旋转过程中可能涉及的像素值变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值