<< Chapter < Page Chapter >> Page >

Smoothing

// 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 1 273 1 4 7 4 1 4 16 26 16 4 7 26 41 26 7 4 16 26 16 4 1 4 7 4 1 .

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 -1 -1 -1 -1 9 -1 -1 -1 -1 produces the effect of [link] .

Edge crispening

Non-linear filtering: median filter

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 N (odd) numbers, the median element of the set is the one that separates N 1 2 smaller elements from N 1 2 larger elements. A typical median filter mask is cross-shaped. For example, a 3 3 mask can cover, when applied to a certain image point, the pixels with values x 4 x 7 99 12 x 9 x , thus replacing the value 99 with the mean value 9 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Media processing in processing. OpenStax CNX. Nov 10, 2010 Download for free at http://cnx.org/content/col10268/1.14
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Media processing in processing' conversation and receive update notifications?

Ask