Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Filtering and Transforming Digital Images

Filtering and Transforming Digital Images In this Issue Welcome to the Core Java Technologies Tech Tips for April 7, 2004. Here you\'ll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE).

Tutorial Details:

FILTERING AND TRANSFORMING DIGITAL IMAGES

The February 10, 2004 Tech Tip Styling Digital Images with ConvolveOp showed how to use java.awt.image.ConvolveOp to sharpen and blur images as well as darken and brighten images. ConvolveOp implements a convolution, which is the easiest way to blur and sharpen images. A convolution blurs images by averaging nearby pixels. It sharpens edges by highlighting differences between the colors of nearby pixels. But there are other filters and transforms you can apply to an image to meet your needs. In this tip you will apply affine transformations such as a rotation and a scale. You will also apply filters that perform linear and non-linear brightening. You will also use a color filter to transform a color image to grayscale. Each of these filters and transforms are performed by classes in the java.awt.image package that implement the interface BufferedImageOp.

As was the case in the \"Styling Digital Images with ConvolveOp\" Tech Tip, you need a test image. In fact, you can reuse the image from that tip -- you can find the image here. In the code examples below, the image name is set to test.jpg. The image should be in the directory in which you run these examples.

To begin, let\'s create a framework for these examples. The program below, ExampleFramework, is similar to the example code used in the ConvolveOp Tech Tip. As in the code examples for the earlier tip, ExampleFramework uses a simple JFrame that holds the image, and uses a JSlider. However to make it easy to plug in a variety of filters and transforms, the code related to the specific effect has been removed, and accessor methods have been inserted. For consistency, the processed image will be referred to as filteredImage, whether the change is the result of applying a filter or a transform.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;

public class ExampleFramework extends JPanel {
private BufferedImage originalImage;
private BufferedImage filteredImage;
private JSlider slide = new JSlider(1, 50, 25);
private int height, width;

ExampleFramework(String titlebar) {
createBufferedImages();
setUpJFrame(titlebar);
}

private void createBufferedImages() {
Image image =
new ImageIcon(\"test.jpg\").getImage();
height = image.getHeight(null);
width = image.getWidth(null);
originalImage =
new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
filteredImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = originalImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
}

private void setUpJFrame(String titlebar) {
JFrame frame = new JFrame(titlebar);
frame.setSize(filteredImage.getWidth(),
filteredImage.getHeight());
frame.getContentPane().setLayout
(new BorderLayout());
frame.getContentPane().add
(this, BorderLayout.CENTER);
frame.getContentPane().add
(slide, BorderLayout.SOUTH);
frame.setResizable(false);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

// accessor methods for plugging in filters

public void setChangeListener(ChangeListener cl) {
slide.addChangeListener(cl);
}

public void setFilteredImage
(BufferedImage image) {
filteredImage = image;
repaint();
}

public int getSlideValue() {
return slide.getValue();
}

public BufferedImage getOriginal() {
return originalImage;
}

public void paintComponent(Graphics g) {
g.clearRect(0, 0, width, height);
g.drawImage(filteredImage, 0, 0, this);
}
}

As a first example, let\'s use an affine transformation to zoom the image in and out. You might want to refer to the September 9, 2003 Tech Tip Understanding AffineTransform for some of the mathematical details behind such transforms. You will create a new instance of an AffineTransformOp, and pass in two arguments. You will then produce the new image from the old image using the AffineTransformOp that you created. Finally, you will tell the ExampleFramework object to update the displayed image.


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Filtering and Transforming Digital Images

View Tutorial:
Filtering and Transforming Digital Images

Related Tutorials:

To jar or not to jar? - JavaWorld - July 1998
To jar or not to jar? - JavaWorld - July 1998
 
Image processing with Java 2D - JavaWorld - September 1998
Image processing with Java 2D - JavaWorld - September 1998
 
Jini: New technology for a networked world - JavaWorld June 1999
Jini: New technology for a networked world - JavaWorld June 1999
 
Apple announces Java 2 plans at MacWorld
Apple announces Java 2 plans at MacWorld
 
Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001
Jato: The new kid on the open source block, Part 2 - JavaWorld April 2001
 
Bridge the gap between Java and Twain
Bridge the gap between Java and Twain
 
Secure Web services
Secure Web services
 
Navigate through virtual worlds using Java 3D
Navigate through virtual worlds using Java 3D
 
SAAJ: No strings attached
SAAJ: No strings attached
 
Ganymede
A log4j plugin to Eclipse that works similar to chainsaw (SocketServer). Includes color, filtering, detailed information, and saves settings.
 
Filtering and Transforming Digital Images
Filtering and Transforming Digital Images In this Issue Welcome to the Core Java Technologies Tech Tips for April 7, 2004. Here you\'ll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE).
 
JSANE - Image Acquisition from Digital Cameras and Scanners
JSANE - Image Acquisition from Digital Cameras and Scanners Image Acquisition from Digital Cameras and Scanners with Java on Mac/Linux/Solaris/Unix/BSD, etc. SANE is the de facto standard to access scanners/cameras on AIX, BeOS, Darwin, FreeBSD, HP-
 
HeapAnalyzer
What is HeapAnalyzer? HeapAnalyzer allows the finding of a possible JavaTM heap leak area through its heuristic search engine and analysis of the Java heap dump in Java applications. Java heap areas define objects, arrays, and classes.
 
Java Tech: Acquire Images with TWAIN and SANE, Part 1
Scanners, digital cameras, and other image-acquisition devices are part of the computing landscape. Despite their ubiquity, however, Java does not provide a standard API for interacting with these devices. And yet there certainly is a desire to have a sta
 
JTwain
JTwain will implement a Java interface to the the Win32 C DLL TWAIN acquire methods.
 
Lucene in Action
Lucene in Action Lucene is a gem in the open-source world--a highly scalable, fast search engine. It delivers performance and is disarmingly easy to use. Lucene in Action is the authoritative guide to Lucene. It describes how to index your data, includin
 
Digital Clock developed in java
This digital clock displays the local time in 24-hour format. Hours, minutes and seconds are displayed as 7-segment BCD-LED digits with segments tapered at both ends.
 
Project Pixory goes Open Source
A personal image server based on Java. Pixory has a pure web interface that allows you to manage and share photo albums over the wwww.
 
VolatileBufferedToolkitImage Strategies
Ever wondered what kind of image to use in your application? Or what method to use in creating it? This article attempts to address this challenging topic.
 
JPackIt JPackIt is a Java application for packaging a Java project into single executable package
Java Project in single jar, class or exe containing all java application resources and referenced libraries.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.