Processing Image Pixels, Applying Image Convolution in Java, Part 2 Anderson SC

Part 2 of this lesson teaches you how to design copying filters, smoothing filters, sharpening filters, 3D embossing filters, and edge detection filters, and how to apply those filters to images.

Local Companies

No 1 PC Repair
(843) 757-6933
Bluffton, SC
U S Consulting Co
(803) 366-5900
454 Anderson Rd S
Rock Hill, SC
It Resource Solutions Com
(803) 748-1282
1201 Main St
Columbia, SC
Computer Control & Integration Inc
(864) 458-7587
1200 Woodruff Rd
Greenville, SC
Blue Collar Objects
(843) 216-7550
401 Seacoast Pkwy
Mount Pleasant, SC
Universal Data Solutions
(843) 556-5565
1583 Savannah Hwy
Charleston, SC
Modus 21
(843) 958-8900
212 King St
Charleston, SC
Sibley & Associates
(864) 458-8427
1200 Woodruff Rd
Greenville, SC
Datacom
(803) 798-3901
652 Bush River Rd
Columbia, SC
James Gang Information Center
(803) 366-7630
594 Cayce Olin Dr
Rock Hill, SC

provided by: 
Originally published at Internet.com


Java Programming, Notes # 414 * Preface * Background Information * Discussion and Sample Code * Run the Programs * Summary * What's Next * References * Complete Program Listings -----------------------------------

Preface

Part of a series

This lesson is one in a series designed to teach you how to use Java to create special effects with images by directly manipulating the pixels in the images. The first lesson in the series was entitled Processing Image Pixels using Java, Getting Started. The previous lesson was Part 1 of this two-part lesson.

This is Part 2 of the two-part lesson. You are strongly encouraged to review the first part of this lesson entitled Processing Image Pixels, Applying Image Convolution in Java, Part 1 before continuing with this lesson.

The primary objective of this lesson is to teach you how to integrate much of what you have already learned about Digital Signal Processing (DSP) and Image Convolution into Java programs that can be used to experiment with, and to understand the effects of a wide variety of image-convolution operations.

Part 1 of this lesson showed you how to design copying filters, smoothing filters, sharpening filters, 3D embossing filters, and edge detection filters, and how to apply those filters to images. The results of numerous experiments using filters of the types listed above were presented. This part of the lesson explains the code required to perform the experiments that were presented in Part 1.

You will need a driver program

The lesson entitled Processing Image Pixels Using Java: Controlling Contrast and Brightness provided and explained a class named ImgMod02a that makes it easy to: * Manipulate and modify the pixels that belong to an image. * Display the processed image along with the original image.

ImgMod02a serves as a driver that controls the execution of a second program that actually processes the pixels. It displays the original and processed images in the standard format shown in Figure 1.


Figure 1

Get the class and the interface

The image-processing programs that I will explain in this lesson run under the control of ImgMod02a. In order to compile and run the programs that I will provide in this lesson, you will need to go to the lessons entitled Processing Image Pixels Using Java: Controlling Contrast and Brightness and Processing Image Pixels using Java, Getting Started to get copies of the class named ImgMod02a and the interface named ImmgIntfc02

Viewing tip

You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different figures and listings while you are reading about them.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.

I particularly recommend that you study the lessons referred to in the References section of this lesson.

Background Information

A three-dimensional array of pixel data as type int

The driver program named ImgMod02a: * Extracts the pixels from an image file. * Converts the pixel data to type int. * Stores the pixel data in a three-dimensional array of type int that is well suited for processing. * Passes the three-dimensional array object's reference to a method in an object instantiated from an image-processing class. * Receives a reference to a three-dimensional array object containing processed pixel data from the image-processing method. * Displays the original image and the processed image in a stacked display as shown in Figure 1. * Makes it possible for the user to provide new input data to the image-processing method, invoking the image-processing method repeatedly in order to create new displays showing the newly-processed image along with the original image.

The manner in which that is accomplished was explained in earlier lessons.

A grid of colored pixels

Each three-dimensional array object represents one image consisting of a grid of colored pixels. The pixels in the grid are arranged in rows and columns when they are rendered. One of the dimensions of the array represents rows. A second dimension represents columns. The third dimension represents the color (and transparency) of the pixels.

Convolution in one dimension

The earlier lesson entitled Convolution and Frequency Filtering in Java taught you about performing convolution in one dimension. In that lesson, I showed you how to apply a convolution filter to a sampled time series in one dimension. As you may recall, the mathematical process in one dimension involves the following steps: * Register the n-point convolution filter with the first n samples in the time series. * Compute an output value, which is the sum of the products of the convolution filter coefficient values and the corresponding time series values. * Optionally divide the sum of products output value by the number of filter coefficients. * Move the convolution filter one step forward, registering it with the next n samples in the time series and compute the next output value as a sum of products. * Repeat this process until all samples in the time series have been processed.

Complete Program Listings

Complete listings of the programs discussed in this lesson are provided in this section.

A disclaimer

The programs that I am providing and explaining in this series of lessons are not intended to be used for high-volume production work. Numerous integrated image-processing programs are available for that purpose. In addition, the Java Advanced Imaging (JAI) API has a number of built-in special effects if you prefer to write your own production image-processing programs using Java.

