Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Image Processing with MATLAB

Similar presentations


Presentation on theme: "Introduction to Image Processing with MATLAB"— Presentation transcript:

1 Introduction to Image Processing with MATLAB
EE4212 GA: Cui Zhaopeng January, 2014

2 Outline Basics about images in MATLAB Some useful commands
(Some slides are adopted from Amin Allalou) Some useful commands Performance issues Simple examples

3 MATLAB and Images An image in MATLAB is treated as a matrix
Every pixel is a matrix element All the operators in MATLAB defined on matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.

4 Images in MATLAB MATLAB can import/export several image formats
BMP (Microsoft Windows Bitmap) GIF (Graphics Interchange Files) HDF (Hierarchical Data Format) JPEG (Joint Photographic Experts Group) PCX (Paintbrush) PNG (Portable Network Graphics) TIFF (Tagged Image File Format) XWD (X Window Dump) MATLAB can also load raw-data or other types of image data Data types in MATLAB Double (64-bit double-precision floating point) Single (32-bit single-precision floating point) Int32 (32-bit signed integer) Int16 (16-bit signed integer) Int8 (8-bit signed integer) Uint32 (32-bit unsigned integer) Uint16 (16-bit unsigned integer) Uint8 (8-bit unsigned integer)

5 Images in MATLAB • Binary images : {0,1} • Intensity images : [0,1] or uint8, double etc. • RGB images : m-by-n-by-3 • Indexed images : m-by-3 color map • Multidimensional images m-by-n-by-p (p is the number of layers)

