+ All Categories
Home > Documents > 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing...

1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing...

Date post: 21-Dec-2015
Category:
View: 225 times
Download: 2 times
Share this document with a friend
Popular Tags:
61
1 Lecture 8: Basic Images Manipulation (2)
Transcript
Page 1: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

1

Lecture 8: Basic Images Manipulation

(2)

Page 2: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

2

Contents

Displaying image in Java

Printing

Manipulation of pixel data

Page 3: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

3

Displaying Image in Java

Image display using awt.

Image display using swing components.

Page 4: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

4

Printing

How do we simulate intermediate shades of grey in an 8-bit image?

How can we use small number of coloured inks to simulate the huge range of colours possible in a 24-bit image?

Page 5: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

5

Printing Grayscale Images

Problem: How to print a gray-scale image when the only color ink available is black (the ink) and white (the paper)?

This is the problem faced by newspapers and any other print media. Newspapers print at 80-100 DPI (dots per inch) Magazines print at 200-300 DPI

Color printing is a more complicated variant of this problem.

The solution is to use a technique known as halftoning.The process of generating binary pattern of black and white dots from an image.

Page 6: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

6

Halftoning

Photographs are images with continuous shades of black, white, and gray. These shades are tones.

Halftoning is a technique, originally developed for the printing industry, to reproduce continuous tone images using printing techniques capable of black and white only, not shades of gray.

Page 7: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

7

Need for Digital Image Halftoning

Examples of reduced grayscale/color resolutionLaser and inkjet printers ($9.3B revenue in 2001 in US)

Facsimile machines

Low-cost liquid crystal displays

Halftoning is wordlength reduction for imagesGrayscale: 8-bit to 1-bit (binary)

Color displays: 24-bit RGB to 12-bit RGB (e.g. PDA/cell)

Color displays: 24-bit RGB to 8-bit RGB (e.g. cell phones)

Color printers: 24-bit RGB to CMY (each color binarized)

Halftoning tries to reproduce full range of gray/ color while preserving quality & spatial resolution.

Introduction

Page 8: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

8

Halftoning

The process of transforming a grayscale image to a halftone.Create the illusion of gray-scale by varying the average dot density in local regions of the imageDots are always something that can be counted and it must be possible to hold a measuring stick to them. Takes advantage of the fact that the eye integrates intensity of small image regions.Sacrifices spatial resolution for gray-scale resolution (unless the output device can be over-sampled as most ink-jet printers are these days)Spatial refers to image itself, based on direct manipulation pixels.

Page 9: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

9

Halftoning

                                                                                                                               

Consider a grayscale image (left) which is halftoned (right) for printing. The right image looks like a grayscale image but is actually only black and white!

Zooming in on the image reveals “pixel” sizes of differing sizes and shapes.

Source: www.adobe.com

Page 10: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

10

Halftoning

Page 11: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

11

Digital Halftoning

The previous example is from the domain of print media which uses physical filters, lights, and film to generate the halftoned images.

Digital halftoning cannot be done this way since digital images consist of identically shaped pixels (usually rectangular) which are either black or white.

The main problem is “Should this pixel be white or black”?

Several solutions to this problem exist

• Bi-level Thresholding

• Font/Pattern replacement

• Random thresholding

• Dither matrices

• Error Diffusion

Page 12: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

12

Bilevel Thresholding

Re-quantize the image using a 1 bit color

If the gray-level of a pixel is less than some threshold value then set the output to black otherwise set the output to white.

The threshold value may be the “center of the available gray-scale range”

The threshold value may by the “average of all pixels in the image”

The T value can be choosen e.g inspecting the histogram image f.

or

Page 13: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

13

Bi-level Thresholding Example

Original Image Absolute Threshold Adaptive Threshold

Page 14: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

14

Patterning

This is the age-old trick of substituting a “font” pattern for each pixel. Replacing each pixel by a pattern taken from binary font.Consider the following 3x3 font pattern

0 1 2 3 4

5 6 7 8 9

This font can be used to print an image consisting of 10 gray levels

