# 原点中心の回転 library(imager) # 度からラジアンに変換する関数 rad <- function(deg) { return(deg * pi / 180) } im <- grayscale(boats) # 回転角(度) theta <- 30 dim0 <- dim(im) # cimg変数で処理すると遅いので配列で処理する g0 <- array(im, dim0) g1 <- array(0, dim0) # 回転 ct <- cos(rad(theta)) st <- sin(rad(theta)) for (x1 in 1 : dim0[1]) { for (y1 in 1 : dim0[2]) { x0 <- round( x1 * ct + y1 * st) y0 <- round(-x1 * st + y1 * ct) if (x0 < 1 || x0 > dim0[1] || y0 < 1 || y0 > dim0[2]) next g1[x1, y1, ,] <- g0[x0, y0, ,] } } # 配列→cimg変数 im.rotate <- as.cimg(g1) # 同時に2枚並べるレイアウト layout(t(1 : 2)) plot(im, interpolate = FALSE, main = "元の画像") plot(im.rotate, interpolate = FALSE, main = "回転後の画像") # レイアウトを元に戻す layout(1)