## Saturday, August 07, 2010

### A "Smart Sobel" image filter

The original image ("Lena"), left, and the same image transformed via Smart Sobel (right).

Last time, I talked about how to implement Smart Blur. The latter gets its "smartness" from the fact that the blur effect is applied preferentially to less-noisy parts of the image. The same tactic can be used with other filter effects as well. Take the Sobel kernel, for example:
`float [] kernel = {2, 1, 0,1, 0,-1,0,-1,-2};`
Convolving an image with this kernel tends to produce an image in which edges (only) have been preserved, in rather harsh fashion, as seen here:

Ordinary Sobel transformation produces a rather harsh result.

This is an effect whose harshness begs to be tamed by the "smart" approach. With a "smart Sobel" filter, we would apply maximum Sobel effect to the least-noisy parts of the image and no Sobel filtering to the "busiest" parts of the image, and interpolate between the two extremes for other parts of the image.

That's easy to do with just some trivial modifications to the Smart Blur code I gave last time. Without further ado, here is the code for the Smart Sobel filter:

`import java.awt.image.Kernel;import java.awt.image.BufferedImage;import java.awt.image.ConvolveOp;import java.awt.Graphics;public class SmartSobelFilter {   double SENSITIVITY = 21;   int REGION_SIZE = 5;   float [] kernelArray = {           2, 1, 0,           1, 0, -1,           0, -1,-2   };   Kernel kernel = new Kernel( 3,3,  kernelArray  );   float [] normalizeKernel( float [] ar ) {           int n = 0;           for (int i = 0; i < ar.length; i++)           n += ar[i];           for (int i = 0; i < ar.length; i++)           ar[i] /= n;           return ar;   }   public double lerp( double a,double b, double amt) {           return a + amt * ( b - a );   }   public double getLerpAmount( double a, double cutoff ) {           if ( a > cutoff )           return 1.0;           return a / cutoff;   }   public double rmsError( int [] pixels ) {           double ave = 0;           for ( int i = 0; i < pixels.length; i++ )           ave += ( pixels[ i ] >> 8 ) & 255;           ave /= pixels.length;           double diff = 0;           double accumulator = 0;           for ( int i = 0; i < pixels.length; i++ ) {                   diff = ( ( pixels[ i ] >> 8 ) & 255 ) - ave;                   diff *= diff;                   accumulator += diff;           }           double rms = accumulator / pixels.length;           rms = Math.sqrt( rms );           return rms;   }   int [] getSample( BufferedImage image, int x, int y, int size ) {           int [] pixels = {};           try {                   BufferedImage subimage = image.getSubimage( x,y, size, size );                   pixels = subimage.getRGB( 0,0,size,size,null,0,size );           }           catch( Exception e ) {                   // will arrive here if we requested                   // pixels outside the image bounds           }           return pixels;   }   int lerpPixel( int oldpixel, int newpixel, double amt ) {           int oldRed = ( oldpixel >> 16 ) & 255;           int newRed = ( newpixel >> 16 ) & 255;           int red = (int) lerp( (double)oldRed, (double)newRed, amt ) & 255;           int oldGreen = ( oldpixel >> 8 ) & 255;           int newGreen = ( newpixel >> 8 ) & 255;           int green = (int) lerp( (double)oldGreen, (double)newGreen, amt ) & 255;           int oldBlue = oldpixel & 255;           int newBlue = newpixel & 255;           int blue = (int) lerp( (double)oldBlue, (double)newBlue, amt ) & 255;           return ( red << 16 ) | ( green << 8 ) | blue;   }   int [] blurImage( BufferedImage image,   int [] orig, int [] blur, double sensitivity ) {           int newPixel = 0;           double amt = 0;           int size = REGION_SIZE;           for ( int i = 0; i < orig.length; i++ ) {                   int w = image.getWidth();                   int [] pix = getSample( image, i % w, i / w, size );                   if ( pix.length == 0 )                   continue;                   amt = getLerpAmount ( rmsError( pix ), sensitivity );                   newPixel = lerpPixel( blur[ i ], orig[ i ],  amt );                   orig[ i ] = newPixel;           }           return orig;   }   public void invert( int [] pixels ) {           for (int i = 0; i < pixels.length; i++)           pixels[i] = ~pixels[i];   }   public BufferedImage filter( BufferedImage image ) {           ConvolveOp convolver = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,           null);           // clone image into target           BufferedImage target = new BufferedImage(image.getWidth(), image           .getHeight(), image.getType());           Graphics g = target.createGraphics();           g.drawImage(image, 0, 0, null);           g.dispose();           int w = target.getWidth();           int h = target.getHeight();           // get source pixels           int [] pixels = image.getRGB(0, 0, w, h, null, 0, w);           // blur the cloned image           target = convolver.filter(target, image);           // get the blurred pixels           int [] blurryPixels = target.getRGB(0, 0, w, h, null, 0, w);           invert( blurryPixels );           // go thru the image and interpolate values           pixels = blurImage(image, pixels, blurryPixels, SENSITIVITY);           // replace original pixels with new ones           image.setRGB(0, 0, w, h, pixels, 0, w);           return image;   }}`
To use the filter, instantiate it and then call the filter() method, passing a java.awt.image.BufferedImage. The method returns a transformed BufferedImage.