Grey level 9Since we are replacing each pixel by a 3x3 block of pixels, both the width and height of the image increased by a factor of 3

Page 15: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

15

Patterning

Page 16: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

16

Patterning

Given an NxN font there are N2+1 “gray levels” to choose from. N < number of grayscale level.The output image is N times larger (in both dimensions) than the original. Font patterns must be chosen carefully to avoid the introduction of artificial patterns into the output.Drawback – create false lines and contours in homogeneous image regions.

algorithm patternHalftone(im, font) INPUT: Grayscale image im and a set of font patterns OUTPUT: BW halftone of the input

Let output be an “empty” image for every pixel P in im let F be the font that most closely approximates the actual gray-level of P replace pixel P with font F in the output image return output

algorithm patternHalftone(im, font) INPUT: Grayscale image im and a set of font patterns OUTPUT: BW halftone of the input

Let output be an “empty” image for every pixel P in im let F be the font that most closely approximates the actual gray-level of P replace pixel P with font F in the output image return output

Page 17: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

17

Patterning Example

33 113 234

64 121 219

92 133 245

33 113 234

64 121 219

92 133 245

1 4 8

2 4 8

3 5 9

Pattern the gray-scale image below

Same image shown in grayscale

pixels scaled to thecorresponding font value

binary output image

Page 18: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

18

Patterning Example

Page 19: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

19

Random Thresholding

Selects a threshold value at random for every pixel.

The output is white if the pixel value exceeds the threshold and black otherwise.

High intensity pixels have a greater chance of becoming white while low intensity pixels have a greater change of becoming black

Attempts to spread the error per pixel evenly (or randomly) throughout the image

algorithm randomDither(image)INPUT: An imageOUTPUT: A B/W dithered version of the input

Let output be an “empty” imagefor every pixel P of the input

let T be a random value between 0 and 255if P <= T then output blackelse output white

return output

Page 20: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

20

Random Dithering Example

Page 21: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

21

Dither Matrices

Selects a threshold value using a pre-determined pattern.The output is white if the pixel value exceeds the pre-determined threshold and black otherwise.The matrix is laid like a tile over entire image and each pixel value is compared with the corresponding threshold from the matrix.Attempts to spread the error per pixel evenly throughout small regions of the image (and hence, throughout the entire image).

algorithm matrixDither(image, M)INPUT: An image and an NxN “dither” matrix MOUTPUT: A B/W dithered version of the input

Let output be an “empty” imagefor every pixel P(x,y)of the input

let T be M[x%N][y%N]if P <= T then output blackelse output white

return output

Page 22: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

22

D1 and D2 Dither Matrices

D1 =13

20

D2 =

513715

91113

614412

10280

64192

1280

80208112240

1441617648

9622464192

160321280

Page 23: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

23

2x2 Pattern Dithering Example

Page 24: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

24

4x4 Pattern Dithering Example

Page 25: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

25

Page 26: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

26

Halftoning: Floyd-Steinberg error diffusion

To correct the errors introduced by thresholding.

Selects a threshold value which is at the center of the gray-scale range

The output is white if the pixel value exceeds the threshold and black otherwise.

The error of a pixel is the difference between the actual value of the pixel and the value selected (black or white)

This error is carried over to nearby pixels in a systematic way

This gives an average density of white dots proportional to the average intensity level within a region of the image

Pixel 7/16

3/16 5/16 1/16

Typical threshold value is 128 for images with pixel values in [0,255]

Page 27: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

27

Floyd-Steinberg

Pseudocode

Page 28: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

28

Floyd-Steinberg

Page 29: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

29

Floyd-Steinberg

Java implementation of Algorithm 5.1

Page 30: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

30

Floyd-Steinberg

Page 31: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

31

Floyd-Steinberg

Returns the smaller of two int values. That is, the result the argument closer to the value of Integer.MIN_VALUE. If the arguments have the same value, the result is that same value.

Page 32: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

32

Floyd-Steinberg Example

35 89 95 132

68 112 100 150

51 45 98 127

0 ? ? ?

