首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-如何将图像裁剪/遮罩为AOI - Ebimage

R-如何将图像裁剪/遮罩为AOI - Ebimage
EN

Stack Overflow用户
提问于 2019-02-24 23:07:48
回答 1查看 598关注 0票数 0

我有一个图像,我想要裁剪/遮罩/剪切它-我不知道英语中“好”这个词是什么。到目前为止,我使用的是ebimage库。我的图像具有以下尺寸:

代码语言:javascript
复制
  dim          : 768 512 

我希望图像从左: 200右:250下:100上:150。我怎么才能裁剪到这种程度呢?

代码语言:javascript
复制
library(EBImage)
f = system.file("images", "sample.png", package="EBImage")
img = readImage(f)
display(img)
#get new extend??**
writeImage(img, "new_extent.png")

我必须对几个图像执行此操作...提前感谢;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 08:11:36

EBImage中的图像只是简单的数组。要裁剪图像,只需将数组的第一维和第二维(可能超过2维)设置为子集。在你的例子中,这个子集是:

代码语言:javascript
复制
  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()

这是一个与图像交互的示例。

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54853242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档