Image Processing using Matlab_图文

Image Processing using Matlab

Sumitha Balasuriya
http//www.dcs.gla.ac.uk/~sumitha/

Images in Matlab
? Matlab is optimised for operating on matrices ? Images are matrices! ? Many useful built-in functions in the Matlab Image Processing Toolbox ? Very easy to write your own image processing functions
Image Processing using Matlab Sumitha Balasuriya 2

Loading and displaying images
>> I=imread('mandrill.bmp','bmp'); % load image

Matrix with image data

image filename as a string

image format as a string

>> image(I) % display image >> whos I
Name I Size 512x512x3 Bytes Class

786432 uint8 array

Grand total is 786432 elements using 786432 bytes

Dimensions of I (red, green and blue intensity information)
Image Processing using Matlab

Matlab can only perform arithmetic operations on data with class double!
Sumitha Balasuriya

Display the left half of the mandrill image 3

Representation of Images
? Images are just an array of numbers >> I % ctrl+c to halt output! ? Intensity of each pixel is represented by the pixel element’s value in the red, green and blue matrices >> I(1,1,:) % RGB values of element (1,1) ans(:,:,1) = Red 135 Images where the pixel value in the image represents the intensity of the pixel are called ans(:,:,2) = intensity images. Green 97 ans(:,:,3) = 33 Blue
Image Processing using Matlab Sumitha Balasuriya 4

Indexed images
? ? An indexed image is where the pixel values are indices to elements in a colour map or colour lookup table. The colour map will contain entries corresponding to red, green and blue intensities for each index in the image.
3 5 2 5 1 5 7 9 4 6 8 1 9 9 6 2 8 5 7 14 9 11 5 2 8 3 6 10 3 4 4 5 6 1 18 7 7 8 6 19 2 2 6 5 5 13 4 5 8 6 7 1 14 3 1 9 1 8 7 4 6 9 1 4 4 4 6 1 13 6 1 6 2 5 5 4 5 9 3 4 3 4

>> jet(20) % Generate a jet colourmap for 20 indices
ans = 0 0 0 0 0 0 0 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 0.6000 0 0 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 0.6000 0.4000 0.2000 0 0 0 0.6000 0.8000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8000 0.6000 0.4000 0.2000 0 0 0 0 0 0 0 0

RGB Entry for index value 3

11 1 1 2 4 4 8 9 15 10

8 16 8 4 2 3

Values can range from 0.0 to 1.0

7 7

Red, green and blue intensities of the nearest index in the colourmap are used to display the image.
Sumitha Balasuriya 5

Image Processing using Matlab

Displaying indexed images
>> I2=I(:,:,2); % green values of I image as it >> image(I2) Matlab considers I2 as an indexedblue entries doesn’t contain entries for red, green and >> colorbar % display colourmap
Index Associated color Colour Lookup Table

Image Processing using Matlab

Sumitha Balasuriya

6

Displaying indexed images
? change colourmap >> colormap(gray)
Type >>help graph3d to get a list of built-in colourmaps. Experiment with different built-in colourmaps. Define your own colourmap mymap by creating a matrix (size m x 3 ) with red, green, blue entries. Display an image using your colourmap.

(continued)
Red =1.0, Green = 1.0, Blue =1.0, corresponds to index 64

Red =0.0, Green = 0.0, Blue = 0.0, corresponds to index 1 Red =1.0, Green = 1.0, Blue =1.0, corresponds to index 255

? scale colourmap >> imagesc(I2)
Image Processing using Matlab Sumitha Balasuriya

Red =0.0, Green = 0.0, Blue = 0.0, corresponds to index 0

7

Useful functions for displaying images
>> axis image % plot fits to data >> h=axes('position', [0 0 0.5 0.5]); >> axes(h); >> imagesc(I2)

Investigate axis and axes functions using Matlab’s help

Image Processing using Matlab

Sumitha Balasuriya

8

Histograms
? Frequency of the intensity values of the image ? Quantise frequency into intervals (called bins) ? (Un-normalised) probability density function of image intensities

Image Processing using Matlab

Sumitha Balasuriya

9

Computing histograms of images in Matlab
>>hist(reshape(double(Lena(:,:,2)),[512*512 1]),50)
Histogram function Convert image into a 262144 by 1 distribution of values Number of bins

