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.
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 :)
Labels:Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment