As the comments indicate, we generate random noise and then take a
weighted average of the target pixels and the noise. The parameterqcontrols
the weight of the noise, with largerqvalues producing more blurring. The
random noise itself is a sample from U(0,1), the uniform distribution on the
interval (0,1). Note that the following is a matrix operation:newimg@grey <- (1-q)*img@grey + q*randomnoiseSo, let’s give it a try:> mtrush3 <- blurpart(mtrush1,84:163,135:177,0.65)
> plot(mtrush3)The result is shown in Figure 3-3.Figure 3-3: Mount Rushmore, with President Roosevelt blurred3.2.4 Filtering on Matrices............................................
Filtering can be done with matrices, just as with vectors. You must be careful
with the syntax, though. Let’s start with a simple example:>x
x
[1,] 1 2
[2,] 2 3
[3,] 3 4
> x[x[,2] >= 3,]
x
[1,] 2 3
[2,] 3 466 Chapter 3