Sunday, January 31, 2010

Sobel edge detection using Java Advanced Imaging

The Java Advanced Imaging API supports a number of interesting convolutions straight out of the box, and one of them is Sobel edge detection.

The Sobel edge-detection kernel comes in two varieties, corresponding to horizontal edge detection and vertical edge detection:
 1  2  1
0 0 0
-1 -2 -1

1 0 -1
2 0 -2
1 0 -1
You can combine them, and/or run them serially against an image, to detect all edges in an image. And that's what the following code example (in JavaScript) does. You can run the following script against an image of your choice using the ImageMunger app I wrote about a few days ago. Be sure the Java Advanced Imaging JARs are in your classpath.
/* Sobel.js
* Kas Thomas
* 31 January 2010
* Public domain.
* An edge-detection routine using
* Java Advanced Imaging.
* Requires Java Advanced Imaging library:
* Run this file using ImageMunger:

jai =;

pb = new );

// ImageMunger puts "Image" in global scope:
pb.addSource( Image );
pb.add( sobelH );
pb.add( sobelV );

renderedOp = jai.JAI.create( "gradientmagnitude", pb );
var image = renderedOp.getRendering().getAsBufferedImage();
Panel.setImage( invertImage( image ) );

// take BufferedImage as arg; flip all bits in all pixels
function invertImage( image ) {

var w = image.getWidth();
var h = image.getHeight();
var pixels = image.getRGB( 0,0, w,h, null, 0,w );

for ( var i = 0; i < pixels.length; i++ )
pixels[ i ] =~ pixels[ i ]; // flip pixel bits

image.setRGB( 0,0, w,h, pixels, 0, w );
return image;

If you run the Sobel operation by itself, you get a "negative" image, like so:

If you want the inverted version of this image (see example further above), you need to invert the individual pixels of the image. The super-fast way to do it is with a lookup table, but you can also do the pixel inversion manually, in a loop, which is what I've done (for illustrative purposes). In JavaScript the pixel-inversion loop adds about one second of processing time for a 600 x 400 image. The Sobel filter by itself takes around a half a second.

Sobel tends to be very sensitive to noise (it will feature-enhance specks and JPEG artifacts), so it often helps to smooth an image, first, with a blurring operation, prior to applying Sobel.

Future projects:
  • Write a "tunable" version of Sobel that can detect soft or hard edges, according to a tuning parameter.
  • Write a version of Sobel that's tunable by color (viz., detecting just blue edges, or just black edges, or just medium-grey edges).


  1. Good information about sobel edge detection using java advanced imaging. Such a useful and very informative article. Keep blogging and thanks for sharing.

    clipping path

  2. Can't get the first 3 lines to work... ¿?

  3. Anonymous7:47 AM

    After boosting their organisation for their customers proceeded service. dothesurvey An internet-connected gadget like a computer system, customer survey how the experience of the customer enter survey to win you instantly go into the drawing as well as customer feedback Currently you should go into the Store Number.

  4. Whatever about this wood router table Router Table Reviews is tailored toward accuracy, here dependability, and also accuracy.

  5. Really loved reading your blog. It was very well authored and easy to understand.
    Website Development Company

  6. Being a blogger, I appreciate the effort you put into this. Thanks for the great information you shared. Be sure to check out my article about team building. I think it might interest you. Read on and share your comments.


Add a comment. Registration required because trolls.