Exploring Image Processing

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

278 εμφανίσεις

January 2006


Daniel Bryant

1

Image Processing in Java:




A Technical Guide

Daniel Bryant

Dept. of Computing

University of Surrey


d.bryant@surrey.ac.uk

January 2006

Daniel Bryant

2

The Purpose of the Tutorial Labs


These labs are designed to help
you!




More information on the technical (Java) concepts that we will
be looking at in the regular labs



Introduce object
-
orientated/Java techniques



I can also provide help with any problems from the regular
labs or coursework



If you want information about a specific topic/concept within
Java or have any comments please let me know!



January 2006

Daniel Bryant

3

Resources


Please download a copy of these slides from the module
website



An excellent book to learn Java from the beginning is


Head First Java (Second Edition)

by Kathy Sierra and Bert
Bates



The Head First books contain a unique style (with lots of
pictures and funny techniques), but they make learning a lot
more enjoyable and really do work!


January 2006

Daniel Bryant

4

Resources


You can download Java and NetBeans free for home use at
http://java.sun.com/javase/downloads/index_jdk5.jsp


Download Java 5.0
and NetBeans 5.5
(the newest version
of NetBeans)

January 2006

Daniel Bryant

5

Today’s Lab


Running code


Arguments


Working directory




Coursework hints


Class variables


Methods (in particular for MirrorXReflection)


January 2006

Daniel Bryant

6

What happens when you specify
an image name in the arguments?


Arguments are essentially the
parameters that are passed
from the operating system into
the Java Application that you
are running (your Main class).
In this example it is
ImageViewer.



NetBeans allows us to specify
arguments in an easy way. If
we didn’t have NetBeans we
would have to invoke our Java
application at the command
line like this:


java ImageViewer
Argument1 Argument2 Argument3

N.B. Remember regardless of whether you use NetBeans or the command line the arguments
must separated by spaces (not commas) and try not to use hyphens in the arguments. You would
also have to set your classpath to include the two library JAR files

January 2006

Daniel Bryant

7

The Working Directory


The working directory is the location where the Java Virtual
Machine (JVM) will by default load or save any input/output
from your application



We have seen in the first lab that the argument name
‘mattgrey.jpg’ or ‘l1.jpg’ was the name of an image located in the
working directory (the
Images
directory)



You can of course navigate through a file system in Java (and
you are not restricted to doing everything in one directory), but
we will cover this in a later lab

January 2006

Daniel Bryant

8

Processing the Arguments


So, how is the specified image name argument transformed
into a representation of the image in our application?



Remember back to last semester


when you run a Java
application, which method is run first?



The answer is the main method, which always has a method
signature like this:



public static void main(String[] argv)





January 2006

Daniel Bryant

9

Processing the Arguments


Hopefully you can remember what the
public static void

part
means, but the main item of interest here is the
String[] argv



This creates an array of Strings that contain all of the arguments
specified at the command line or in NetBeans in the order they were
supplied.



For example if we supplied the arguments “matthew2.jpg output.txt”
then the first position in the String array (argv[0]) would contain
“matthew2.jpg” and the second (argv[1]) would contain “output.txt”.



This is equivalent to the statements:


argv[0] = “matthew2.jpg”;


argv[1] = “output.jpg”;


January 2006

Daniel Bryant

10

The Examples


The remaining slides assume that you have set up your
NetBeans environment correctly and have loaded in a project
that:


Contains the two JAR file Libraries specified in the first
regular lab


Includes the Chap05 directories



Remember in NetBeans the top toolbar contains useful icons:


Run your Main Class

Build and Clean

Save all files

January 2006

Daniel Bryant

11

The First Coursework


I would recommend creating a new Java class for each image
operation that you have to implement



So, for the ShrinkBySkipping operation I would create a new
class called
Shrink

and copy the
MirrorXReflection

code into
this class



You will then have to alter the class name and the name of the
constructor to the same name as the new Java file you have
created (i.e. Shrink)



You will then have to modify the code as described over the
remainder of the slides

January 2006

Daniel Bryant

12

Coursework Hints



I thought it would be useful to examine the MirrorXReflection
class (which was adapted from Nick Efford’s Dither code) in
more detail to help you incorporate the image operations as
specified in the regular lab sheet




MirrorXReflection contains 2 class variables and 4 methods (a
constructor, a main method and two general methods)

January 2006

Daniel Bryant

13

Class Variables



Recall from last semester’s labs class variables represent
“state” in an object (such as a persons age or the current gear
in a car) and can be accessed by all methods in the class




Class variables can be marked private (only code in this class
can access the variable) or public (code from any
class/package can access the variable)


January 2006

Daniel Bryant

14

Class Variables


In the MirrorXReflection class the two class variables are


private BufferedImage sourceImage






private ImageView[] views


The type of the variable is
BufferedImage


an internal
representation of an image

The name of the variable

The type of the variable is an
array (indicated by [ ]) of
ImageView. More detail is
included later in the slides

January 2006

Daniel Bryant

15

Class Methods


The MirrorXReflection contains four methods
-



public MirrorXReflection(String imageFile)



throws IOException, ImageDecoderException





public void readImage(String filename)



throws IOException, ImageDecoderException




public BufferedImage xReflectionInPlace(BufferedImage image)




public static void main(String[] argv)

January 2006

Daniel Bryant

16

Anatomy of a Method

public BufferedImage xReflectionInPlace(BufferedImage image) { … }

Access modifier


public, private etc.
This determines if the method can be
called outside of class it belongs to

Return type


This is the type of the variable that will be returned to the calling code
when the method completes. This example shows that when a call to the
xReflectionInPlace method completes the statement that called the method will
have access to a new BufferedImage variable.

