如何在许多图像中分割图像?在R

时间:2016-12-06 19:26:45

标签: r

我的图像尺寸为5400x2700,我想要做的是将图像分割成许多100x100尺寸的图像。

我正在尝试以下方式,但这给了我错误:

for (i in seq(1,5400-100,100)){
  for (j in seq(1,2700-100,100)){
    sub_image = image_original[i:i+100,j:j+100]
  }
}

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

这个比使用普通剪刀打印图片更快:

library(magick)
frink <- image_read("https://jeroenooms.github.io/images/frink.png")
( info <- image_info(frink) )
#   format width height colorspace filesize
# 1    PNG   220    445       sRGB    73494

crops <- list()
for (x in seq(0,info$width%/%100*100,100)) 
  for (y in seq(0,info$height%/%100*100,100)) 
    crops <- c(crops, image_crop(frink, sprintf("100x100+%d+%d", x, y)))

image_join(crops)
#    format width height colorspace filesize
# 1     PNG   100    100       sRGB        0
# 2     PNG   100    100       sRGB        0
# 3     PNG   100    100       sRGB        0
# 4     PNG   100    100       sRGB        0
# 5     PNG   100     45       sRGB        0
# 6     PNG   100    100       sRGB        0
# 7     PNG   100    100       sRGB        0
# 8     PNG   100    100       sRGB        0
# 9     PNG   100    100       sRGB        0
# 10    PNG   100     45       sRGB        0
# 11    PNG    20    100       sRGB        0
# 12    PNG    20    100       sRGB        0
# 13    PNG    20    100       sRGB        0
# 14    PNG    20    100       sRGB        0
# 15    PNG    20     45       sRGB        0

# For example, write tile in column 1, row 3 to a temp file: 
image_write(crops[[3]], tf <- tempfile(fileext = ".png"))
shell.exec(tf)