如何旋转图像 R 光栅 [英] How to rotate an image R raster
问题描述
我有下面的代码,可以将图像保存到我的电脑.我想将该图像围绕其中心(或左下角)旋转 45,90 和 135 度,然后另存为 3 个不同的图像.我怎么能这样做?
I have code below which saves an image to my pc. I would like to rotate that image by 45,90 and 135 degrees around its center (or bottom left hand corner) and then save as 3 different images. How could I do that?
library(raster)
r1 <- brick(system.file("external/rlogo.grd", package="raster"))
x <- crop(r1, extent(0,50,0,50))
plotRGB(x)
png(width=50, height=50)
par(mai=c(0,0,0,0))
image(x)
dev.off()
---------update1-------------
---------update1-------------------------
根据接受的答案,工作代码如下
Based upon the accepted answer the working code is as below
library(raster)
r1 <- brick(system.file("external/rlogo.grd", package="raster"))
r1
x <- crop(r1, extent(0,ncol(r1),0,nrow(r1)))
plotRGB(x)
x1 <- 0:ncol(x)
y1 <- 0:nrow(x)
z <- matrix(1, nrow=length(x1), ncol=length(y1))
col.mat <- t(apply(matrix(rgb(getValues(x)/255), nrow=nrow(x), byrow=TRUE), 2, rev))
# Rotate 45 degrees
persp(x1, y1, z, zlim=c(0,2), theta = 20, phi = 90,
col = col.mat, scale=FALSE, border=NA, box=FALSE)
png("SaveThisPlot.png")
persp(x1, y1, z, zlim=c(0,2), theta = 20, phi = 90,
col = col.mat, scale=FALSE, border=NA, box=FALSE)
dev.off()
推荐答案
对于 90 度旋转,这是一个简单的解决方案:
For the 90 degree rotations, this is an easy solution:
image(t(flip(x, 1)))
image(t(flip(x, 2)))
plotRGB(t(flip(x, 1)))
plotRGB(t(flip(x, 2)))
对于 45 度和 135 度旋转,会有点棘手.可能还有其他方法,但我将使用 persp
函数并为 theta
参数提供不同的角度.
For the 45 degree and 135 degree rotation, it will be a bit trickier. There are probably other ways, but I'll use the persp
function and give different angles to the theta
argument.
只需正确设置对 persp
的调用即可.x1
、y1
和 z
只是 persp
函数的输入(参见 ?persp代码> 以了解有关该函数的参数的更多信息).
col.mat
是一个保存颜色值的矩阵.
It's just a matter of setting up the call to persp
properly. x1
, y1
, and z
are just inputs for the persp
function (see ?persp
for more about the arguments to that function). col.mat
is a matrix holding color values.
x1 <- 0:ncol(x)
y1 <- 0:nrow(x)
z <- matrix(1, nrow=length(x1), ncol=length(y1))
col.mat <- t(apply(matrix(rgb(getValues(x)/255), nrow=nrow(x), byrow=TRUE), 2, rev))
# the transposing and reversing are just to get the colors in the same
# spots as they're in when viewing plotRGB(x).
#
# getValues(x) is how you get the rgb colors, in the form of a 3-column matrix.
# rgb(getValues(x)/255) converts them into hex code, which is convenient enough here.
如果您发现这是您正在寻找的镜像,请尝试以不同的方式填充颜色矩阵.例如:
If you find that this is the mirror image of what you're looking for, try filling up the color matrix differently. For example:
col.mat <- matrix(rgb(getValues(x)/255), nrow=nrow(x))
如您所知,正确填充颜色矩阵是使这种方法适合您的关键.我将把它作为练习留给读者,以了解如何对颜色矩阵进行任何其他操作.
As you can tell, filling up the color matrix properly is the key to making this approach work for you. I'll leave it as an exercise to the reader to figure out how to do any other manipulations to the color matrix.
现在,调用 persp
.这里我设置了zlim
的值,所以有一个范围包括1.因为我把所有的z
值都设置为1,所以你需要设置一个有效的范围,否则,persp
将抛出有关无效限制的错误.它不喜欢从 1 到 1 的范围.
Now, call persp
. Here, I set the zlim
values so there is a range including 1. Because I made all the z
values 1, you need to set a valid range, otherwise, persp
will throw an error about invalid limits. It doesn't like a range from 1 to 1.
# Rotate 45 degrees
persp(x1, y1, z, zlim=c(0,2), theta = 45, phi = 90,
col = col.mat, scale=FALSE, border=NA, box=FALSE)
这里是 135 度:
# Rotate 135 degrees
persp(x1, y1, z, zlim=c(0,2), theta = 135, phi = 90,
col = col.mat, scale=FALSE, border=NA, box=FALSE)
可以按照您在问题中显示的相同方式保存图:
Saving the plots can be done in the same way you show in your question:
png("SaveThisPlot.png")
persp(x1, y1, z, zlim=c(0,2), theta = 135, phi = 90,
col = col.mat, scale=FALSE, border=NA, box=FALSE)
dev.off()
这篇关于如何旋转图像 R 光栅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!