? ? ? ?

? ? ? ?

35 104 95 132

79 114 100 150

51 45 98 127

35 89 95 132

68 112 100 150

51 45 98 127

15

11 2

Error = 35

<128

0.3125*error

0.4375*error

+

Page 33: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

33

Floyd-Steinberg Example

0 0 ? ?

? ? ? ?

? ? ? ?

35 104 95 132

79 114 100 150

51 45 98 127

Error=104

35 104 95 132

79 114 100 150

51 45 98 127

35 104 141 132

99 147 107 150

51 45 98 127

46

20 33 7

Page 34: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

34

Floyd-Steinberg Example

0 0 255 ?

? ? ? ?

? ? ? ?

35 104 141 132

99 147 106 150

51 45 98 127

Error=141-255=-114

35 104 141 132

99 147 107 150

51 45 98 127

35 104 141 82

99 126 71 143

51 45 98 127

-50

-21 -36 -7

>128

Page 35: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

35

Floyd-Steinberg Example

127984551

15010011268

132958935

Input Image

255000

25502550

025500

Output Image

Note that this is not an in-place algorithm. Extra storage is required! (i.e. copy the input image and then manipulate the copy)

The sum of all gray levels in the input is 1102. The sum of all values in the output is 1020. The average per-pixel error is –6.83

Page 36: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

36

Floyd-Steinberg Error Diffusion

Page 37: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

37

Colour Printing

Page 38: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

38

Colour Printing(Great 1 Page tutorial from Adobe)

Color prints are typically constructed from 4 half-toned images using the CMYK color model (K is black).

The individual planes are halftoned using rotated screens to avoid odd-patterned moire effects.

The screen angles are typicallyY=0°     C=15°     M=75°     K=45°

Source: www.adobe.com

Page 39: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

39

Colour Printing(Great 1 Page tutorial from Adobe)

When viewed with the naked eye at normal viewing distance (inset), the image appears to have continuous color tone. When viewed closely, the superimposed cyan, magenta, yellow, and black halftones become apparent.

Source: www.adobe.com

Page 40: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

40

Colour Half-toning

A common problem arises when displaying images on systems with limited resources.

Imagine a web browser loading a 24-bit image. The web browser is using an 5-bit display with a pre-configured color palette. How can the image be displayed?

Color dithering is used to display high-color images using a limited palette of colors. The usual algorithm is a modification of Floyd-Steinberg diffusion.

interlaced pixels

Page 41: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

41

Colour Dithering

Page 42: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

42

Colour Halftoning

Page 43: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

43

Colour Halftoning

So What's Really Important? The choice of color reduction function and options depends upon the requirements for speed of execution, image quality, and ability to display multiple images at the same time.

Hints For fastest speed use a standard palette.

For highest image quality use diffusion scatter.

To display multiple images simultaneously use the same palette for all images.

Page 44: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

44

Colour Dithering

Page 45: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

45

Colour Dithering

When do we need dithering?Under fixed lighting conditions the typical person can discern approximately 100 different brightness levels. This number varies slightly with hue (for instance we can see more distinct shades of green than blue). Which particular set of 100 levels also changes as a function of the ambient lighting conditions. The 256 colors available for each primary in a true color display are usually adequate for representing these 100 levels under normal indoor lighting (when the nonlinearities of the display are properly compensated for). Thus, there is usually no need to dither a true color display. On index displays dithering is frequently used to represent color images. Given a 256 entry color map you can only represent approximately 6 colors per red, green, and blue primary (6x6x6=216). However, if just one or two hues are used it is possible to allocate enough color table entries (~50 per hue) so that dithering can be avoided.

Page 46: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

46

Color Dithering

Page 47: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

47

Color Dithering

Problems with ditheringWhen a signal, or color, is modified from it original value by the addition of some other value, it is said that noise has been addedWhite noise generates values within some interval such that all values are equally likely. Solution generate symmetric values.

Page 48: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

48

Color Dithering

To find the error between two RGB colors, compute the length of the 3D vector between the colors.

