EBImage 中的图像只是数组。要裁剪图像,您只需将数组的第一维和第二维子集(可能有超过 2 维)。在您的示例中,该子集将是:
ix <- 200:250
iy <- 100:150
dim(img) # determine the dimensions, 2 in this case
new_img <- img[ix, iy]
plot(new_img)
writeImage(new_img, "new_img.png")
如果您知道每张图片的坐标,您只需为每张图片创建索引,如上所示。但是,如果您想选择要裁剪的每个图像的部分,您可以使用locator() 将图像绘制为光栅图像。
这是与图像交互的示例。
# Starting with EBImage loaded
f <- system.file("images", "sample.png", package="EBImage")
img <- readImage(f)
plot(img)
# This call to locator selects two points and places a red mark at each
p <- locator(2, type = "p", pch = 3, col = "red")
print(p)
> $x
> [1] 62.35648 314.30908
>
> $y
> [1] 166.1247 316.4605
# Convert the pairs of coordinates into a index to subset the array
p <- lapply(p, round)
i <- lapply(p, function(v) min(v):max(v))
new_img <- img[i$x, i$y]
plot(new_img)