E.g.
BufferedImage myImage = readImage(fileName);

Parameters that are passed into the method. These
parameters must be specified when calling the method
E.g.
xReflectionInPlace(sourceImage);

You can include any number of parameters for a
method e.g.
public BufferedImage
average(BufferedImage image, int n)

must be
passed a BufferedImage and an int (such as 5) i.e.

BufferedImage image = average(image,5);

January 2006

Daniel Bryant

17

Anatomy of
another
Method

public void readImage(String filename)throws IOException, ImageDecoderException

Access modifier


public, private etc.
This determines if the method can be
called outside of class it belongs to

Return type


void means no variable is returned and
so when calling this method you use the call the
method without loading the result into a variable

readImage(filename);


and NOT
void voidValue = readImage(filename);

Parameters that are passed into the code in
the method. These parameters must be
specified when calling the method


E.g. readImage(“myImageFile.jpg”);

Which Exceptions are thrown. These
two specified Exceptions must be
caught when calling the method using
try/catch blocks like last semester

try { readImage(fileName);

}

catch(IOException ioe) { … }

catch(ImageDecoderException idx)
{… }

January 2006

Daniel Bryant

18

MirrorXReflection


public MirrorXReflection(String ImageFile)


This is the constructor method. Remember that the constructor
name must
match the name of the class exactly

and also have
no return type (i.e. not
public
void

MirrorXReflection()

or
public
String

MirrorXReflection()

)



The constructor method is called whenever a new object with
this class type is created using the statement




new MirrorXReflection(filename)




(This is located in the main method of our example)


January 2006

Daniel Bryant

19

Modifications Needed

public MirrorXReflection(String imageFile) throws IOException, ImageDecoderException {



super("xReflect: " + imageFile);


readImage(imageFile);


views = new ImageView[2];


views[0] = new ImageView(sourceImage);


views[1] = new ImageView(
xReflectionInPlace
(sourceImage));




JTabbedPane tabbedPane = new JTabbedPane();



tabbedPane.add(new JScrollPane(views[0]), "input");


tabbedPane.add(new JScrollPane(views[1]), "xReflect");




getContentPane().add(tabbedPane);


addWindowListener(new WindowMonitor());

}

You will need to change
the name of the method
being called
(highlighted) for each
image operation and you
may also need to pass
in different parameters
to this method (i.e.
shrink takes a
BufferedImage and an
int, such as 2)

Create a tabbed pane

and add the two ImageView variables
from the views array

January 2006

Daniel Bryant

20

ImageView


The ImageView class is included as part of the standard Java
libraries and is intended to act in a manner similar to the
image tag <img> in HTML



You can load a BufferedImage (named sourceImage in this
example) in an ImageView variable


ImageView view = new ImageView(sourceImage);



You can then add the ImageView variable to a window created
in Java, much like including the <img> tag in a piece of HTML
code.

January 2006

Daniel Bryant

21

readImage


public void readImage(String filename)


This method reads the image file (specified as a String
variable
filename

parameter) from the file system and
loads an internal representation of the image into the
BufferedImage sourceImage class variable



This method currently includes code that converts the
specified image into a grayscale picture. Can you identify
this code?


January 2006

Daniel Bryant

22

xReflectionInPlace


This is the method where the image processing takes place


Therefore when you create a new class for each image
operation (shrink, average, etc…) you will have to replace
this method



Be aware that each method for an operation may require
different parameters. For example, the average method
requires that you pass in an array of BufferedImages and not
just a single image


You will need to find out what the average operation does,
how many images you will need to pass in and how to
create an array (Hint look at how the ImageView array
named views is created and loaded in the constructor)

January 2006

Daniel Bryant

23


public static void main(String[] argv) {


if (argv.length > 0) {




try {


JFrame frame = new MirrorXReflection(argv[0]);




frame.pack();


frame.setVisible(true);


} catch (Exception e) {


System.err.println(e);


System.exit(0);


}


} else {


System.err.println("usage: java MirrorXReflectionion <imagefile> ");


System.exit(1);


}


}

main

Check if the
argv
array contains more than one element (i.e. the number
of arguments you have specified in NetBeans separated by a space)

Try

and create a
JFrame
(a
GUI window) and load a
new

MirrorXReflection (with
the argv[0] filename passed
as a parameter) into that
window

Catch
any
exceptions
(errors) and
print the error
to the
terminal. Then
exit the
application

If the
argv
array contains no elements print an error message to the
terminal and shutdown the application

January 2006

Daniel Bryant

24


public static void main(String[] argv) {


if (argv.length > 0) {




try {


JFrame frame = new MirrorXReflection(argv[0]);




frame.pack();


frame.setVisible(true);


} catch (Exception e) {


System.err.println(e);


System.exit(0);


}


} else {


System.err.println("usage: java MirrorXReflectionion <imagefile> ");


System.exit(1);


}


}

main


modifications needed

You will need to modify this statement to load the results of the correct image
operation into the JFrame variable

Remember you may also have to pass in additional arguments to certain
image operations (for example the extra int required for Shrink) and two or
more images for the average operation

January 2006

Daniel Bryant

25

The remainder of the lab



Have a go at the coursework…




Hopefully, you have everything you need to complete Shrink
and Enlarge




You will require slightly different techniques for Average and
Subtract due to processing multiple images

January 2006

Daniel Bryant

26

Extra Work


You could also combine all the image operations (i.e. methods
into a single class file)


To do this you will need to pass in appropriate arguments from
NetBeans that will enable you to do all the operations (Hint: you
may need to pass more than one image filename in the
arguments)



You could also ask the user to specify any values required
(such as the n value on the Shrink methods) using a
JOptionPane Dialogue Box like we used last semester