1 / 28

Image processing with Java - 2D cont’d

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

bwebb
Download Presentation

Image processing with Java - 2D cont’d

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Image processing with Java - 2Dcont’d

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

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

  4. How to draw an image? • The Graphicsobject is available to draw anImage 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()

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

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

  7. Using BufferedImages • The BufferedImage class is the main class supporting the immediate imaging mode. • It manages an image in memory, providing ways • to storepixel data, • to interpret pixel data, and • to render the pixel data to a Graphics orGraphics2D context.

  8. Example: Creating a BufferedImage

  9. public Graphics2DcreateDemoGraphics2D(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 memoryusing one of several constructor methods provided

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

  11. 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 2DAPI 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

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

  13. Creating an Offscreen Buffer • The simplest way to create an image that we can use as an offscreen buffer is • to use theComponent.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.

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

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

  16. Drawing on offscreen buffer • Create a BufferedImage • Create a Graphics2Dobject to render into newly built BufferedImage using the createGraphics method • Use Graphics2Dobject for rendering • When rendering into a Swing component, Swing automatically double buffers the display • Useful for animation to avoid flickering

  17. Example: The Use of Offscreen Buffering

  18. public void update(Graphics g){ Graphics2Dg2= (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); }

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

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

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

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

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

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

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

  26. 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 ofBufferedImage simpleBlur.filter(sourceImage, destImage) // blur the image

  27. Blurring with Convolution

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

More Related