Rotate Image

LeetCode Rotate Image


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?


关键地方在于就地。而不用新的内存。
这里采用两次旋转的方法。

/*
 1 2 3     7 8 9     7 4 1
 4 5 6  => 4 5 6  => 8 5 2
 7 8 9     1 2 3     9 6 3
*/
void rotate(vector<vector<int> > &matrix) {
    reverse(matrix.begin(), matrix.end());
    for (int i = 0; i < matrix.size(); ++i) {
        for (int j = i + 1; j < matrix[i].size(); ++j)
            swap(matrix[i][j], matrix[j][i]);
    }
}

在提供的引用中未提及 `rotateimage()` 函数,但有一些与图像旋转相关的函数示例,可作为参考。 ### 功能 不同库中的图像旋转函数功能类似,主要是对图像进行旋转操作。如在 `PIL` 库中,`image.rotate()` 可将图像逆时针旋转指定角度;`cv2.rotate()` 能对图像进行不同角度的顺时针旋转;R 语言的 `magick` 包中的 `image_rotate()` 可对图像进行旋转操作 [^1][^2][^3]。 ### 使用方法 - **`PIL` 库的 `image.rotate()`** ```python from PIL import Image # 打开图像 image = Image.open('your_image.jpg') # 逆时针旋转 90 度 rotated = image.rotate(90, expand=True) # 保存旋转后的图像 rotated.save('rotated.jpg') ``` - **`cv2.rotate()`** ```python import cv2 # 读取图像 img = cv2.imread('your_image.jpg') # 调整图像大小 img = cv2.resize(img, None, fx=0.7, fy=0.7) # 顺时针 90 度旋转 demo1 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) # 顺时针 270 度旋转 demo2 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # 顺时针 180 度旋转 demo3 = cv2.rotate(img, cv2.ROTATE_180) # 显示图像 cv2.imshow('base', img) cv2.imshow('demo1', demo1) cv2.imshow('demo2', demo2) cv2.imshow('demo3', demo3) # 等待按键 cv2.waitKey(0) # 关闭所有窗口 cv2.destroyAllWindows() ``` - **R 语言 `magick` 包的 `image_rotate()`** ```R library(magick) # 假设 frink 是一个图像对象 frink <- image_read('your_image.jpg') # 旋转 45 度 rotated_image <- image_rotate(frink, 45) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值