<< Chapter < Page | Chapter >> Page > |
// smoothed_glass
// smoothing filter, adapted from REAS:// http://processing.org/learning/topics/blur.html
size(210, 170);PImage a; // Declare variable "a" of type PImage
a = loadImage("vetro.jpg"); // Load the images into the programimage(a, 0, 0); // Displays the image from point (0,0)// corrupt the central strip of the image with random noise
float noiseAmp = 0.2;loadPixels();
for(int i=0; i<height; i++) {
for(int j=width/4; j<width*3/4; j++) {
int rdm = constrain((int)(noiseAmp*random(-255, 255) +red(pixels[i*width + j])), 0, 255);pixels[i*width + j] = color(rdm, rdm, rdm);}
}updatePixels();int n2 = 3/2;
int m2 = 3/2;float val = 1.0/9.0;
int[][] output = new int[width][height];float[][]kernel = { {val, val, val},
{val, val, val},{val, val, val} };// Convolve the image
for(int y=0; y<height; y++) {
for(int x=0; x<width/2; x++) {
float sum = 0;for(int k=-n2; k<=n2; k++) {
for(int j=-m2; j<=m2; j++) {
// Reflect x-j to not exceed array boundaryint xp = x-j;
int yp = y-k;if (xp<0) {
xp = xp + width;} else if (x-j>= width) {
xp = xp - width;}
// Reflect y-k to not exceed array boundaryif (yp<0) {
yp = yp + height;} else if (yp>= height) {
yp = yp - height;}
sum = sum + kernel[j+m2][k+n2] * red(get(xp, yp));}
}output[x][y]= int(sum);
}}// Display the result of the convolution
// by copying new data into the pixel bufferloadPixels();
for(int i=0; i<height; i++) {
for(int j=0; j<width/2; j++) {
pixels[i*width + j]=
color(output[j][i], output[j][i], output[j][i]);}
}updatePixels();
For the purpose of smoothing, it is common to create a convolution mask by reading the values of aGaussian bell in two variables. A property of gaussian functions is that their Fourier transform is itself gaussian. Therefore, impulse response and frequency response have the same shape. However, the transform of a thin bell is a large bell, and vice versa. The larger the bell, the more evident thesmoothing effect will be, with consequential loss of details. In visual terms, a gaussian filter produces an effect similar to that of an opalescent glass superimposed over the image. An example of Gaussian bell is .
Conversely, if the purpose is to make the contours and salient tracts of an image more evident ( edge crispening or sharpening), we have to perform a high-pass filtering. Similarly to what we saw in [link] this can be done with a convolution matrix whose central value has opposite sign as compared tosurrounding values. For instance, the convolution matrix produces the effect of [link] .
A filter whose convolution mask is signal-dependent looses its characteristics of linearity. Median filters use themask to select a set of pixels of the input images, and replace the central pixel of the mask with the medianvalue of the selected set. Given a set of (odd) numbers, the median element of the set is the one that separates smaller elements from larger elements. A typical median filter mask is cross-shaped. For example, a mask can cover, when applied to a certain image point, the pixels with values , thus replacing the value with the mean value .
Notification Switch
Would you like to follow the 'Media processing in processing' conversation and receive update notifications?