Thursday, June 13, 2013
Today I am going to show you how you can perform edge detection on any image using Java. Here we will use the convolution operator to perform this. But for this we need a kernel to perform this operation. But before that we will first give a breif description about this.
ConvolveOp class : ConvolveOp is one of the most versatile image operators in the 2D API. It represents a spatial convolution, which is a fancy mathematical term that means that the color of each destination pixel is determined by combining the colors of the corresponding source pixel and its neighbors. A convolution operator can be used to blur or sharpen an image, among other things.
Kernel class : The convolution operator uses a kernel to specify how a source pixel and its neighbors are combined. A kernel is simply a matrix, where the center of the matrix represents the source pixel and the other elements correspond to the neighboring source pixels. The destination color is calculated by multiplying each pixel color by its corresponding kernel coefficient and adding the results together.
How the operation will be performed - We will first read an image using read() method of ImageIO as BufferedImage. Next pass this to a function edgeDetect() where the operation will be carried out. Here we will first create a 3x3 array for kernel whose values add upto less than 1, which means that the image that will be produced will be darker. Next we will create the convolution operator creating ConvolveOp instance using the Kernel. Next we will call filter() method to perform the operation.

Screenshots of the images
Original Image
Image after Edge Detection

--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;

import javax.imageio.ImageIO;


public class EdgeDetection {
    public BufferedImage detectEdge(BufferedImage src){
       //kernel for edge detection adding upto less than 1
       float edgeArr[]={0,-1,0,-1,4,-1,0,-1,0};
       //creating the convolution operator
       ConvolveOp edgeOp=new ConvolveOp(new Kernel(3,3,edgeArr),ConvolveOp.EDGE_NO_OP,null);
   
       return edgeOp.filter(src, null);  //operating on image
    }
    
    public static void main(String[] args)throws Exception {
       EdgeDetection obj=new EdgeDetection();
       BufferedImage src=ImageIO.read(new File("src_edge.jpg")),
       dest=obj.detectEdge(src);  //edge detection
       ImageIO.write(dest, "jpeg",new File("dest_edge.jpg"));
    }
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
Hope this helps. Keep coding :)

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.