6 Image Import and Export
Read and write images in Matlab I=imread(LED_RGB.jpg'); figure,imshow(I) Igrey=rgb2gray(I); figure(2); imshow(Igrey) imwrite(lgrey, 'LED_GRAY.jpg', 'jpg') Alternatives to imshow image -- create and display image object imagesc -- scale and display as image imtool -- opens a new Image Tool

7 Image Sizes and RGB Planes
Sizes of images: Show red plane >> size(I) ans = 266 R G B The RGB image is a 3D matrix of RGB planes 257 Note: the size of an image matrix in MATLAB is different from that shown in other softwares. >> I_R = I; >> I_R(:,:,2:3) = 0; >> figure(3),imshow(I_R)

8 Images and Matrices How to build a matrix (or image)?
>> B = zeros(3,3) B = >> C = ones(3,3) C = >>imshow(A,[],'InitialMagnification','fit')

9 Images and Matrices Accesing image elements (row, column)
X Y Accesing image elements (row, column) : can be used to extract a whole column or row or a part of a column or row >> A(2,1) ans = 4 >> A(:,2) ans = 2 5 8 A = >> A(1:2,2) ans = 2 5

10 Image Arithmetic Arithmetic operations such as addition, subtraction, multiplication and division can be applied to images in MATLAB +, -, *, / performs matrix operations To perform an elementwise operation use . (.*, ./, .*, .^ etc) >> A+A ans = A = >> A*A ans = >> A.*A ans =

11 Some Useful Commands Image Display
imagesc(I) - scale and display as image imshow(I) - display image getimage - get image data from axes truesize - adjust display size of image plot(x,y,’b*’) - plot points given by the coordinates (x,y); The point plotted is a blue asterisk’*’. See the help page for more information about colors and markers. “hold on” causes subsequent image operations to display results on top of image, instead of replacing image.

12 Some Useful Commands Image Conversion im2bw(I) - image to binary
im2double(I) - image to double precision im2uint8(I) - image to 8-bit unsigned integers im2uint16(I) - image to 16-bit unsigned integers mat2gray(A) - matrix to intensity image rgb2gray(I) - RGB image to grayscale

13 Some Useful Commands Matrix Transformation
B = reshape(A,m,n) returns the mn matrix B whose elements are taken columnwise from matrix A . The product of the specified dimensions, m*n, must be the same as prod(size(A)). B = repmat(A,m,n) creates a large matrix consisting of an m-by-n tiling of copies of A. The size of B is [size(A,1)*m, (size(A,2)*n]. >> A = [1 2 3; 4 5 6] A = >> B =reshape(A,3,2) B = >> C = reshape(A,1,[]) C = ‘[]’ here means that the length of the dimension will be calculated such that the product of the dimensions equals prod(size(A)).

14 Some Useful Commands Others Use the help page to find more functions.
diag(A), where A is a vector with n components, returns an n-by-n diagonal matrix having A as its main diagonal. If A is a square symbolic matrix, diag(A) returns the main diagonal of A. M = dlmread(filename) reads from the ASCII-delimited numeric data file filename to output matrix M. The filename input is a string enclosed in single quotes, e.g. ‘myfile.txt’. dlmwrite(filename, M) writes matrix M into an ASCII format file using the default delimiter (,) to separate matrix elements. [U, S, V] = svd(A) return numeric unitary matrices U and V with the columns containing the singular vectors and a diagonal matrix S containing the singular values. listing = dir('name') returns attributes about name to an m-by-1 structure, where m is the number of files and folders in name. cpselect(‘name1’, ‘name2’) displays 2 images side by side, allowing you to click on corresponding points. Use the help page to find more functions.

15 Performance Issues Characters of MATLAB Improve the performance
very fast on vector and matrix operations; Correspondingly slow with loops; Improve the performance Try to preallocate arrays; Try to avoid loops; Try to vectorize code. Use stopwatch or profile to identify where you need to work on speed improvements.

16 Example (1) Generate a sequence with 𝑥 𝑘 = 𝑥 𝑘−1 +5, 𝑘=2~ 𝑥 1 =0 % Poor style tic x = 0; for k = 2: x(k) = x(k-1) + 5; end toc Elapsed time is seconds. % Better style tic x = zeros(1, ); for k = 2: x(k) = x(k-1) + 5; end toc Elapsed time is seconds. Preallocating the vector makes it faster. Results are from

17 Example (2) Given two image matrices FImg and Bimg with the same size (400*600*3), blend these two images using a matrix Alpha (400*600). clear;clc; FImg = im2double(imread('Foreground.jpg')); figure;imshow(FImg); BImg = im2double(imread('Background.jpg')); figure;imshow(BImg); Alpha = dlmread('Alpha.txt');

18 Example (2) We can vectorize this part to avoid loops. % Poor style
tic % measure performance using stopwatch timer BlendingImg = zeros(size(FImg)); for i = 1 : size(FImg, 1) for j = 1 : size(FImg, 2) for k = 1 : size(FImg, 3) BlendingImg(i, j, k) = (FImg(i, j, k)*Alpha(i,j) + (1-Alpha(i,j))*BImg(i, j, k)); end toc Elapsed time is seconds. We can vectorize this part to avoid loops. % Better style tic % measure performance using stopwatch timer BlendingImg = FImg.*repmat(Alpha,[1,1,3]) + BImg.*repmat(1-Alpha,[1,1,3]); toc Elapsed time is seconds.

19 Example (3) Compute Eigenfaces. (You are not expected to know the detail.) % Compute Eigenfaces clear; clc; %Some parameters ImgSize = [64 64]; VectorDim = prod(ImgSize); %Read images from files FileName = './Faces/'; FaceImgs = dir([FileName '*.png']); FaceNum = length(FaceImgs); InputFaces = zeros(VectorDim,FaceNum); for i=1:FaceNum InputFaces(:,i) = reshape(imread([FileName FaceImgs(i).name]),[],1); end % Preallocating the matrix

20 Example (3) %Calculate the mean face and shifted faces
MeanFace = mean(InputFaces, 2); ShiftedFaces = InputFaces - repmat(MeanFace, 1, FaceNum); %Calculate the eigenvectors and eigenvalues using SVD [U S V] = svd(ShiftedFaces); %Display the top 10 EigenFaces figure; title('The first 10 Eigenfaces.'); for n = 1:10 subplot(2, 5, n); Temp = (255/(max(U(:,n))-min(U(:,n)))).* (U(:,n)-min(U(:,n))); %scale the vector to [0 255] imshow(uint8(reshape(Temp, ImgSize))); end % Vectorizing code to avoid loops

21 Example (4) Compute the homography matrix between two sets of points.
There are two sets of 2d points 𝑋 1 , 𝑋 2 , 𝑋 3 …, 𝑋 𝑚 and 𝑋 1 ′ , 𝑋 2 ′ , 𝑋 3 ′ …, 𝑋 𝑚 ′ . 𝑋 𝑛 =[ 𝑥 𝑛 , 𝑦 𝑛 , 𝑧 𝑛 ]′ and 𝑋 𝑛 ′ =[ 𝑥 𝑛 ′ , 𝑦 𝑛 ′ , 𝑧 𝑛 ′ ]′ (𝑛=1,…,𝑚)are homogenous vectors. We want to compute the homography matrix 𝐻 which satisfies 𝑥 𝑛 ′ 𝑦 𝑛 ′ 𝑧 𝑛 ′ =𝜆 ℎ 11 ℎ 12 ℎ 13 ℎ 21 ℎ 22 ℎ 23 ℎ 31 ℎ 32 ℎ 𝑥 𝑛 𝑦 𝑛 𝑧 𝑛 , 𝑤ℎ𝑒𝑟𝑒 𝐻= ℎ 11 ℎ 12 ℎ 13 ℎ 21 ℎ 22 ℎ 23 ℎ 31 ℎ 32 ℎ After simple derivation, we can get 𝐻 by solving the following linear system, 𝑥 1 𝑧 1 ′ 𝑦 1 𝑧 1 ′ 𝑧 1 𝑧 1 ′ −𝑥 1 𝑥 1 ′ −𝑦 1 𝑥 1 ′ −𝑧 1 𝑥 1 ′ 𝑥 1 𝑧 1 ′ 𝑦 1 𝑧 1 ′ 𝑧 1 𝑧 1 ′ −𝑥 1 𝑦 1 ′ −𝑦 1 𝑦 1 ′ −𝑧 1 𝑦 1 ′ 𝑥 2 𝑧 2 ′ 𝑦 2 𝑧 2 ′ 𝑧 2 𝑧 2 ′ −𝑥 2 𝑥 2 ′ −𝑦 2 𝑥 2 ′ −𝑧 2 𝑥 2 ′ 𝑥 2 𝑧 2 ′ 𝑦 2 𝑧 2 ′ 𝑧 2 𝑧 2 ′ −𝑥 2 𝑦 2 ′ −𝑦 2 𝑦 2 ′ −𝑧 2 𝑦 2 ′ … … 𝑥 𝑚 𝑧 𝑚 ′ 𝑦 𝑚 𝑧 𝑚 ′ 𝑧 𝑚 𝑧 𝑚 ′ −𝑥 𝑚 𝑥 𝑚 ′ −𝑦 𝑚 𝑥 𝑚 ′ −𝑧 𝑚 𝑥 𝑚 ′ 𝑥 𝑚 𝑧 𝑚 ′ 𝑦 𝑚 𝑧 𝑚 ′ 𝑧 𝑚 𝑧 𝑚 ′ −𝑥 𝑚 𝑦 𝑚 ′ −𝑦 𝑚 𝑦 𝑚 ′ −𝑧 𝑚 𝑦 𝑚 ′ ℎ 11 ℎ 12 ℎ 13 ℎ 21 ℎ 22 ℎ 23 ℎ 31 ℎ 32 ℎ 33 =0

22 Example (4) Compute the homography matrix between two sets of points.
%Poor style clear; clc; load points1.txt; %suppose the points have been normalized load points2.txt; for i=1:size(points1,1) A(2*i-1,1:3) = points1(i,:)*points2(i,3); A(2*i-1,4:6) = 0; A(2*i-1,7:9) = -points1(i,:)*points2(i,1); A(2*i,1:3) = 0; A(2*i,4:6) = points1(i,:)*points2(i,3); A(2*i,7:9) = -points1(i,:)*points2(i,2); End % Extract nullspace [U S V] = svd(A); s = diag(S); nullspace_dimension = sum(s < eps * s(1) * 1e3); if nullspace_dimension > 1 fprintf('Nullspace is a bit roomy...'); end h = V(:,9); H = reshape(h,3,3)' % It does not preallocate A here, %e.g. A = zeros(size(points1,1)*2,9). %Actually we do not need loops here.

23 Example (4) Compute Homography between two sets of points
%Better style clear; clc; load points1.txt; %suppose the points have been normalized load points2.txt; Num = size(points1,1); A=[points1.*repmat(points2(:,3), 1,3), zeros(Num,3), -points1.*repmat(points2(:,1), 1,3); zeros(Num,3), points1.*repmat(points2(:,3), 1,3), -points1.*repmat(points2(:,2), 1,3)]; % Extract nullspace [U S V] = svd(A); s = diag(S); nullspace_dimension = sum(s < eps * s(1) * 1e3); if nullspace_dimension > 1 fprintf('Nullspace is a bit roomy...'); end h = V(:,9); H = reshape(h,3,3)' % Vectorizing code to avoid loops

24 Useful Links Online tutorial or codes:
Writing Fast MATLAB Code: Profiling, vectorization, and more. VGG MultiView Compute Library: Codes for some basic algorithms in Computer Vision A basic tutorial for MATLAB Image Processing Toolbox: MATLAB documentation:

25 GA Hours My contact: Laboratory: E (Note: As E4 may be renovated this semester, I will inform you if there is a change.) Friday 4:00-5:30pm (Week 2, 3, 5, 7, 8, 9, 10, 11, 12, 13)


Download ppt "Introduction to Image Processing with MATLAB"

Similar presentations


Ads by Google