首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R砖提取色通道

R砖提取色通道
EN

Stack Overflow用户
提问于 2015-07-30 20:20:03
回答 1查看 171关注 0票数 1

我的问题是基于my earlier question。该解决方案可以很好地处理给定的示例(rlogo)。但是,当我加载JPEG图像时,我会得到一个错误,如下所示。

代码语言:javascript
复制
   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@value为空:(

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-03 12:09:11

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

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

代码语言:javascript
复制
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公式,这个圆看起来更像一个椭圆。

我试过:

代码语言:javascript
复制
mat =(rep(1:height,each=width)-(height-circley))^2+(rep(1:width,height) - circlex )^2 >=radius^2

这意味着:

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

https://stackoverflow.com/questions/31733283

复制
相关文章

相似问题

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