R 砖提取颜色通道

R brick extracting color channels

我的问题是基于 。该解决方案适用于给定的示例(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()

您可以对 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

给出: