Department of Electrical and Computer Engineering EEL 6562 Image Processing and Computer Vision Lecture 4 Image Quantization Dr. Dapeng Oliver Wu University of Florida Department of Electrical and Computer Engineering Fall 2016
Outline Introduction Uniform quantization Minimum Mean Squared Error (MMSE) quantization Scalar quantization Vector quantization Machine learning, data mining, bioinformatics, source coding, computer vision, pattern recognition, artificial intelligence Implementation Visual quantization Half toning
Image Sampling Sampling by i = 0 Δx 1 … N - 1 Δy y’ x’ j = 0 1 2 M-1
Examples i N-1 M-1 j
Matrix Representation fmin ≤ f(i, j) ≤ fmax , value in a continuum Popular image size N = 64, M = 64 (26), # of pixels = 4,096. N = 512, M = 512 (29), # of pixels = 262,144. N = 1280, M = 1024, # of pixels = 1,310,720.
Purposes of Quantization Analog to digital conversion (ADC) Use a finite number of (discrete) values to represent infinite number of (continuous) values Compression: Use less number of (discrete) values to represent a continuous/discrete signal It is like election in human society Question: how to choose representative values and the regions being represented? There are infinite number of ways. Which is optimal?
Quantization fmax fmin
A Quantizer Q(f) Quantizer f Q(f) r8 r7 Decision Levels {tk, k = 1, …, L+1} r6 Reconstruction Levels {rk, k = 1, …, L} r5 If r4 f Then Q(f) = rk t1 t2 t3 t4 t5 t6 t7 t8 L levels need bits r3 returns the smallest integer that is bigger than or equal to x r2 Quantizer error r1 r0 r0 r1 r2 r3 r4 r5 r6 r7 r8 f t1 t2 t3 t4 t5 t6 t7 t8
Matrix Representation For grayscale image, 256 levels or 8 bits/pixel is sufficient for most applications For color image, each component (R, G, B) needs 256 levels or 8 bits/pixel (bpp) Storage for typical images 512 x 512, 8 bits grayscale image: 262,144 Bytes 1024 x 768, 24 bits true color image: 2,359,296 Bytes
Uniform Quantization Equal distances between adjacent decision levels and between adjacent reconstruction levels tl – tl-1 = rl – rl-1 = q Parameters of Uniform Quantization L: levels (L = 2R) B: dynamic range B = fmax – fmin q: quantization interval (stepsize), which is uniform q = B/L = B2-R Quantization function Note: returns the biggest integer that is smaller than or equal to x
A Uniform Quantizer Q(f) r7=fmax-q/2 r6 r5 r4 stepsize q=(fmax-fmin)/8 r0=fmin+q/2 f t0 t1 t2 t3 t4 t5 t6 t7 t8 fmin fmax
Truncated Uniform Quantizer Signal with infinite dynamic range Truncate the lower and higher values to fmin and fmax Parameters fmin, fmax L (R) Quantization function
A Truncated Uniform Quantizer overload region Q(f) r7=fmax-q/2 r6 r5 r4 r3 r2 r1 r0=fmin+q/2 f t0 =-∞ fmin t1 t2 t3 t4 t5 t6 t7 fmax t8 =∞ overload region
Example 1 of Uniform Quantizer The output of a CCD camera is in the range of 0.0 to 5.0 volt L = 256 q = 5 / 256 (volt) The output value in the interval (l * q, (l + 1) * q] is represented by l, l = 0, …, 255. The reconstruction level rl = l * q + q/2, l = 0,…, 255.
Example 2 of Uniform Quantizer Digital Image of 256 gray levels Quantize it into 4 levels fmin= 0, fmax = 256, q = 256 / 4 = 64, q/2 = 32, Compress the image from 8 bits/pixel to 2 bits/pixel. The compression ratio is 4.
Examples of Uniform Quantizer q=8, L=32 Original, L=256 q=16, L=16 q=64, L=4
Performance Measures for a Quantizer Mean squared error (MSE) Peak Signal to Noise Ratio (PSNR)
Implementation of Uniform Quantization Setup the quantization function Q(f) for all possible input level first. Matlab code x = imread('lena_gray.bmp'); [height, width] = size(x); B = 256; x = double(x); % This is the quantization table Q = zeros(256, 1); % Quantized to 16 levels L = 16; q = B / L; for i = 0:255, Q(i+1, 1) = floor(i / q) * q + q /2; end y = zeros(size(x)); for i = 1:height, for j = 1:width, y(i, j) = Q(x(i,j) + 1); MSE = mean(mean((x-y).^2)) PSNR =10*log10(255^2/MSE) % in dB y=uint8(y); imshow(y) L = 16, MSE = 21.6, PSNR=34.8 dB
Quantization Effect – False Contour 1-D Signal 2-D Image f(t) t t f(i,j)=max(I,j)
Minimum Mean Square Error Quantizer Mean squared error (MSE) of a quantizer Where p(f) is the probability density function of f Uniform distribution Gaussian distribution Laplacian distribution MSE for a specific image Minimize E in terms of {tk} and {rk}
Minimum Mean Square Error Quantizer MMSE Quantizer Nearest neighbor condition Centriod condition
MMSE Quantizer for A Uniform Source
MMSE Quantizer Example for a Uniform Source Original Image L=64, MSE=1.507 L=16, MSE=21.624 L=2, MSE=1384.15 L=8, MSE=83.725 L=4, MSE=368.50
MMSE Quantizer for a Non-uniform Source Following conditions may not have a closed form solution Numerical procedures are necessary Tables 4.1 and 4.2 in Jain’s book give the optimal tl and rl for Gaussian and Laplacian sources with unit variance and zero mean. Anil K. Jain, ``Fundamentals of digital image processing,'' Englewood Cliffs, NJ : Prentice Hall, 1989.
MMSE Quantizer Example for a Gaussian Source Original Image PDF: Gaussian μf=128, σf=36 Uniform Quantization L=16, MSE=22.73 MMSE Quantization L=16, MSE=14.35
Types of Quantization Scalar quantization: Vector quantization Quantize a scalar random variable into representative scalars The scalar random variable can be continuous or discrete Vector quantization Quantize a random vector into representative vectors The random vector can be continuous or discrete
Llyod Algorithm When the distribution of the signal is unknown The quantizer is designed based on a training set containing representative samples tl and rl are determined iteratively, based on the nearest neighbor and centroid conditions, respectively.
Lloyd Algorithm (for scalar quantization) Iterative algorithms for determining MMSE quantizer parameters Can be based on a pdf or training data Iterate between centroid condition (election) and nearest neighbor condition (partitioning)
Generalized Lloyd Algorithm (for vector quantization) Start with initial codewords Iterate between centroid condition (senator election) and nearest neighbor condition (constituency partitioning)
Visual Quantization Reduce the artifact of false contour Better visual perception (Human eyes are very sensitive to edges) Might produce worse MSE
Pseudo Random Noise Quantizer f Uniform Quantizer Q() Step size: q + g + + + + - h Random Noise Generator Uniform in (-q/2, q/2)
Example 8 Level Pseudo Random 8 Level Uniform Quantizer MSE=85.84 8 Level Pseudo Random Noise Quantizer MSE=86.09
Halftoning Halftoning Use 2 levels to render a multi-level image Create the illusion of continuous-tone pictures on bi-level printers or display devices. E.g, representing gray scale image using 2 levels (black and white) CMYB color printer (using cyan, magenta, yellow, black toners) Black/white printer (using black toner) monochrome monitor
Dithering Halftoning is achieved by dithering Two Level Quantizer f + fw + (Multiple levels) - fb (Two levels; applied to one dot) Dither Signal d For a B/W printer, 600 dpi (dots per inch); one square inch has 360,000 dots. # of dots in black determines gray level.
Types of Dithering Ordered dithering Error-diffusion-based dithering
d Ordered Dither Signal Dither signal d(m,n) is obtained by repeating a kxk dither matrix Dk(m,n) d(m, n) = Dk([m]k, [n]k), where [m]k means m modulo k. Criteria for the dither matrix Contains sufficiently distinct values Similar values should be separated as far as possible Dk d Dk Dk Dk Dk Dk … … …
Recursive Design of the Dither Matrix Define D2 Recursion where Uk is a kxk matrix of 1’s. Usage Dk needs to be scaled and shifted, q=256/2k
Example K = 4, q = 16 fb=64 fw=192 Halftoning with k=4, q=16 2 Level Uniform Quantization Halftoning with k=4, q=16
Halftoning Using Error Diffusion + - e[n-1] Delay
Quantization Summary Typically L = 28 = 256 and we have log2(L) = log2(28) = 8 bit quantization. From now on omit references to fmin; fmax and unless otherwise stated assume that the original digital images are quantized to 8 bits or 256 levels. To denote this refer to as taking integer values k where 0≤k≤255, i.e., let us say that {0, …, 255}
Homework 2 Using Matlab or C or any other computer language, to implement uniform quantization of the grayscale image in “YourLastNameGray.jpg” obtained in HW1. Your program should do the following: i) read a grayscale image into an array; ii) quantize and save the quantized image in a different array; iii) compute the MSE between the original and quantized images; and iv) display and print the quantized image. Your program should allow you to vary the reconstruction level (L). Record the MSE obtained with L=64, 32, 16, 8, and save the quantized images with corresponding L values, MSE values, and PSNR values. Your submission should include 1) your computer program file and 2) an MS WORD file that contains L values, MSE values, PSNR values, and the images. Please put the images in the WORD file, instead of in JPEG files. JPEG files are not allowed to upload as separate files.
Homework 2 Put your solution in a single WORD file or a single pdf file. Submit your homework in the format of WORD file, Matlab file, or pdf through E-Learning web site under the directory of Homework 2. Please submit your homework in separate files (e.g., *.m or *.doc or *.pdf). Please do not submit zip files or JPEG files. JPEG files should be embedded in a WORD file or pdf file. Due: 4pm, 9/14
Reading “Digital Image Processing”, Chapter 2 (Section 2.4.1 – 2.4.3)