The programs that I am providing in this series are intended to make it easier for you to develop and experiment with image-processing algorithms and to gain a better understanding of how they work, and why they do what they do.

Listing 28 /* File Graph08.java Copyright 2005, R.G.Baldwin This is an updated version of Graph03 to allow the user to plot up to eight functions instead of only 5. GraphIntfc08 is a corresponding update to the earlier interface named GraphIntfc01. Graph03 and GraphIntfc01 were explained in lesson 1488 entitled Convolution and Matched Filtering in Java. This program is very similar to Graph01 except that it has been modified to allow the user to manually resize and replot the frame. Note:  This program requires access to the interface named GraphIntfc08. This is a plotting program.  It is designed to access a class file, which implements GraphIntfc08, and to plot up to eight functions defined in that class file. The plotting surface is divided into the required number of equally sized plotting areas, and one function is plotted on Cartesian coordinates in each area. The methods corresponding to the functions are named f1, f2, f3, f4, f5, f6, f7, and f8. The class containing the functions must also define a method named getNmbr(), which takes no parameters and returns the number of functions to be plotted.  If this method returns a value greater than 8, a NoSuchMethodException will be thrown. Note that the constructor for the class that implements GraphIntfc08 must not require any parameters due to the use of the newInstance method of the Class class to instantiate an object of that class. If the number of functions is less than 8, then the absent method names must begin with f8 and work down toward f1.  For example, if the number of functions is 3, then the program will expect to call methods named f1, f2, and f3. It is OK for the absent methods to be defined in the class. They simply won't be invoked.  In fact, because they are declared in the interface, they must be defined as dummy methods in the class that implements the interface. The plotting areas have alternating white and gray backgrounds to make them easy to separate visually. All curves are plotted in black.  A Cartesian coordinate system with axes, tic marks, and labels is drawn in red in each plotting area. The Cartesian coordinate system in each plotting area has the same horizontal and vertical scale, as well as the same tic marks and labels on the axes. The labels displayed on the axes, correspond to the values of the extreme edges of the plotting area. The program also compiles a sample class named junk, which contains eight methods and the method named getNmbr.  This makes it easy to compile and test this program in a stand-alone mode. At runtime, the name of the class that implements the GraphIntfc08 interface must be provided as a command-line parameter.  If this parameter is missing, the program instantiates an object from the internal class named junk and plots the data provided by that class.  Thus, you can testt the program by running it with no command-line parameter. This program provides the following text fields for user input, along with a button labeled Graph.  This allows the user to adjust the parameters and replot the graph as many times with as many plotting scales as needed: xMin = minimum x-axis value xMax = maximum x-axis value yMin = minimum y-axis value yMax = maximum y-axis value xTicInt = tic interval on x-axis yTicInt = tic interval on y-axis xCalcInc = calculation interval The user can modify any of these parameters and then click the Graph button to cause the eight functions to be re-plotted according to the new parameters. Whenever the Graph button is clicked, the event handler instantiates a new object of the class that implements the GraphIntfc08 interface.  Depending on the nature of that class, this may be redundant in some cases.  However, it is useful in those cases where it is necessary to refresh the values of instance variables defined in the class (such as a counter, for example). This program uses constants that were first defined in the Color class of v1.4.0.  Therefore, the program requires v1.4.0 or later to compile and run correctly. Tested using J2SE 5.0 under WinXP. **********************************************************/ import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.border.*; class Graph08{   public static void main(String[] args)                               throws NoSuchMethodException,                                     ClassNotFoundException,                                     InstantiationException,                                     IllegalAccessException{     if(args.length == 1){       //pass command-line paramater       new GUI(args[0]);     }else{       //no command-line parameter given       new GUI(null);     }//end else   }// end main }//end class Graph08 definition //=======================================================// class GUI extends JFrame implements ActionListener{   //Define plotting parameters and their default values.   double xMin = 0.0;   double xMax = 400.0;   double yMin = -100.0;   double yMax = 100.0;   //Tic mark intervals   double xTicInt = 20.0;   double yTicInt = 20.0;   //Tic mark lengths.  If too small on x-axis, a default   // value is used later.   double xTicLen = (yMax-yMin)/50;   double yTicLen = (xMax-xMin)/50;   //Calculation interval along x-axis   double xCalcInc = 1.0;   //Text fields for plotting parameters   JTextField xMinTxt = new JTextField("" + xMin);   JTextField xMaxTxt = new JTextField("" + xMax);   JTextField yMinTxt = new JTextField("" + yMin);   JTextField yMaxTxt = new JTextField("" + yMax);   JTextField xTicIntTxt = new JTextField("" + xTicInt);   JTextField yTicIntTxt = new JTextField("" + yTicInt);   JTextField xCalcIncTxt = new JTextField("" + xCalcInc);   //Panels to contain a label and a text

Author: Richard G. Baldwin

Read article at Internet.com site

Featured Local Company

Powerserve International

706-826-1506
929 Broad Street
Augusta, GA
http://www.powerserve.net