library(stringr)
color9 <- c("yellow", "yellow","yellow",
"yellow","yellow","yellow",
"yellow","yellow","yellow")
color9
# 目前電腦認為它是什麼
class(color9)
plot(color9)
color9_lineRaster <- as.raster(color9)
# 電腦認為它是什麼?
class(color9_lineRaster)
plot(color9_lineRaster) # 是一條黃色的線
color9_line1_matrix <- matrix(color9, 9,1)
color9_line1_matrix
try(plot(color9_line1_matrix))
# 教電腦是有顏色的線
color9_line1_raster <- as.raster(color9_line1_matrix)
plot(color9_line1_raster)
color9_line2_raster <- as.raster(matrix(color9, 1, 9))
plot(color9_line2_raster)
color9sq <- c("yellow", "yellow","yellow",
"purple","purple","purple",
"aliceblue","aliceblue","aliceblue")
color9sq_matrix <- matrix(color9sq,3,3)
color9sq_matrix
color9sq_raster <- as.raster(color9sq_matrix)
plot(color9sq_raster)
library(magick)
## Linking to ImageMagick 6.9.11.32
## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
## Disabled features: fftw, ghostscript, x11
# windows user needs this step
download.file("https://www.dropbox.com/s/7ab61bq92s9yt4e/ntpu.png?dl=1",destfile = "./ntpu.png",mode = "wb")
# read original image file
ntpuLogo <- image_read("./ntpu.png")
# convert image into a matrix of color codes (raster matrix)
ntpuLogoRaster <- as.raster(ntpuLogo)
# plot raster image
plot(ntpuLogoRaster)
ntpuLogoRaster_redFrame <- ntpuLogoRaster
dim(ntpuLogoRaster_redFrame)
ntpuLogoRaster_redFrame[, c(1,300)] <- "red"
ntpuLogoRaster_redFrame[c(1,183), ] <- "red"
plot(ntpuLogoRaster_redFrame)
# ntpuLogoRaster_redFrame
ntpuLogoRaster_greySelect <- ntpuLogoRaster
ntpuLogoRaster_greySelect[105:145,55:95] <- "grey"
plot(ntpuLogoRaster_greySelect)
# ntpuLogoRaster_greySelect
indexFromMatrix <- str_which(color9sq_matrix, "yellow")
indexFromRaster <- str_which(color9sq_raster, "yellow")
color9sqRaster_colorChangeBasedOnMatrix <- color9sq_raster
color9sqRaster_colorChangeBasedOnMatrix[indexFromMatrix] <- "white"
plot(color9sqRaster_colorChangeBasedOnMatrix)
color9sqRaster_colorChangeBasedOnRaster <- color9sq_raster
color9sqRaster_colorChangeBasedOnRaster[indexFromRaster] <- "white"
plot(color9sqRaster_colorChangeBasedOnRaster)
使用str_which來對色顏文字進行處理時,物件必需是as.matrix(raster object)而非raster object本身,此時的index才會是正確的。
# shutdown all red
color9sq_raster3 <- str_replace(as.matrix(color9sq_raster2), "88", "00")
color9sq_raster3 <- as.raster(matrix(color9sq_raster3,3,3))
plot(color9sq_raster3)
ntpuLogoRaster_blue <- ntpuLogoRaster
{
# put your code inside here
whichIsNotTransparent <-
str_which(
as.matrix(ntpuLogoRaster_blue),
"transparent", negate=T
)
}
ntpuLogoRaster_blue[whichIsNotTransparent] <- "blue"
plot(ntpuLogoRaster_blue)
# whichIsNotTransparent