Skip to content

Instantly share code, notes, and snippets.

@hliang
Created March 17, 2017 06:05
Show Gist options
  • Save hliang/7c304be8f45c2885bb29551978ede1a0 to your computer and use it in GitHub Desktop.
Save hliang/7c304be8f45c2885bb29551978ede1a0 to your computer and use it in GitHub Desktop.
code snippet from The Art of R Programming: A Tour of Statistical Software Design
# a code snippet from the book:
# The Art of R Programming: A Tour of Statistical Software Design
# load library
library("pixmap")
# show mtrush
mtrush1 = read.pnm("path/to/mtrush1.pgm")
mtrush1
plot(mtrush1)
# blot out a specific area
mtrush2 = mtrush1
mtrush2@grey[84:163, 135:177] = 1
plot(mtrush2)
# add noise
blurpart = function(img, rows, cols, q) {
lrows = length(rows)
lcols = length(cols)
newimg = img
randomnoise = matrix(nrow=lrows, ncol=lcols, runif(lrows * lcols))
newimg@grey[rows, cols] = (1 - q) * img@grey[rows, cols] + q * randomnoise
return(newimg)
}
blurpart(mtrush1, 84:163, 135:177, 0.65)
mtrush3 = blurpart(mtrush2, 84:163, 135:177, 0.65)
plot(mtrush3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment