如何旋转图像栅格 [英] How to rotate an image R raster

查看:213
本文介绍了如何旋转图像栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有代码将图像保存到我的电脑.我想将该图像围绕其中心(或左下角)旋转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的调用即可. x1y1z只是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()

这篇关于如何旋转图像栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