Laboratory of Image Processing Pier Luigi Mazzeo [email protected] November 4 th, 2014.

67
Laboratory of Image Processing Pier Luigi Mazzeo [email protected] November 4 th , 2014

Transcript of Laboratory of Image Processing Pier Luigi Mazzeo [email protected] November 4 th, 2014.

Page 1: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Laboratory ofImage Processing

Pier Luigi [email protected]

November 4th, 2014

Page 2: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Outline

• Introduction to MATLAB• Image Processing with MATLAB

Simple operations with images

2

Page 3: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Reference

• An Introduction to Digital Image Processing with MATLAB by Alasdair MacAndrew. (download from www.ino.it/home/mazzeo/downloads/)

• Digital Image Processing using MATLAB. 2° Edition Rafael C. Gonzales, Richard E. Woods, Steven L. Eddins. Gatesmark Publishing.

3

Page 4: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

What is MATLAB?• MATLAB = Matrix Laboratory

• “MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++ and Fortran.” (www.mathworks.com)

• MATLAB is an interactive, interpreted language that is designed for fast numerical matrix calculations

4

Page 5: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

The MATLAB Environment• MATLAB window

components:Workspace

> Displays all the defined variables

Command Window > To execute commands in

the MATLAB environmentCommand History

> Displays record of the commands used

File Editor Window> Define your

functions

5

Page 6: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

MATLAB Help• MATLAB Help is an

extremely powerful assistance to learning MATLAB

• Help not only contains the theoretical background, but also shows demos for implementation

• MATLAB Help can be opened by using the HELP pull-down menu

6

Page 7: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

MATLAB Help (cont.)• Any command description

can be found by typing the command in the search field

• As shown above, the command to take square root (sqrt) is searched

• We can also utilize MATLAB Help from the command window as shown

7

Page 8: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

More about the Workspace

• who, whos – current variables in the workspace

• save – save workspace variables to *.mat file• load – load variables from *.mat file• clear – clear workspace variables

8

Page 9: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Matrices in MATLAB

• Matrix is the main MATLAB data type• How to build a matrix?

– A=[1 2 3; 4 5 6; 7 8 9];– Creates matrix A of size 3 x 3

• Special matrices:– zeros(n,m), ones(n,m), eye(n,m), rand(), randn()

9

Page 10: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Basic Operations on Matrices

• All operators in MATLAB are defined on matrices: +, -, *, /, ^, sqrt, sin, cos, etc.

• Element-wise operators defined with a preceding dot: .*, ./, .^

• size(A) – size vector• sum(A) – columns sums vector• sum(sum(A)) – sum of all the elements

10

Page 11: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Variable Name in Matlab

• Variable naming rules

- must be unique in the first 63 characters

- must begin with a letter

- may not contain blank spaces or other types of punctuation

- may contain any combination of letters, digits, and underscores

- are case-sensitive

- should not use Matlab keyword

• Pre-defined variable names

• pi11

Page 12: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Logical Operators

• ==, <, >, (not equal) ~=, (not) ~

• find(‘condition’) – Returns indexes of A’s elements that satisfy the condition

12

Page 13: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Logical Operators (cont.)

• Example:

>>A=[7 3 5; 6 2 1], Idx=find(A<4)A=

7 3 5

6 2 1

Idx=3

4

6

13

Page 14: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Flow Control

• MATLAB has five flow control constructs:– if statement– switch statement– for loop– while loop– break statement

14

Page 15: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

if

• IF statement condition– The general form of the IF statement is

IF expressionstatements

ELSEIF expressionstatements

ELSEstatements

END

15

Page 16: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

switch

• SWITCH – Switch among several cases based on expression

• The general form of SWITCH statement is:SWITCH switch_expr

CASE case_expr,statement, …, statement

CASE {case_expr1, case_expr2, case_expr3, …}statement, …, statement…

OTHERWISEstatement, …, statement

END

16

Page 17: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

switch (cont.)

• Note:– Only the statements between the matching CASE

and the next CASE, OTHERWISE, or END are executed

– Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary)

17

Page 18: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

for

• FOR repeats statements a specific number of times

• The general form of a FOR statement is:FOR variable=expr

statements

END

18

Page 19: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

while

• WHILE repeats statements an indefinite number of times

• The general form of a WHILE statement is:WHILE expression

statements

END

19

Page 20: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Scripts and Functions

• There are two kinds of M-files:

– Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace

– Functions, which can accept input arguments and return output arguments. Internal variables are local to the function

20

Page 21: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Functions in MATLAB (cont.)• Example:

