# リサイズ(最近傍法) library(imager) im <- grayscale(boats) # 倍率 ax <- 2 ay <- 2 # 元画像の次元 dim0 <- dim(im) # 新画像の次元 dim1 <- dim0 # 横幅のリサイズ dim1[1] <- round(ax * dim0[1]) # 縦幅のリサイズ dim1[2] <- round(ay * dim0[2]) # cimg変数で処理すると遅いので配列で処理する g0 <- array(im, dim0) g1 <- array(0, dim1) # 新画像の座標でループ for (x1 in 1 : dim1[1]) { # 逆変換 x1→x0 x0 <- round(x1 / ax) if (x0 < 1 || x0 > dim0[1]) next for (y1 in 1 : dim1[2]) { # 逆変換 y1→y0 y0 <- round(y1 / ay) if (y0 < 1 || y0 > dim0[2]) next g1[x1, y1, ,] <- g0[x0, y0, ,] } } # 配列→cimg変数 im.resize <- as.cimg(g1) # 同時に2枚並べるレイアウト layout(t(1 : 2)) xl <- c(1, width(im.resize)) yl <- c(height(im.resize), 1) plot(im, xlim = xl, ylim = yl, interpolate = FALSE, main = "元の画像") plot(im.resize, xlim = xl, ylim = yl, interpolate = FALSE, main = "最近傍法") # レイアウトを元に戻す layout(1)