W&W
Image Processing in Java
Mansoura FCI
W&W
Why Image Processing ?
• Medical Application• Geographic area• Image analysis step• Space• …etc
W&W
What to Study in Image Processing?
• Image is never perfect• Noise, blue, distortion, color decay
• The amount of images generated each day is huge• NASA
W&W
Bitmap Representation
• Also called “raster or pixel maps” representation
• An image is broken up into a grid
100
50 pixel
Gray level
Original picture Digital imagef(x, y) I[r, c] or I[x, y]
x
y
W&W
Java Image Processing
2 classesImage:
• Abstract• gets instances of platform specific subclasses
obtained via methods such as getImage() in the Applet class.
• provides limited information about an image.
BufferedImage:• a non-abstract subclass of Image • provides much greater access to and control of
image data.
W&W
How is Image Represented in Java?
• not 2D array of colors for reasons of speed and space• BufferedImage ( optional to know its internal structure)
W&W
Raster – rectangular array of pixels and is
composed of:• Data Buffer - actual data• Sample Model - knows how
extract to interpret the data buffer
ColorModel – • contains methods for
converting the pixel data into color.
W&W
SampleModel (optional):
• The abstract SampleModel has several subclasses such as :
• PixelInterleavedSampleModel, • SinglePixelPackedSampleModel ,• MultiPixelPackedSampleModel • and BandedSampleModel that describe
particular packing arrangements.
Sample :The term sample refers to one of the values that describe a pixel.
• In RGB color space, for example, there are three samples – the red sample, the green sample, and the blue sample
Band :• The collection of values of a particular
sample for all the pixels is referred to as a band. That is, if you filtered an image to show only the red values, this would display the red band.
• Except for specialized cases, you don’t need to deal directly with the details of the internal structure of the class.
W&W
BufferedImage Types
Determined using an argument passed to the constructor of the class.
some of its types are:• TYPE_INT_ARGB:
• four 8-bits alpha, red, green, blue, packed in a 32-bit integer.• TYPE_INT_RGB:
• Similar with TYPE_INT_ARGB but without alpha component.• TYPE_BYTE_GRAY:
• An unsigned byte grayscale image.
• The alpha component determines how transparent is the image. For TYPE_INT_ARGB it can take the values from 0 (fully transparent) to 255 (fully opaque).
W&W
Java Imaging API
1-image handling2-Image processing
W&W
1-Image Handling
W&W
Image Loading
• The Java AWT core language classes originally provided for loading and display of images in GIF and JPEG encoding format.
• With Java 1.4 came access to PNG encoding • with Java 5.0(java 1.5) came the bitmap formats BMP
(bitmap) and WBMP (wireless Bitmap).
4 types of Methods (choose any):
• ImageObserver ( see reference stated later)• MediaTracker• ImageIcon• BufferedImage
W&W
Image Loading- MediaTracker
• simple and elegant way to monitor the image loading.
• track one or more images .
• Very useful ,specially for Applets
• Example...
int image_id = 1;img = getImage (getCodeBase (), "m20.gif");//For Applettracker = new MediaTracker (this);// Pass the image reference and image ID nummber.tracker.addImage (img, image_id);….
• To Track specific ID , use:• int status (int image-id, boolean load)
• wait for Image:• tracker.waitForID (ImageID);
W&W
Image Loading-ImageIcon
• Better way• ImageIcon img_icon = new ImageIcon (url);• Image image = img_icon.getImage ();
W&W
Image Loading- BufferedImage
• Using ImageIO class (from java 1.4)
• this is a much simpler and better
• public static BufferedImage read (File input)
• public static BufferedImage read (URL input)
• String[] formats=ImageIO.getReaderFormatNames ();//query //for supported formats
W&W
Display and Creation of Image
DisplayImage:• Use method drawImage on the
Graphics Object
Creating Image From scratch:Image image = createImage (width, height);Graphics g = image.getGraphics ();paint (g);//example , not always paint()
W&W
Note: Double Buffering
Problem : • When many Graphics commands are used
sequentially, some flicker can happen (specially in Animation)
• Solution steps (double buffering): 1. do all the current scene commands on a
HIDDEN image , 2. display it as a whole, 3. do that for next scene using another
image.
W&W
Double Buffering Examples
• Example1:BufferedImage buffered_image =new BufferedImage (width,
height, BufferedImage.TYPE_INT_RGB);Graphics g = buffered_image.getGraphics (); //Casting Must be done to have Graphics2Dpaint (g);
• Example2:
Graphics2D buf_graphics = buffered_img.createGraphics();buf_graphics .drawImage (image, 0, 0, null);//no observer
W&W
Image Saving
• With Java 1.4 came the javax.imageio package and sub-packages that allow for both reading and writing of JPEG and PNG images.
• With Java 5.0 came BMP/WBMP image reading and writing.
• GIF files can be read but not saved.
• The GIF encoder involves the patented LZW compression algorithm so GIF encoding was not included in the standard Java packages.
• You can use Third party library for GIF.
W&W
Image Saving Example
• Example:
BufferedImage bi = …;
File file = new File ("c:\images\anImage.jpg");
ImageIO.write (bi, "jpeg", file);
W&W
2-Image processing
W&W
introduction
Generally in Image processing we have 2 types of processing:• Spatial image processing • Frequency image processing (not mentioned here).
• Sublasses of the Java 2D BufferedImageOp interface define a number of image processing operations (filters) that can be applied to an image.
W&W
Transformations typesThe concrete subclasses of BufferedImageOP are:
1. AffineTransformOp: geometric transformation of coordinates. For example, translation, rotation,
scaling.
2. ConvolveOp: combine groups of pixel values to obtain a new pixel value. For example,
sharpening, edge detection, blurring.
3. ColorConvertOp: A pixel-by-pixel color conversion of the data in the source image. For example,
RGB to grayscale conversion.• RescaleOp:
change pixel values based on a linear equation. For example, color inversion, reddening, brightening, darkening.
1. LookupOp: change pixel values based on a table lookup. For example, color inversion,
reddening, brightening, darkening.
24
TranslationTranslation
y
x
x'x dxy'y dy
x '
y '
x
y
dxdy
25
ScalingScaling
y
x
x' sx xy' sy y
x '
y '
sx0
0
sy
x
y
26
RotationRotation
y
x
x'x cos( ) y sin( )
y'y sin( ) y cos( )
x '
y '
cos( )
sin( )
sin( )
cos( )
x
y
27
ReflectionReflection
y
x
1 0 0
0 1 0
0 0 1
x
y
1
28
ShearShear
y
x
1 shx 0
0 1 0
0 0 0
x
y
1
29
TransformationsTransformations
• Transformations can be concatenated:– C = Translate * Scale * Rotate * Shear
• Java Classes:– AffineTransform ( java.awt.geom )– AffineTransformOp ( java.awt.image )
W&W
AffineTransform
Example:public void translateTransform() {AffineTransform at = new AffineTransform();at.translate(100, 100);BufferedImageOp op = new AffineTransformOp (at,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);image = op.filter(image, null);}
Example2AffineTransform shearer = AffineTransform.getShearInstance(0.4, 0.0);AffineTransFormOp shear_op = new AffineTransformOp (shearer, interpolation);BufferedImage dest_img = shear_op.filter (source_img, null);
Interpolation: - to avoid holes in destination image , use inverse transform, the method of selecting which source pixel is interpolation
W&W
Convolution Processing
• A convolution operator calculates the value of each pixel in the destination image, based on the value of the pixel in the original (source) pixel and its neighbours.
• The matrix defining this transformation is called kernel. It specifies the weights of the values of the neighbor pixels and the source pixel to obtain the value of the destination pixel.
• The convolution is calculated by applying the kernel to every pixel in the source image.
• The kernel must have an odd number of rows and columns. Typically it is a 3x3 matrix.
W&W
How to Address Pixels of an Image?
• Neighbors of a pixel• 4-neighbors of (i, j)• 8-neighbors of (i, j)
j
(i, j) (i, j+1)(i, j-1)
(i-1, j) (i-1, j+1)
(i+1, j+1)(i+1, j)
(i-1, j-1)
(i+1, j-1)i
(row)
(column)
W&W
Examplekernel Source Image
A sample steps Resulting Image
W&W
Edge Detection Example
• Ex1. Edge Detection:float edge_mat = {0.0, -1.0, 0.0,
-1.0, 4.0, -1.0,0.0, -1.0, 0.0};
ConvoleOp edge_finder_op =new ConvoleOp (new Kernel(3,3,edge_mat),ConvoleOp.EDGE_NO_OP, null);
BufferedImage edge_img = edge_finder_op.filter (an_image,null);
• See demo
W&W
Rescaling
• The RescaleOp filter applies a scaling factor and an offset to each color component so as to brighten or dim an image:• dest-color = source-color * scale-factor + offset;
• You could do this also with a lookup table but you need less code with this filter.
• For example:RescaleOp brighten_op = new RescaleOp (2.0f, 32f, null);BufferedImage dest_image = brighten_op.filter(source_image, null);
• Here each color component of each pixel is multiplied by 2.0 and then added to 32.
• If the value exceeds the maximum for that component, the maximum is used.
W&W
Color spacing
• The ColorConvertOp filter changes an image from one color space to another.
• A common requirement in image processing is to change to gray scale:
ColorSpace gray_space = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp convert_to_gray_op = new ColorConvertOp(gray_space, null);
BufferedImage gray_img = convert_to_gray_op.filter(source_image, null);
• The java.awt.color.ColorSpace class offers a number of options including TYPE_CMYK and TYPE_HSV.
W&W
Custom filters
• Modify kernels:
Sharpening Blurring
See demo…
W&W
Funny Example
• public final BufferedImage filter (BufferedImage source_img,• BufferedImage dest_img) {• // If no destination image provided, make one of same• // form as source•• int width = source_img.getWidth ();• int height= source_img.getHeight ();• for (int y=0; y < height; y++) {• for (int x=0; x < width; x++) {• int pixel = source_img.getRGB (x,y);• // Get the component colors• int red = (pixel >> 16) & 0xff;• int green = (pixel >> 8) & 0xff;• int blue = pixel & 0xff;• // Rotate the values• int tmp = blue;• blue = green;• green = red;• red = tmp;• // Put new value into corresponding pixel of• // destination image;• pixel = (255 << 24) | (red << 16) | (green << 8) |• blue;• dest_img.setRGB (x,y,pixel);• }• }• return dest_img;• } // filter
W&W
input
W&W
output
W&W
References
• Ref• Using Rasters for Image Processing, Part 1,by Anghel Leonard , http://
javaboutique.internet.com/tutorials/rasters/• JavaTech:An Introduction to Scientific and Technical Computing with Java ,Clark
S, indsey, Johnny S. Tolliver,and Thomas Lindblad , cambridge university press 2005 - chapter 11
• An Introduction to Image Manipulation with Java,Dr Dimitris C. Dracopoulos• Programmer’s Guide to the Java 2D™ API- Enhanced Graphics and Imaging for
Java• JavaTM 2 SDK, Standard Edition, 1.4 Version ,April 24, 2001• Digital Image Processing course - 2005,Mansoura FCI ,Dr-Ahmed Tolba
•Java 101:Imaging & Java, Ryan Cuprak