– A file called STAT.M:function [mean, stdev]=stat(x)%STAT Interesting statistics.n=length(x);mean=sum(x)/n;stdev=sqrt(sum((x-mean).^2)/n);

– Defines a new function called STAT that calculates the mean and standard deviation of a vector. Function name and file name should be the SAME!

21

Page 22: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Visualization and Graphics• plot(x,y),plot(x,sin(x)) – plot 1D function• figure, figure(k) – open a new figure• hold on, hold off – refreshing• axis([xmin xmax ymin ymax]) – change axes• title(‘figure titile’) – add title to figure• mesh(x_ax,y_ax,z_mat) – view surface• contour(z_mat) – view z as topo map• subplot(3,1,2) – locate several plots in figure

22

Page 23: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Saving your Work• save mysession

% creates mysession.mat with all variables • save mysession a b

% save only variables a and b • clear all

% clear all variables • clear a b

% clear variables a and b • load mysession

% load session 23

Page 24: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Outline

• Introduction to MATLAB• Image Processing with MATLAB

Simple operations with images

24

Page 25: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

A digital image differs from a photo in that the values are all discrete.

• Usually they take on only integer values.• A digital image can be considered as a large array of discrete

dots, each of which has a brightness associated with it. These dots are called picture elements, or more simply pixels.

• The pixels surrounding a given pixel constitute its neighborhood A neighborhood can be characterized by its shape in the same way as a matrix: we can speak of a 3x3 neighborhood, or of a 5x7 neighborhood.

Image and digital images

25

Page 26: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Pixel neighbourhood

26

Page 27: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Aspects of image processing• Image Enhancement: Processing an image so that the result is

more suitable for a particular application. (sharpening or deblurring an out of focus image, highlighting edges, improving image contrast, or brightening an image, removing noise).

• Image Restoration: This may be considered as reversing the damage done to an image by a known cause. (removing of blur caused by linear motion, removal of optical distortions).

• Image Segmentation: This involves subdividing an image intoconstituent parts, or isolating certain aspects of an image.(finding lines, circles, or particular shapes in an image, in an aerial photograph, identifying cars, trees, buildings, or roads.

27

Page 28: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

• Binary: Each pixel is just black or white. Since there are only two possible values for each pixel (0,1), we only need one bit per pixel.

• Grayscale: Each pixel is a shade of gray, normally from 0 (black) to 255 (white). This range means that each pixel can be represented by eight bits, or exactly one byte. Other greyscale ranges are used, but generally they are a power of 2.

• True Color, or RGB: Each pixel has a particular color; that color is described by the amount of red, green and blue in it. If each of these components has a range 0–255, this gives a total of 2563 different possible colors. Such an image is a “stack” of three matrices; representing the red, green and blue values for each pixel. This means that for every pixel there correspond 3 values.

Types of digital image

28

Page 29: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Binary Image

29

Page 30: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Greyscale Image

30

Page 31: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Color Image

31

Page 32: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

General commands

• imread: Read an image• figure: creates a figure on the screen.• imshow(g): which displays the matrix g as an

image.• impixel(i,j): the command returns the value of

the pixel (i,j)• iminfo: Information about the image.

32

Page 33: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Data types

33

Page 34: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Image Information

34

Page 35: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Converting between different data type

•gray2ind - intensity image to index image•im2bw - image to binary•im2double - image to double precision•im2uint8 - image to 8-bit unsigned integers•im2uint16 - image to 16-bit unsigned integers•ind2gray - indexed image to intensity image•mat2gray - matrix to intensity image•rgb2gray - RGB image to grayscale•rgb2ind - RGB image to indexed image

35

Page 36: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

• Greyscale images can be transformed into a sequence of binary images by breaking them up into their bit-planes.

• We consider the grey value of each pixel of an 8-bit image as an 8-bit binary word.

• The 0th bit plane consists of the last bit of each grey value.• Since this bit has the least effect (least significant bit

plane).• The 7th bit plane consists of the first bit in each value

(most significant bit plane).

Bit planes

36

Page 37: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Initial Image

37

Page 38: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Bit plane 0

38

Page 39: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Bit plane 4

39

Page 40: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Bit plane 7

40

Page 41: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

• Spatial resolution is the density of pixels over the image: the greater the spatial resolution, the more pixels are used to display the image.

• Halve the size of the image: It does this by taking out every other row and every other column, thus leaving only those matrix elements whose row and column indices are even.

• Double the size of the image: all the pixels are repeated to produce an image with the same size as the original, but with half the resolution in each direction.

Spatial Resolution

41

Page 42: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Interpolation

42

Page 43: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Extrapolation

43

Page 44: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

These operations act by applying a simple function y=f(x) to each gray value in the image.•Simple functions include adding or subtract a constant value to each pixel: y = x±C (imadd, imsubtract)•Multiplying each pixel by a constant: y = C·x (immultiply, imdivide)•Complement: For a grayscale image is its photographic negative.

Point Processing: Arithmetic operations

44

Page 45: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Arithmetic operations: Addition, subtraction

45

Page 46: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Arithmetic operations: Multiplication, division

46

Page 47: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Complement

47

Page 48: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Addition

Image: I Image: I+50

48

Page 49: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Subtraction

Image: I Image: I-80

49

Page 50: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Multiplication

Image: I Image: I*3

50

Page 51: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Division

Image: I Image: I/2

51

Page 52: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Complement

Image: I Image: 255-I

52

Page 53: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

What is the Image Processing Toolbox?

• The Image Processing Toolbox is a collection of functions that extend the capabilities of the MATLAB’s numeric computing environment. The toolbox supports a wide range of image processing operations, including:– Geometric operations– Neighborhood and block operations– Linear filtering and filter design– Transforms– Image analysis and enhancement– Binary image operations– Region of interest operations

53

Page 54: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

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)– raw-data and 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)

