Arc
Arc

Reputation: 1700

Blurring part of an image

I was going through "The Art of R programming" and came across this piece:

# adds random noise to img, at the range rows,cols of img; img and the
# return value are both objects of class pixmap; the parameter q
# controls the weight of the noise, with the result being 1-q times the
# original image plus q times the random noise
blurpart <- function(img,rows,cols,q) {
  lrows <- length(rows)
  lcols <- length(cols)
  newimg <- img
  randomnoise <- matrix(nrow=lrows, ncol=ncols,runif(lrows*lcols))
  newimg@grey <- (1-q) * img@grey + q * randomnoise
  return(newimg)
}

My question is about the line:

newimg@grey <- (1-q) * img@grey + q * randomnoise

How does newimg@grey end up being of the same size as img@grey. Since randomnoise is smaller matrix, how is the newimg@grey part recognising what part of the image to blur.

I thought it should be something like:

newimg <- img
newimg@grey[rows,cols] <- (1-q) * img@grey[rows,cols] + q * randomnoise

Upvotes: 1

Views: 308

Answers (1)

Arc
Arc

Reputation: 1700

There seems to be a printing mistake in the book and I have verified that code in the book doesn't work even after get the typo right. I have sent the feedback to the the writer. The correct piece should be:

newimg <- img
newimg@grey[rows,cols] <- (1-q) * img@grey[rows,cols] + q * randomnoise

Upvotes: 2

Related Questions