我有一个图像,我想要裁剪/遮罩/剪切它-我不知道英语中“好”这个词是什么。到目前为止,我使用的是ebimage库。我的图像具有以下尺寸:
dim : 768 512 我希望图像从左: 200右:250下:100上:150。我怎么才能裁剪到这种程度呢?
library(EBImage)
f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)
display(img)
#get new extend??**
writeImage(img, "new_extent.png")我必须对几个图像执行此操作...提前感谢;)
发布于 2019-05-07 08:11:36
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)

https://stackoverflow.com/questions/54853242
复制相似问题