image processing with java - 2d cont’d. overview an imaging model that supports the manipulation...

28
Image processing with Java - 2D cont’d

Upload: cora-haynes

Post on 02-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Image processing with Java - 2D

cont’d

Page 2: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Overview An imaging model that supports the manipulation of fixed-

resolution images stored in memory. A new Image class in the java.awt.image package,

BufferedImage, can be used to hold and to manipulate image data retrieved from a file or a URL.

For example: A BufferedImage can be used to implement double

buffering. The graphic elements are rendered off-screen to the BufferedImage and copied to the screen through a call to Graphics2D drawImage.

The classes BufferedImage and BufferedImageOp also enable us to perform a variety of image-filtering operations,

such as blur and sharpen.

Page 3: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Double Buffering We can use the images as offscreen drawing surfaces

according to storing them as pictures This allows us to render any image, including text and

graphics, to an offscreen buffer that we can display at a later time.

The advantage of doing this is that the images is seen only when it is complete

Drawing a complicated image could take several milliseconds or more

which can be seen by the user as flashing and flickeringThis flashing is distracting

causes the user to perceive his rendering as slower than actually is

Usage of an offscreen image to reduce flickers is called double buffering. Because:

the screen is considered a buffer for pixels, and the offscreen image is the second buffer,

where we can prepare pixels for display.

Page 4: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

How to draw an image?

The Graphics object is available to draw an Image via getgraphics() method.

Canvas c = new Canvas();Image test = c.createImage (200,100);Graphics gc = test. getGraphics ();gc.setColor (Color.red);gc.fillRect(0,0,200,100);

Once we have constructed and filled an offscreen image, it will still not be visible To actually display the image, call drawImage()

Page 5: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Creating an Image Object

Canvas c = new canvas();

Image test = c.createImage(200,100);

This creates an instance of Canvas Then calls the createImage() method to

actually make an Image object At this point the image is blank.

Page 6: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Creating a BufferedImage

Call the Component.createImage method; This returns a BufferedImage whose drawing

characteristics match those of the component used to create it

The created image is opaque, has the foreground and background colors of the Component,

We can't adjust the transparency of the image.

We could use this technique when we want to do double buffered drawing for animation in a component;

Page 7: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Using BufferedImages

The BufferedImage class is the main class supporting the immediate imaging mode.

It manages an image in memory, providing ways

to store pixel data, to interpret pixel data, and to render the pixel data to a Graphics or Graphics2D

context.

Page 8: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Example:

Creating a BufferedImage

Page 9: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

public Graphics2D createDemoGraphics2D(Graphics g) { Graphics2D g2 = null; int width = getSize(). width; int height = getSize(). height; if (offImg == null || offImg.getWidth() != width || offImg.getHeight() != height) { offImg = (BufferedImage) createImage (width, height); } if (offImg != null) { g2 = offImg.createGraphics(); g2.setBackground(getBackground()); } // .. clear canvas ..

g2.clearRect(0, 0, width, height); return g2; } We can also create a blank BufferedImage in memory using one of several

constructor methods provided

Page 10: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Drawing in an Offscreen Buffer

The BufferedImage class can be used to prepare graphic elements offscreen

then copy them to the screen.

This technique is especially useful when a graphic is complex or used repeatedly.

For example, if we want to display a complicated shape several times:

draw it once into an offscreen buffer, copy it to different locations in the window.

By drawing the shape once and copying it, we can display the graphics more quickly.

Page 11: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Drawing in an Offscreen Buffer cont’d

The java.awt package facilitates the use of offscreen buffers

Drawing an Image object implements as same way that we draw to a window.

All of the Java 2D API rendering features can be used when drawing to offscreen images.

Offscreen buffers are often used for animation.We can use an offscreen buffer to draw an object once and

then move it around in a window. We use an offscreen buffer to provide feedback as a user

moves a graphic using the mouse. Instead of redrawing the graphic at every mouse location, we

can draw the graphic once to an offscreen buffer, and then copy it to the mouse location as the user drags the mouse

Page 12: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

The program draw to an offscreen image and then copy that image into a window multiple times.

The last time the image is copied, it is transformed. Transforming the image instead of redrawing it with the

transformation might produce unsatisfactory results.

Page 13: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Creating an Offscreen Buffer The simplest way to create an image that we can

use as an offscreen buffer is to use the Component.createImage method.

By creating an image whose color space, depth, and pixel layout exactly match the window into which we are drawing,

The image can be efficiently copied to a graphics device. This allows drawImage to do its job quickly.

We can also construct a BufferedImage object directly to use as an offscreen buffer.

This is useful when we need control over the offscreen image's type or transparency.

Page 14: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Predefined image types supporting BufferedImage

TYPE_3BYTE_BGR TYPE_4BYTE_ABGR TYPE_4BYTE_ABGR_PRE TYPE_BYTE_BINARY TYPE_BYTE_GRAY TYPE_BYTE_INDEXED TYPE_CUSTOM TYPE_INT_ARGB_PRE TYPE_INT_ARGB TYPE_INT_BGR TYPE_INT_RGB TYPE_USHORT_555_RGB TYPE_USHORT_565_RGB TYPE_INT_GRAY

Page 15: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Java Drawing Offscreen Buffer

