【问题标题】:R- How can i crop/mask a image to AOI - EbimageR-我如何将图像裁剪/屏蔽到 AOI - Ebimage
【发布时间】:2019-02-24 15:07:47
【问题描述】:

我有一张图片,我想对其进行裁剪/遮罩/剪切 - 我不知道英语中的“好”这个词。 直到现在我使用 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")

我必须为几张图片执行此操作...在此先感谢 ;)

【问题讨论】:

    标签: r image crop mask


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 1970-01-01
      • 2020-09-23
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2012-11-16
      相关资源
      最近更新 更多