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

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

Digital Images in Java Java’s imaging classes. Java imaging library  Java has good support for image processing  Must master a handful of classes and.
Android UserInterfaces Nasrullah Niazi. overView All user interface elements in an Android app are built using View and ViewGroup objects. A View is an.
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Lecture 9 Grey Level & Colour Enhancement TK3813 Dr. Masri Ayob.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Continuing to very Powerful Rendering Model Java2D.
CS 551 / CS 645 Antialiasing. What is a pixel? A pixel is not… –A box –A disk –A teeny tiny little light A pixel is a point –It has no dimension –It occupies.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Lecture09 Push & Immediate Mode Imaging. Push Imaging Model (PIM) ImageProducer is an interface for objects which can produce the image data for Images.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Chapter 4 2D Graphics: Advanced Topics  To construct custom shape primitives  To apply basic image processing techniques  To create fractal images.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Sketchify Tutorial Graphics and Animation in Sketchify sketchify.sf.net Željko Obrenović
Manipulating and Displaying Images in Java Lihang Ying Department of Computing Science University of Alberta, Canada
Introduction to Computer Graphics
Vector vs. Bitmap SciVis V
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
V Obtained from a summer workshop in Guildford County July, 2014
Image Processing1 Image Filtering Filtering can be use to enhance some features and de- enhance others. Usually used as a pre-processing step.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
COMP Bitmapped and Vector Graphics Pages Using Qwizdom.
Vector vs. Bitmap
Animation and Double-Buffering. The animation methods described here are based on standard techniques of double-buffering applicable to most high-level.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Photoshop Teppo Räisänen LIIKE/OAMK General Information Photoshop is an advanced image editing tool Web Graphics Printed material UI is based on.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
1 Computer Graphics Week2 –Creating a Picture. Steps for creating a picture Creating a model Perform necessary transformation Lighting and rendering the.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
INT 840E Computer graphics Introduction & Graphic’s Architecture.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
Chapter 18 產生驗證碼. No Cache 防止在 JSP 或 SERVLET 中的輸出不被 BROWSER 保存在 CACHE 中.
Introduction to Image processing using processing.
Introduction to Processing. 2 What is processing? A simple programming environment that was created to make it easier to develop visually oriented applications.
High Performance Java Swing Animation David Wallace Croft Presented to the Plano Java Users Group Plano, TX Copyright 2004 David Wallace Croft.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Java Dynamic Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Pinnacle Pro Painting Program User Manual Created by: David Kwasny Chris Schulz W. Scott DePouw.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
: Chapter 5: Image Filtering 1 Montri Karnjanadecha ac.th/~montri Image Processing.
Instructor: Mircea Nicolescu Lecture 5 CS 485 / 685 Computer Vision.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Java 2D Image Manipulation A Brief Overview Ziv Yaniv.
Guilford County SciVis V104.03
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Vector vs. Bitmap. Vector Images Vector images (also called outline images) are images made with lines, text, and shapes. Test type is considered to be.
Layers in Adobe After Effect
Vector vs. Bitmap.
IMAGES.
Chapter III, Desktop Imaging Systems and Issues: Lesson IV Working With Images
Basic Graphics Drawing Shapes 1.
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
Defining Classes and Methods
CS297 Graphics with Java and OpenGL
Presentation transcript:

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 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.

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 flickering  This 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.

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()

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.

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;

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.

Example: Creating a BufferedImage

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

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.

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

 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.

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.

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

Java Drawing Offscreen Buffer BufferedImage image = new BufferedImage(imageWidth,imageHeight, BufferedImage.TYPE_BYTE_RGB); Graphics2D g2d = image.CreateGraphics(); g2d.draw(...);....

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 rendering  When rendering into a Swing component, Swing automatically double buffers the display  Useful for animation to avoid flickering

Example: The Use of Offscreen Buffering

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); }

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); }

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.

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.

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);

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);

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

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

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

Blurring with Convolution

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);