BufferedImage image = new BufferedImage(imageWidth,imageHeight,

BufferedImage.TYPE_BYTE_RGB);

Graphics2D g2d = image.CreateGraphics();

g2d.draw(...);

....

Page 16: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Drawing on offscreen buffer

Create a BufferedImage Create a Graphics2D object to render into

newly built BufferedImage using the createGraphics method

Use Graphics2D object for renderingWhen rendering into a Swing component, Swing

automatically double buffers the display

Useful for animation to avoid flickering

Page 17: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Example:

The Use of Offscreen Buffering

Page 18: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

public void update(Graphics g){ Graphics2D g2 = (Graphics2D)g; if(firstTime){ Dimension dim = getSize(); int w = dim.width; int h = dim.height; area = new Rectangle(dim); bi = (BufferedImage)createImage(w, h); big = bi.createGraphics(); rect.setLocation(w/2-50, h/2-25); big.setStroke(new BasicStroke(8.0f));firstTime = false; } // Clears the rectangle that was previously drawn. big.setColor(Color.white); big.clearRect(0, 0, area.width, area.height); // Draws and fills the newly positioned rectangle to the buffer. big.setPaint(strokePolka); big.draw(rect); big.setPaint(fillPolka); big.fill(rect); // Draws the buffered image to the screen. g2.drawImage(bi, 0, 0, this); }

Page 19: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Rendering a BufferedImage To render a buffered image into a specific context, call one

of the drawImage method of the context's Graphics object. For example, when rendering within a Component.paint

method, we call drawImage on the graphics object passed to the method.

public void paint(Graphics g) { if (getSize().width <= 0 || getSize().height <= 0) return; Graphics2D g2 = (Graphics2D) g; if (offImg != null && isShowing()){ g2.drawImage(offImg, 0, 0, this);} }

Page 20: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Manipulating BufferedImage Data Directly

In addition to drawing directly in a BufferedImage, we can directly access and manipulate the image's pixel data in a

couple of ways . These are useful if you're implementing the BufferedImageOp

filtering interface The image package provides a pair of interfaces that

define operations on BufferedImage and Raster objects: BufferedImageOp and RasterOp.

The classes that implement these interfaces include AffineTransformOp, BandCombineOp, ColorConvertOp, ConvolveOp, Look pop, RescaleOp.

These classes can be used to geometrically transform, blur, sharpen, enhance contrast, threshold, and color correct images.

Page 21: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Edge Detection and Enhancement

Edge detection is commonly used in medical imaging and mapping applications.

Edge detection is used to increase the contrast between adjacent structures in an image, allowing the viewer to

discriminate greater detail.

Page 22: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

The Code illustrating Edge Detection

float[] elements = { 0.0f, -1.0f, 0.0f,

-1.0f, 4.f, -1.0f,

0.0f, -1.0f, 0.0f};

...

BufferedImage bimg = new BufferedImage(bw,bh,BufferedImage.TYPE_INT_RGB);

Kernel kernel = new Kernel(3, 3, elements);

ConvolveOp cop = new ConvolveOp(kernel,

ConvolveOp.EDGE_NO_OP, null);

cop.filter(bi,bimg);

Page 23: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Lookup-table Manipulation

A lookup operation can be used to alter individual components of a pixel.

byte reverse[] = new byte[256]; for (int j=0; j<200; j++){ reverse[j]=(byte)(256-j); } ByteLookupTable blut=new ByteLookupTable(0, reverse); LookupOp lop = new LookupOp(blut, null); lop.filter(bi,bimg);

Page 24: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Using an Image Processing Operation

Convolution is the process that underlies most spatial filtering algorithms.

Convolution is the process of weighting or averaging the value of each pixel in an image with the values of neighboring pixels.

This allows each output pixel to be affected by the immediate neighborhood in a way that can be mathematically specified with a kernel

Page 25: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Blurring with Convolution

The following code fragment illustrates how to use one of the image processing classes, ConvolveOp.

In this example, each pixel in the source image is averaged equally with the eight pixels that surround it.

The variable simpleBlur contains a new instance of ConvolveOp that implements a blur operation on a BufferedImage or a Raster.

Suppose that sourceImage and destImage are two instances of BufferedImage.

When we call filter( the core method of the ConvolveOp class) it sets the value of each pixel in the destination image by averaging the corresponding pixel in the source image with the eight pixels that surround it

Page 26: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Blurring with Convolution float weight = 1.0f/9.0f; float[] elements = new float[9]; // create 2D array // fill the array with nine equal elements for (i = 0; i < 9; i++) { elements[i] = weight; }// use the array of elements as argument to create a Kernel

private Kernel myKernel = new Kernel(3, 3, elements); public ConvolveOp simpleBlur = new ConvolveOp(myKernel); // sourceImage and destImage are instances of BufferedImage

simpleBlur.filter(sourceImage, destImage) // blur the image

Page 27: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Blurring with Convolution

Page 28: Image processing with Java - 2D cont’d. Overview  An imaging model that supports the manipulation of fixed- resolution images stored in memory.  A new

Sharpening with Convolution float[] elements = {0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f}; ... Kernel kernel = new Kernel(3,3,elements);ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); cop.filter(bi,bimg);