【题目】
原文:
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
译文:
一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度。 你能原地进行操作吗?(即不开辟额外的存储空间)
【分析】
点击打开链接
【代码一】
/*********************************
* 日期:2014-05-14
* 作者:SJF0115
* 题目: Rotate Image
* 来源:CareerCup
**********************************/
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
//旋转图片
void RotateImage(vector<vector<int> > &matrix){
int i,j,temp;
int N = matrix.size();
// 沿着副对角线反转
for(i = 0; i < N;i++){
for(j = 0;j < N - i;j++){
temp = matrix[i][j];
matrix[i][j] = matrix[N - 1 - j][N - 1 - i];
matrix[N - 1 - j][N - 1 - i] = temp;
}
}
// 沿着水平中线反转
for(i = 0; i < N / 2;i++){
for (j = 0; j < N;j++){
temp = matrix[i][j];
matrix[i][j] = matrix[N - 1 - i][j];
matrix[N - 1 - i][j] = temp;
}
}
}
int main(){
vector<int> row1 = {1,2,3};
vector<int> row2 = {4,5,6};
vector<int> row3 = {7,8,9};
vector<vector<int>> matrix;
matrix.push_back(row1);
matrix.push_back(row2);
matrix.push_back(row3);
RotateImage(matrix);
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
【代码二】
/*********************************
* 日期:2014-05-14
* 作者:SJF0115
* 题目: Rotate Image
* 来源:CareerCup
**********************************/
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
//旋转图片
void RotateImage(vector<vector<int> > &matrix){
int layer,i,top;
int n = matrix.size();
//一层一层旋转
for(layer = 0;layer < n/2;layer++){
//第layer层第一个元素
int first = layer;
//第layer层最后一个个元素
int last = n -1 - layer;
for(i = first;i < last;i++){
//偏移量
int offset = i - first;
top = matrix[first][i];
//left-top
matrix[first][i] = matrix[last-offset][first];
//bottom-left
matrix[last-offset][first] = matrix[last][last-offset];
//right-bottom
matrix[last][last-offset] = matrix[i][last];
//top-right
matrix[i][last] = top;
}//for
}//for
}
int main(){
vector<int> row1 = {1,2,3};
vector<int> row2 = {4,5,6};
vector<int> row3 = {7,8,9};
vector<vector<int>> matrix;
matrix.push_back(row1);
matrix.push_back(row2);
matrix.push_back(row3);
RotateImage(matrix);
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
分享到:
相关推荐
rotate image matlab
最新的rotateimage图像处理组件,实现图片以中心/非中心 作为旋转焦点进行任意角度的旋转 内有Demo可供参考学习 a component similar to TImage with the ability to show the image rotated at an arbitrary ...
matlab开发-RotateImage。图像旋转由3个可分离的偏斜执行
使用javascript旋转图片,任意度数,测试通过。只是图片不能太大,不然很慢,或是死掉
利用matlab对图像进行旋转处理的程序代码。以及图像插值算法的原理
很多新手无法安装这个控件, 因为原始控件不带package包,缺少加载文件。这个带了,里边缺少的文件补齐了。 install时如果出现 dsgnintf.dcu或DesignIde.dcu 找不到 或 DockForm.dcu 找不到 ... ...
VB api编程模块旋转图像设计代码VB api code programming model rotate the image design
Image Rotate Using Javascript
React本机图像旋转该项目支持本机> = 0.40.0 该模块旨在与react-native的一起使用,以创建功能齐全的裁剪工具安装首先通过npm安装软件包$ npm install react-native-image-rotate 然后使用链接本机库$ react-native ...
Rotate image without degrading it
这个matlab程序实现图像的任意角度的顺时针旋转。
Rotate a bitmap image转动一个位图(10KB)
Unlike traditional rotation of images, where every n'th pixel is sampled and copied to the result image, this template provides much more accurate image rotation features (weighing the pixels). ...
此apache mod_log_rotate 是win32下vc9版的apache 日志文件管理工具。 使用: 复制mod_log_rotate.so到 Apache2/modules 确保你的系统安装了 Visual C++ 2008 Redistributable (可从这里下载:) ...
edge detection, emboss, sharpen, smooth, flip vertical and horizontal, gamma adjustment, negative, interpolated resize, rotate 90, 180, 270 and arbitrary angle, cropping, format conversion including ...
jquery.rotate.min.js 版本VERSION: 2.3
rotate旋转
语言:English 允许您使用右键单击上下文菜单旋转图像 当右键单击图像时添加“旋转”菜单选项,允许您右键或左键 </ div>