54

Page 55: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Images in MATLAB

• Binary images : {0,1}

• Intensity images : [0,1] or uint8, double etc.

• RGB images : m × n × 3

• Multidimensional images: m × n × p (p is the number of layers)

55

Page 56: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Image Import and Export

• Read and write images in Matlabimg = imread('apple.jpg');dim = size(img);figure;imshow(img);imwrite(img, 'output.bmp', 'bmp');

• Alternatives to imshow imagesc(I) imtool(I) image(I)

56

Page 57: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Images and Matrices

Column 1 to 256

Row

1 to 256

o

[0, 0]

o

[256, 256]

How to build a matrix(or image)?Intensity Image:

row = 256;col = 256;img = zeros(row, col);img(100:105, :) = 0.5;img(:, 100:105) = 1;figure;imshow(img);

57

Page 58: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Images and Matrices

Binary Image:

row = 256;col = 256;img = rand(row, col);img = round(img);figure;imshow(img);

58

Page 59: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Image Display

• image - create and display image object• imagesc - scale and display as image• imshow - display image• colorbar - display colorbar• getimage - get image data from axes• truesize - adjust display size of image• zoom - zoom in and zoom out of 2D plot

59

Page 60: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Image Conversion

• gray2ind - intensity image to index image• im2bw - image to binary• im2double - image to double precision• im2uint8 - image to 8-bit unsigned integers• im2uint16 - image to 16-bit unsigned integers• ind2gray - indexed image to intensity image• mat2gray - matrix to intensity image• rgb2gray - RGB image to grayscale• rgb2ind - RGB image to indexed image

60

Page 61: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Image Operations

• RGB image to gray image• Image resize• Image crop• Image rotate• Image histogram• Image histogram equalization• Image DCT/IDCT• Convolution

61

Page 62: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Outline

• Introduction to MATLAB– Basics & Examples

• Image Processing with MATLAB– Basics & Examples

62

Page 63: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Examples working with Images (2/3)

Blending two images

63

Page 64: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Examples working with Images (3/3)

Sobel descriptor to detect object edge

64

Page 65: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Performance Issues

• The idea: MATLAB is– very fast on vector and matrix operations– Correspondingly slow with loops

• Try to avoid loops

• Try to vectorize your codehttp://www.mathworks.com/support/tech-notes/1100/1109.html

65

Page 66: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Vectorize Loops• Example

– Given image matrices, A and B, of the same size (540*380), blend these two imagesapple = imread(‘apple.jpg'); orange = imread(‘orange.jpg’);

• Poor Style % measure performance using stopwatch timer

tic for i = 1 : size(apple, 1)

for j = 1 : size(apple, 2) for k = 1 : size(apple, 3)

output(i, j, k) = (apple(i, j, k) + orange(i, j, k))/2;

end end

end toc

• Elapsed time is 0.138116 seconds66

Page 67: Laboratory of Image Processing Pier Luigi Mazzeo pierluigi.mazzeo@cnr.it November 4 th, 2014.

Vectorize Loops (cont.)

• Example– Given image matrices, A and B, of the same size (600*400),

blend these two images apple = imread(‘apple.jpg'); orange = imread(‘orange.jpg’);

• Better Styletic % measure performance using stopwatch timerOutput = (apple + orange)/2;toc• Elapsed time is 0.099802 seconds

• Computation is faster!

67