Generate the histograms of the green channel of the Lena image using the following number of bins : 10, 20, 50, 100, 200, 500, 1000 Histogram equalisation works by equitably distributing the pixels among the histogram bins. Histogram equalise the green channel of the Lena image using Matlab’s histeq function. Compare the equalised image with the original. Display the histogram of the equalised image. The number of pixels in each bin should be approximately equal.
Image Processing using Matlab Sumitha Balasuriya 10

Visualising the intensity surface
>>surf(double(imresize(Lena(:,:,2),[50 50])))

Change type to double precision

Remember to reduce size of image!

Use Matlab’s built-in mesh and shading surface visualisation functions

Image Processing using Matlab

Sumitha Balasuriya

11

Useful functions for manipulating images
? Convert image to grayscale >>Igray=rgb2gray(I); ? Resize image >>Ismall=imresize(I,[100 100], 'bilinear'); ? Rotate image >>I90=imrotate(I,90);
Image Processing using Matlab Sumitha Balasuriya 12

Other useful functions
Convert polar coordinates to cartesian coordinates >>pol2cart(rho,theta) Convert polar coordinates to cartesian coordinates >>cart2pol(x,y) Get size of matrix >>size(I) Check if a variable is null >>isempty(I) Find indices and elements in a matrix >>[X,Y]=find(I>100) Change the dimensions of a matrix >>reshape(rand(10,10),[100 1]) Trigonometric functions sin, cos, tan Fast Fourier Transform fft2(I)

Discrete Cosine Transform dct(I)

Add elements of a Matrix
(columnwise addition in matrices)

>>sum(I)

Exponentials and Logarithms exp log log10
Sumitha Balasuriya 13

Image Processing using Matlab

Convolution
Bit of theory! Convolution of two functions f(x) and g(x)
??

h( x ) ? f ( x ) ? g ( x ) ?

??

?

f (r ) g ( x ? r )dr

Output filtered image

Image

convolution operator

Filter (mask/kernel)

Support region of filter where g(x-r) is nonzero

Discrete image processing 2D form
H ( x, y ) ?
height width j ?1 i ?1

? ? I (i, j )M ( x ? i, y ? j )
Compute the convolution where there are valid indices in the kernel
Sumitha Balasuriya 14

Image Processing using Matlab

Convolution example
Image (I)
197 194 194 197 194 194 201 196 192 201 199 194 194 187 190 194 194 194 194 190 195 198 198 195 198 190 191 195 199 187 194 201 195 198 193 190 186 191 192 189 189 189 186 185 187 179 186 183 191 178 190 196 191 186 177 177 181 164 174 168 132 150 109 115 88 93 74 77 89 90 90 85 90 78 86 99 110 119 164 82 112 87 90 81 94 95 82 84 103 88 101 97 124 96 69 100 76 88 129 84

Filter (M)

?

j

1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9
i

=

0 0 0 0 0 0 0 0 0 0

0 196 195 194 193 194 194 195 194 0

0 196 196 194 193 192 192 193 192 0

0 194 194 193 191 189 188 190 189 0

0 192 192 189 186 184 182 183 181 0

0 170 167 158 154 149 146 147 146 0

0 137 133 124 122 121 122 128 125 0

0 105 98 92 92 91 93 100 100 0

0 97 92 90 89 90 95 106 105 0

0 0 0 0 0 0 0 0 0 0

H ( x, y ) ?

height width j ?1 i ?1

? ? I (i, j )M ( x ? i, y ? j )
Write your own convolution function myconv.m to perform a convolution. It should accept two parameters – the input matrix (image) and convolution kernel, and output the filtered matrix. 15

http://www.s2.chalmers.se/undergraduate/courses0203/ess060/PDFdocuments/ForScreen/Notes/Convolution.pdf

Image Processing using Matlab

Sumitha Balasuriya

Convolution example in 1D
Horizontal slice from Mandrill image 1D Gaussian filter Filtered Signal

?

=

0.01 0.08 0.24 0.34 0.24 0.08 0.01

Image Processing using Matlab

Sumitha Balasuriya

16

Common convolution kernels
0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11
Arithmetic mean filter (smoothing) >>fspecial('average')

