【问题标题】:R brick extracting color channelsR砖提取颜色通道
【发布时间】:2015-08-03 22:27:48
【问题描述】:

我的问题基于my earlier question。该解决方案适用于给定的示例(rlogo)。但是当我加载 JPEG 图像时,我收到如下所示的错误。

   library(raster)
    r1 <- brick("results_circle.jpg")#please load any jpeg image

    width=ncol(r1)
    height=nrow(r1)
    x <- crop(r1, extent(0,width,0,height))
    plotRGB(x)
    str(x)
    class(x)

    #drawing a circle on the image
    circlex=width/2
    circley=height/2
    radius=min(width,height)*0.3 
    draw.circle(circlex,circley,radius,border="blue")

#finding pixels that will lie outside the circle
    mat=( ((1:(height*width) %/% (height) )-(height-circley)*width/height)^2 +((1:(height*width) %% width) -circlex )^2 ) >= radius^2

  #converting pixels that are outside circle to black  
    x@data@values[mat, 3] <- 0
    Error in `[<-`(`*tmp*`, mat, 3, value = 0) : 
      (subscript) logical subscript too long
x@data@values[mat, 1] <- 0
x@data@values[mat, 2] <- 0

我查看了 str(x) 并得到了以下信息。我应该如何修改我的代码以使其如上所示运行? x@data@values 为空:(

> str(x)
Formal class 'RasterBrick' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr "results_circle.jpg"
  .. .. ..@ datanotation: chr "INT1U"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 3
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int [1:3] 1 1 1
  .. .. ..@ blockcols   : int [1:3] 2489 2489 2489
  .. .. ..@ driver      : chr "gdal"
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
  .. .. ..@ values    : logi[0 , 0 ] 
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi FALSE
  .. .. ..@ fromdisk  : logi TRUE
  .. .. ..@ nlayers   : int 3
  .. .. ..@ dropped   : NULL
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num [1:3] 0 0 0
  .. .. ..@ max       : num [1:3] 255 255 255
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr [1:3] "results_circle.1" "results_circle.2" "results_circle.3"
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num 0
  .. .. ..@ xmax: num 2489
  .. .. ..@ ymin: num 0
  .. .. ..@ ymax: num 2385
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 2489
  ..@ nrows   : int 2385
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA
  ..@ history : list()
  ..@ z       : list()

【问题讨论】:

    标签: r image raster


    【解决方案1】:

    您可以对RasterBrick 进行子集化并更改值。

    例如,使用随机图像和您的代码,您可以:

    library(raster)
    
    par(mfrow=c(1,2))
    r1 <- brick("image.jpg")#please load any jpeg image
    
    width=ncol(r1)
    height=nrow(r1)
    x <- crop(r1, extent(0,width,0,height))
    plotRGB(x)
    
    circlex=width/2
    circley=height/2
    radius=min(width,height)*0.3 
    draw.circle(circlex,circley,radius,border="blue")
    
    mat=( ((1:(height*width) %/% (height) )-(height-circley)*width/height)^2 +((1:(height*width) %% width) -circlex )^2 ) >= radius^2
    
    x[mat] <- 0
    plotRGB(x)
    

    得到这个结果:

    查看mat 公式,转换后的圆看起来更像一个椭圆。

    我试过了:

    mat =(rep(1:height,each=width)-(height-circley))^2+(rep(1:width,height) - circlex )^2 >=radius^2
    

    给出:

    【讨论】:

    • 请在我的问题中使用代码 - 第一个框并提供修改后的代码。我觉得我已经在使用光栅砖了
    • 你是,我确实使用了你的代码,只是添加了x[mat] &lt;- 0,我在答案中重新发布了你的代码。然后,我修改了您之前问题中的mat 代码以获得一个圆圈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多