algorithm colorDither(Image IM, Palette CP)for every pixel P in IM (left-to-right-top-to-bottom)

set the color of P to the nearest matching color in CPdiffuse the error using floyd-steinberg on each band

Page 49: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

49

Color Dithering

Page 50: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

50

Manipulation of Pixel Data

Extracting regions of interest.

Basic geometric manipulation.

Page 51: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

51

Extracting Region of Interest

A region of interest (ROI) is a rectangular area within the image defined either by:

The coordinates of the pixels at its upper-left & lower-right corners or

The coordinates of the pixels at its upper-left corner & its dimensions.

Commonly use to limit the extent of image processing operations to some small part of the image.

Interactive image processing software will often provide facility to define ROIs of images using mouse etc.

Page 52: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

52

Extracting Region of Interest

Java’s BufferedImage class has 3 methods that useful when operating on ROIs:

Raster getData(Rectangle rect)

Void setData(Raster raster)

BufferedImage getSubimage(int x, int y, int w, int h).

Example of ROI application: MeanROI application, which displays an image, allow the user to draw a ROI on it and then computes mean grey level within that ROI (see course web page).

Page 53: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

53

Extracting Region of Interest

The getData() method takes as its sole parameter a Rectangle object that specifies the position and dimensions of the ROI. It returns the data from that ROI as a Raster object. The data stored within the raster are independent of the image on which getData() was called, so subsequent changes to the image will not affect the raster. However, the raster’s coordinate system is that of the original image. This means that the first pixel in the raster has the same coordinates as the ROI’s origin. We must be carefhl to take this into account when processing raster data.

Instances of Raster are read-only, but we may cast the object returned by getData() to a WritableRaster if we wish to modify the pixel values. The modified raster can then be loaded back into its parent image by invoking the setData() method of the image, with the raster as a parameter.

If in-place processing of a ROI is required, the getSubimage() method of BufferedImage may be more convenient. Given the coordinates and dimensions of the ROl as parameters, this method returns a subimage that shares the same data array as the parent image. This means that changes made to pixel values in the subimage will affect the parent image. The coordinate system of the subimage is not that of the parent image, so its pixels are indexed starting from (0, 0).

An example of ROl usage can be seen in Listing 5.13. This shows two versions of a method meanValue(). The first computes the mean pixel value in the supplied image (assumed to be greyscale). The second computes the mean within a ROI specified by a Rectangle object. Note that there is no need to duplicate any code in the second version of the method; all that we need to do is invoke getSubimage() on the image using the ROI parameters contained in the Rectangle object and then pass the image that is returned to the first version of meanValue(). Both of these methods are used in the MeanROI application described earlier.

Page 54: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

54

Extracting Region of Interest

Page 55: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

55

Basic Geometric Manipulation

Enlargement or shrinking.Accomplished by replicating or skipping pixels.

Used to magnify small details in an image , or reduce a large image in size.

For a pixel (x,y) in the output image (enlarge by factor n), the corresponding pixel in the input image is at (x/n,y/n).

To shrink an image by n factor, we must sample every nth pixel in the horizontal and vertical dimension and ignore the others.

Page 56: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

56

Basic Geometric Manipulation

Rotation.Relatively simple to implement for the special case where the angle is a multiple of 900.

Reflection along the x or y axis.Reflection along either of the image axes is simply involves reversing the ordering of pixels in the rows or columns of the image.

Page 57: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

57

Page 58: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

58

Page 59: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

59

Page 60: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

60

Assignment 2 (20%)

Write a Java program that can:Read and display an image file.

Perform the following digital halftoning and display the halftone images:

• Random Thresholding

• Floyd-Steinberg error diffusion

Allow the user to select a region of interest (ROI) and then:

• Extract the ROI from the image and display the enlarged (by a factor of n) image.

Allow the user to select the reflection line and display the output.

You can work in a group (maximum of 3 students in a group).

Page 61: 1 Lecture 8: Basic Images Manipulation (2). 2 Contents Displaying image in Java Printing Manipulation of pixel data.

61

Thank you

Q&A


Recommended