-0.17 -0.67 -0.17 -0.67 3.33 -0.67 -0.17 -0.67 -0.17
Laplacian (enhance edges) >>fspecial('laplacian')

-0.17 -0.67 -0.17 -0.67 4.33 -0.67 -0.17 -0.67 -0.17
Sharpening filter >>fspecial('unsharp')

0.01 0.08 0.01 0.08 0.62 0.08 0.01 0.08 0.01
Gaussian filter (smoothing) >>fspecial('gaussian')

1 2 1

0 0 0

-1 -2 -1

1 0 -1

2 0 -2

1 0 -1

Investigate the listed kernels in Matlab by performing convolutions on the Mandrill and Lena images. Study the effects of different kernel sizes (3x3, 9x9, 25x25) on the output.

Sobel operators (edge detection in x and y directions) >>fspecial('sobel') >>fspecial('sobel')’

The median filter is used for noise reduction. It works by replacing a pixel value with the median of its neighbourhood pixel values (vs the mean filter which uses the mean of the neighbourhood pixel values). Apply Matlab’s median filter function medfilt2 on the Mandrill and Lena images. Remember to use different filter sizes (3x3, 9x9, 16x16). Sumitha Balasuriya 17

Image Processing using Matlab

Useful functions for convolution
? Generate useful filters for convolution
>>fspecial('gaussian',[kernel_height kernel_width],sigma)

? 1D convolution
>>conv(signal,filter)

? 2D convolution
>>conv2(double(I(:,:,2)),fspecial('gaussian‘,[kernel_height kernel_width] ,sigma),'valid')
image kernel Border padding options Perform the convolution of an image using Gaussian kernels with different sizes and standard deviations and display the output images.
Image Processing using Matlab Sumitha Balasuriya 18

Tutorial 2
1) 2) 3) Type the code in this handout in Matlab and investigate the results. Do the exercises in the notes. Create a grating2d.m function that generates a 2D steerable spatial frequency. Compute spatial frequencies with an amplitude =1 and the following parameters frequency = 1/50, 1/25, 1/10, 1/5 cycles per pixel, phase= 0, pi/5, pi/4, pi/3, pi/2, pi theta = 0, pi/5, pi/4, pi/3, pi/2, pi The value for pi is returned by the in-built matlab function pi. Display your gratings using the in-built gray colourmap. (figure 1) Create a superposition of two or more gratings with different frequencies and thetas and display the result. You can do this by simply adding the images you generated with grating2d (figure 2) frequency = (1/10 and 1/20), (1/20 and 1/30) theta = (pi/2 and pi/5), (pi/10 and pi/2), (pi/2 and pi) Make sure you examine combinations of different frequencies and theta values. (figure 3). Visualise the intensity surface of the outputs that you have generated. (figure 4) Write a matlab function that segments a greyscale image based on a given threshold (i.e. display pixels values greater than the threshold value, zero otherwise). The function should accept two inputs, the image matrix and the threshold value, and output the thresholded image matrix. (figure 5) function H=grating2d(f,phi,theta,A) % function to generate a 2D grating image % f = frequency % phi = phase % theta = angle % A = amplitude % H=grating2d(f,phi,theta,A) % size of grating height=100; width=100; wr=2*pi*f; % angular frequency wx=wr*cos(theta); wy=wr*sin(theta); for y=1:height for x=1:width H(x,y)=A*cos(wx*(x)+phi+wy*(y)); end end

3)

4)

Figure 1

Figure 2

Figure 3

Figure 4

Figure 5

Image Processing using Matlab

Sumitha Balasuriya

19


相关文档

Image Processing in MATLAB
Image Processing and Matlab
DISTRIBUTED MATLAB BASED SIGNAL AND IMAGE PROCESSING USING JAVAPORTS
Digital Image Processing with Matlab
Simulation Of Image Enhancement Techniques Using Matlab(基于MATLAB图像增强技术)
Digital Image Processing in Matlab 试验
Digital Signal Processing Using Matlab V.4 Chapt1
USING MATLAB TO CREATE AN IMAGE FROM RADAR
Three-Dimensional Image Sensing, Visualization, and Processing Using Integral Imaging
电脑版