There are two knobs to tweak: SENSITIVITY and REGION_SIZE. The former affects how much interpolation happens between native pixels and transformed pixels; a larger value means a more extreme Sobel effect. The latter is the size of the "neighboring region" that will be analyzed for noisiness as we step through the image pixel by pixel. This parameter affects how "blocky" the final image looks.

Ideas for further development:
• Develop a "Smart Sharpen" filter
• Combine with a displacement filter for paintbrush effects
• Overlay (combine) the same image with copies of itself, transformed with various values for SENSITIVITY and REGION_SIZE, to reduce "blockiness"

#### 7 comments:

1. Hello Thomas,

Good job ! I re-implemented your "Smart Sobel Blur Filter" in C and the resulting pictures are very nice !!

I think you could remove the normalizeKernel() function from here, because it is not used (moreover, the sobel's kernel sum is zero using this function !).

And my question is : is this image processing method from you or did you see it somewhere else ? Is there a reference ?(Scientific article, book, ...) Also, is "Smart Sobel Blur Filter" the only name of this processing ?

Great thanks for your work !

1. Thanks for the comments. I came up with it on my own. Sobel is a well known algorithm. I just added a tweak.

2. We are the best writing company providing Literature Review Structure for high school students in every paper type. Our writers have years of experience in Buy Pre Written Research Papers for students globally.

3. https://community.ebay.com/t5/Shipping-Returns/%D8%B4%D8%B1%D9%83%D8%A7%D8%AA-%D9%86%D9%82%D9%84-%D8%B9%D9%81%D8%B4-%D9%88%D8%A7%D8%AB%D8%A7%D8%AB-%D8%A8%D8%A7%D9%84%D9%85%D9%85%D9%84%D9%83%D8%A9-%D8%A7%D9%84%D9%85%D8%AA%D8%AD%D8%AF%D8%A9/qaq-p/26768794#M143546
http://jumperads.unblog.fr/2017/03/22/%d8%a7%d9%81%d8%b6%d9%84-%d8%b4%d8%b1%d9%83%d8%a9-%d9%86%d9%82%d9%84-%d8%b9%d9%81%d8%b4-%d8%a7%d9%84%d9%82%d8%b5%d9%8a%d9%85/
http://jumperads.unblog.fr/2017/03/20/%d8%a7%d9%87%d9%85-%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d8%ae%d9%85%d9%8a%d8%b3-%d9%85%d8%b4%d9%8a%d8%b7/
http://jumperads.unblog.fr/2017/03/20/%d8%b4%d8%b1%d9%83%d8%a9-%d8%a7%d9%84%d9%85%d8%aa%d8%ad%d8%af%d8%a9-%d9%85%d9%86-%d8%a7%d9%87%d9%85-%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d9%85%d9%83/
http://jumperads.unblog.fr/2017/03/20/%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%a7%d8%ab%d8%a7%d8%ab-%d8%a8%d8%a7%d9%84%d8%b1%d9%8a%d8%a7%d8%b6-%d9%85%d9%88%d9%82%d8%b9-%d8%a7%d9%84%d9%85%d8%aa%d8%ad%d8%af%d8%a9/
http://jumperads.unblog.fr/2017/03/23/%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d8%ad%d8%a7%d8%a6%d9%84-2017/
http://jumperads.unblog.fr/2017/03/23/%d8%b4%d8%b1%d9%83%d8%a9-%d8%aa%d9%86%d8%b8%d9%8a%d9%81-%d9%85%d8%aa%d8%ae%d8%b5%d8%b5%d8%a9-%d9%85%d8%af%d9%8a%d9%86%d8%a9-%d8%a7%d9%84%d9%82%d8%b7%d9%8a%d9%81/
http://jumperads.unblog.fr/2017/03/23/%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d9%8a%d9%86%d8%a8%d8%b9/
http://jumperads.unblog.fr/

Add a comment. Registration required because trolls.