420 likes | 429 Views
Chapter 10. Basic Digital Signal Processing of Images. Objectives. Describe the structure and numerical storage classes of digital images as matrices. Compare and contrast the representation of gray-scale and color images.
E N D
Chapter 10 Basic Digital Signal Processing of Images
Objectives • Describe the structure and numerical storage classes of digital images as matrices. • Compare and contrast the representation of gray-scale and color images. • Demonstrate the use of matrix addition, subtraction, and multiplication to achieve certain transformations of images. • Describe and demonstrate the statistical manipulation of pixels to achieve image enhancement and noise removal. • Define and demonstrate 2-dimensional filter convolution of images. • Demonstrate the application of discrete transforms to analyze the frequency content of images.
Structure of Digital Images • A digital image is a matrix (array) of numbers. • Each matrix value (a pixel or “picture element”) represents the light intensity for that position in the visual field • The numbers are typically given as • double-precision intensity • 0 represents “black” • 1 represents “full intensity” or “white” • Or, un-signed integer 8-bits (uint8) • 0 represents “black” • 255 represents “full intensity” or “white” • Only positive numbers are allowed: negative numbers are “black” and numbers > 1 are “white”
>> C=[1 0 1 0;0 1 0 1;1 0 1 0;0 1 0 1] C = 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 >> Cu8=im2uint8(C) % Convert the image C to storage class uint8 Cu8 = 255 0 255 0 0 255 0 255 255 0 255 0 0 255 0 255 >> imshow(C,'InitialMagnification','fit'), title('Display of Matrix C') A Small Image and Its Matrix
>> Cgray=[.125 0 .250 0;0 .375 0 .5;.625 0 .75 0;0 .875 0 1] Cgray = 0.1250 0 0.2500 0 0 0.3750 0 0.5000 0.6250 0 0.7500 0 0 0.8750 0 1.0000 >> Cgray_uint8=im2uint8(Cgray) Cgray_uint8 = 32 0 64 0 0 96 0 128 159 0 191 0 0 223 0 255 >> imshow(Cgray_uint8,'InitialMagnification','fit'), title('Display of Matrix Cgray') Grayscale Image
Color Images • Transmitted color is synthesized by varying intensities of the primary colors red, green, and blue (RGB). • Each pixel location has an RGB intensity value. • The color image matrix is therefore three-dimensional (rows, columns,3), where the “color planes” are R,G,B in that order.
Color Image Structure >> I=imread('wernerboy.png'); >> imtool(I,'InitialMagnification','fit') >> size(I) ans = 64 85 3
Image Resolution >> I3=imread('proppix1.jpg'); >> I3g=rgb2gray(I3); % Convert the color image to a gray-scale B/W image >> imtool(I3g) >> size(I3g) ans = 480 640 ~ 300 K pixels Zoom on cell phone pixels
Image Down-Sampling >> I4=imread('baboon.bmp'); >> I4_2=decimate_image(I4,2); %The parameter “2” is the down-sample factor >> I4_4=decimate_image(I4,4); >> I4_8=decimate_image(I4,8); >> subplot(2,2,1),imshow(I4),title('Original Image') >> subplot(2,2,2),imshow(I4_2),title('Down-sampled N=2') >> subplot(2,2,3),imshow(I4_4),title('Down-sampled N=4') >> subplot(2,2,4),imshow(I4_8),title('Down-sampled N=8')
Image Down-Sampling Down-sampling reduces the number of pixels by N, thus reducing the resolution by the same factor
Image Aliasing >> I5=imread('stripes.bmp'); >> I5_2=decimate_image(I5,2); >> I5_4=decimate_image(I5,4); >> I5_8=decimate_image(I5,8); >> subplot(2,2,1),imshow(I5),title('Original Image') >> subplot(2,2,2),imshow(I5_2),title('Down-sampled N=2') >> subplot(2,2,3),imshow(I5_4),title('Down-sampled N=4') >> subplot(2,2,4),imshow(I5_8),title('Down-sampled N=8')
Image Aliasing Periodic artifacts not in the original image can be the result of under-sampling. Other related examples: the “wagon wheel” effect in movies and the “striped tie” effect on TV
Image Quantization >> I4=imread('baboon.bmp'); >> I4_5b=requantize_image(I4,5); % The parameter “5” is the bit value for quantization >> I4_2b=requantize_image(I4,2); >> I4_1b=requantize_image(I4,1); >> subplot(2,2,1),imshow(I4),title('Original Image – 8 bit Quantization') >> subplot(2,2,2),imshow(I4_5b),title('Requantized 5 bits') >> subplot(2,2,3),imshow(I4_2b),title('Requantized 2 bits') >> subplot(2,2,4),imshow(I4_1b),title('Requantized 1 bit')
Image Quantization 5 bit quantization (32 grayscale levels) is about the limit of satisfactory image quality
Some Common Arithmetic Operations on Image Matrices • Image pixels of two images could be added or subtracted. • Image pixel values can be rescaled(multiplied by a constant) to increase or decrease their intensity (brightness or color adjustment) • Images can be scrolled (matrix multiplication by a uniform translation matrix) • Image pixel values can be masked (eliminated and replaced, “copy and paste”) using element-by-element multiplication
Image Scrolling by Matrix Multiplication • Matrix multiplication is carried out by the following rule: • Matrix multiplication is not commutative; that is,
Image ScrollingUniform Translation Matrices • Square “Shift Down and Left” (SDL) Matrix: 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 • Square “Shift Up and Right” (SUR) Matrix: 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 SDL*A scrolls down one row A*SDL scrolls left one column SUR*A scrolls up one row A*SUR scrolls right one column
Scroll Down and Scroll Left Note that the “zero” row or column would appear as “black” in an image
Scroll Up and Scroll Right Note that the “zero” row or column would appear as “black” in an image
Image Scrolling >> Bu=imread('baboon.bmp'); >> B=im2double(Bu); >> SUR=shift_up(256); % B is of size 256 by 256, so the shift matrices % must be of that size >> SDL=shift_down(256); >> B_down=SDL^64*B; % Shift down by 64 rows (exponentiation) >> B_left=B_down*SUR^64; % Shift left by 64 columns (exponentiation) >> subplot(1,2,1),imshow(B),title('Original Image') >> subplot(1,2,2),imshow(B_left),title('Scrolled 64 Rows and 64 Columns' ) Successive application of the scrolling matrices is equivalent to their exponentiation
Image Masking • Square matrix (“square_mask”) to select matrix values: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 • Square matrix (“square_mask_neg”) to zero matrix values: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 A.*square_mask makes “black” all but the “ones” elements in the A matrix A.*square_mask_neg makes “black” the “zeros” elements in the A matrix
Masking and Image Addition (“Cut and Paste”) >> Bu=imread('baboon.bmp'); >> Su=imread('stripes.bmp'); >> B=im2double(Bu); >> S=im2double(Su); >> S_masked=S.*square_mask(256,100,128,64); % Mask a region of “stripes” >> B_neg_mask=B.*square_mask_neg(256,100,128,64); % Negatively mask the same % region in “baboon” >> paste=S_masked+B_neg_mask; % Add the two image matrices together >> figure, imshow(S_masked),title('Mask of "Stripes"') >> figure, imshow(B_neg_mask),title('Negative Mask of "Baboon"') >> figure, imshow(paste),title('Sum of Masked Images' )
Statistical Properties and Image Enhancement • Global statistical properties: distribution of pixel values (mean and variance) • Histogram equalization for contrast enhancement • Local statistical properties: large pixel deviations in small blocks (“shot” noise) • Noise removal by median filtering
Histogram Equalization >> I=imread('pout.tif'); >> imshow(I,'InitialMagnification','fit') >> figure,imhist(I); >> title('Histogram of Pixel Values for "pout"') >> xlabel('Pixel Value') >> ylabel('Number of Pixels') The low contrast in the image is due to the narrow distribution of pixel values.
Histogram Equalization >> I2=histeq(I); >> imshow(I2,'InitialMagnification','fit') >> figure, imhist(I2) >> title('Pixel Histogram of "pout" After Appication of Histogram Equalization') Contrast is improved with equalization
Median Filtering >> B=imread('BABOON.bmp'); >> BN=imnoise(B,'salt & pepper'); >> subplot(1,2,1), imshow(B),title('Noise-free Image') >> subplot(1,2,2), imshow(BN),title('Salt-and-Pepper Noise') Image contaminated with “salt and pepper” noise
Pixel View of Salt and Pepper Noise The noise is characterized by large deviations from the local pixel values; that is, large deviations from the local median values
Median Filtering >> B=imread('BABOON.bmp'); >> BN=imnoise(B,'salt & pepper'); >> BNf=medfilt2(BN); >> subplot(1,2,1),imshow(BN),title('Noisy Image') >> subplot(1,2,2), imshow(BNf), title('Median Filtered Image') Restoring pixels to the local median value in a 3-by-3 block eliminates the salt and pepper noise, but causes some image blurring
Linear Filtering of Images • Row and column filtering (1-D filtering) • Filter transfer functions the same as those for vector signals and applied to rows and columns independently. • Example: horizontal and vertical edge detection • Correlation and convolution kernels • 2-D filter that operates on pixel blocks • Example: blurring and de-blurring filters
Edge Detection • Object edges in an image are regions where there is a sudden change in pixel values. • A filter that approximates a first derivative should detect such changes. • The difference equation and transfer function for a simple derivative filter is:
Edge Detection >> x=[zeros(1,5),ones(1,5),zeros(1,5)]; >> h=[1,-1]; >> y=filter(h,1,x); >> subplot(2,1,1),stem(x),title('Input Signal') >> subplot(2,1,2),stem(y),title('Edge Detector (Derivative) Filter Output') This simple derivative filter gives a large response for large changes in the signal value. In this case it detects the “edges” of the pulse.
Image Edge Detection >> load echart >> imshow(echart) >> EC1=abs(imfilter(echart,h)); >> figure, imshow(EC1),title('Row-by-Row Edge Detection') >> EC2=abs(imfilter(echart,h')); >> figure,imshow(EC2),title('Column-by-Column Edge Detection') >> ET=EC1+EC2; >> figure, imshow(ET),title('Sum of Row and Column Edge Detection Images') The command “imfilter” applies the impulse response of a linear filter to the rows and columns of an image
2-Dimensional Filtering:The Correlation Kernel filtered with computes the value for the “7” matrix position as follows:
The Correlation Kernel The result of the correlation operation on all the matrix values results in the following recomputed matrix. This is the “output” of the 2-D correlation kernel.
Convolution Kernel Convolution kernel is the correlation kernel rotated 180 degrees Application of the kernel to a matrix value Result when applied to the whole matrix
Example: Image Sharpening >> H = fspecial('unsharp'); >> I = imread('moon.tif'); >> I2 = imfilter(I,H); >> subplot(1,2,1),imshow(I),title('Original Image') >> subplot(1,2,2),imshow(I2),title('Sharpened Image') >> H H = -0.1667 -0.6667 -0.1667 -0.6667 4.3333 -0.6667 -0.1667 -0.6667 -0.1667 H is the correlation kernel for the “sharpening” filter
Just as a discrete linear signal has a frequency domain representation (DFT), so does an image (the 2-D DFT) DFT definition: 2-D DFT definition: An N-by-M pixel image creates a N-by-M matrix of complex numbers representing the frequency content, X[p,q] The corner regions of the X[p,q] matrix contain the low frequency information, while the central region contains the high frequency information Discrete Fourier Transform of Images
DFT of a Simple Image >> f = zeros(30,30); >> f(5:24,13:17) = 1; >> imshow(f, 'InitialMagnification','fit'),title('30 by 30 Pixel Image') Row DFT Column DFT
Row and Column DFT >> f_row=f(15,:); >> [F_row,omega]=dft_demo(f_row); >> stem(omega/pi,abs(F_row)),title('DFT of Row 15') >> f_col=f(:,15); >> [F_col,omega]=dft_demo(f_col); >> figure,stem(omega/pi,abs(F_col)),title('DFT of Colunm 15')
DFT of the Simple Image >> F = fft2(f); >> imshow((log(abs(F))),[0 5], 'InitialMagnification','fit'); colormap(bone); colorbar >> title('2-Dimensional DFT of the Simple Image') Low frequency components are at the corners while high frequency components are in the center. Low absolute values of the transform are black
Inverse DFT for Image Reconstruction • Images can be reconstructed from their transform representation. This forms the basis of image compression schemes. >> Frecon=ifft2(F); >> imshow(Frecon, 'InitialMagnification','fit'),title('Image Reconstructed from DFT')
Summary • Images are represented by an array (matrix) of numbers representing the light intensity at a x-y position in a visual field (pixels) • In MATLAB, the matrix elements are usually represented by double precision intensity values (0 to 1) or uint8 (0 to 255). • Color images are represented by the intensities of the primary colors red, green and blue. • The principles of sampling and aliasing also apply with slightly different interpretations. • Basic image processing involves arithmetic operations on pixel values: • Scalar multiplication (intensity adjustment) • Matrix multiplication (image scrolling) • Masking (image selection) • Addition and subtraction (“cut and paste”) • The statistical properties of images can be used for image enhancement • Global: histogram equalization for contrast enhancement • Local: median filtering for noise removal • Frequency transforms (DFT and others) can be applied to images and can be used for image compression (case study in Chapter 12)