R- 我如何 crop/mask 图像到 AOI - Ebimage
R- How can i crop/mask a image to AOI - Ebimage
我有一张图片,我想 crop/mask/cut 它 - 我不知道英语中的 "good" 单词。
直到现在我都在使用 ebimage 库。我的图片具有以下尺寸:
dim : 768 512
我想要左边的图片:200 right:250 bottom:100 top:150。我怎样才能将它裁剪到这种程度?
library(EBImage)
f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)
display(img)
#get new extend??**
writeImage(img, "new_extent.png")
我必须对几张图片执行此操作...提前致谢 ;)
EBImage 中的图像只是数组。要裁剪图像,您只需对数组的第一维和第二维进行子集化(可能有多个维度)。在您的示例中,此子集为:
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")
如果您知道每个图像的坐标,您可以简单地为每个图像创建索引,如上所示。但是,如果您想要 select 裁剪每个图像的部分,您可以使用 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)
我有一张图片,我想 crop/mask/cut 它 - 我不知道英语中的 "good" 单词。 直到现在我都在使用 ebimage 库。我的图片具有以下尺寸:
dim : 768 512
我想要左边的图片:200 right:250 bottom:100 top:150。我怎样才能将它裁剪到这种程度?
library(EBImage)
f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)
display(img)
#get new extend??**
writeImage(img, "new_extent.png")
我必须对几张图片执行此操作...提前致谢 ;)
EBImage 中的图像只是数组。要裁剪图像,您只需对数组的第一维和第二维进行子集化(可能有多个维度)。在您的示例中,此子集为:
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")
如果您知道每个图像的坐标,您可以简单地为每个图像创建索引,如上所示。但是,如果您想要 select 裁剪每个图像的部分,您可以使用 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)