Download presentation
Presentation is loading. Please wait.
1
Image Processing 2012 Spring IELAB
Ch1. Fundamentals Digital Image Processing using MATLAB, 2nd edition, C.Gonzalez Image Processing 2012 Spring IELAB
2
Contents 1.1 Digital Image Representation 1.2 Reading Images
1.3 Displaying Images 1.4 Writing Images 1.5 Classes 1.6 Image Types 1.7 Converting between Classes 1.8 Array Indexing 1.9 Some Important Standard Arrays 1.10 Introduction to M-function programming
3
1.1 Digital Image Representation
Image can be defined as a 2 dimensional function Amplitude of coordinate is called intensity Gray level refers to intensity of monochrome images RGB consists of three monochrome component images f (x,y)
4
1.1 Digital Image Representation
What is digital images? Digitalizing coordinate value means Sampling Digitalizing amplitude value means Quantization We can consider the digital image as a form with finite coordinate value and finite amplitude value.
5
1.1.1 Coordinate Conventions
A matrix is a result of a sampling and quantization process. Coordinate conventions of MATLAB are different from those of C language In C , we start our number notation with 0. This type of system is called zero-based. 0,1,2,3,4 In MATLAB, we start our number notation with 1. This type of system is called one-based. 1,2,3,4,5
6
1.1.1 Coordinate Conventions
C Language MATLAB ( Image Processing Toolbox)
7
1.1.1 Coordinate Conventions
Main textbook introduces two kinds of coordinate conventions. Pixel Coordinate This coordinate convention is used in this book. This convention uses ‘x’ as row and ‘y’ as column. Spatial coordinate This coordinate convention widely used in other applications. This convention has an advantage to define coordinate using the Cartesian convention. ‘x’ value is used to represent column and y value represents row.
8
1.1.2 Images as Matrix Mathematical Representation MATLAB matrix
9
Dimension of the matrix
1.1.2 Images as Matrix MATLAB considers all variables as matrices Variable naming convention of MATLAB is same as C language. Variables must start with an non-numeric character. A, a, RGB, real_array Dimension of the matrix Representation Row vector Column vector Scalar
10
1.2 Reading Images The imread function can be used to acquire an image data from the file imread(‘filename’) The filename must include the extension of the image .bmp, .jpeg, etc If the file is not located in the ‘current directory’ the path name of the file must be written in full f = imread(‘D:\MyDocuments\Picture.jpg’) .
11
1.2 Reading Images Some other useful functions in MATLAB
The size function calculates a size of a given matrix The whos function gives information for given matrix such as size, byte information, and class type. .
12
1.3 Displaying Images In order to display image files, imshow function is used imshow ( f ) We can scale an image by using below notation imshow( f , [ low, high ] ) Values that are smaller than ‘low’, value becomes black Values that are greater than ‘high’ value becomes white The values in the middle will be scaled accordingly.
13
1.3 Displaying Images Result of imshow(f)
14
1.3 Displaying Images Another way of scaling imshow( f, [ ])
This method will automatically set the lowest value in f as ‘low’ and the highest value in f as ‘high’.
15
1.3 Displaying Images Another useful function in MATLAB is the imtool function imtool( f )
16
1.3 Displaying Images An example of the Image Tool window
17
1.4 Writing Images Images can be written to the current directory using imwrite( f, ‘filename’ ) The file will be saved in the ‘current directory’ The imwrite function can also reduce the quality of the saved image imwrite( f, ‘filename.jpg’, quality, q) q = 0~100
18
1.4 Writing Images Example of reduced qualtiy via saving
19
1.4 Writing Images In order to obtain detail of an image, we can use
imfinfo filename
20
1.4 Writing Images The information from imfinfo can be saved in a variable to be called on later for other operations
21
1.4 Writing Images A more general version of the imwrite is as below:
‘Parameter’ This variable determines the type of compression to be done on the image ‘colres’ determines the column resolution ‘rowres’ determines the row resolution If colres is equal to rowres, we can just specify a single scalar value.
22
1.4 Writing Images Example of resolution changing
imwrite( f, ‘sf.tif’, ‘compression’, ‘none’, ‘resolution’, res )
23
1.4 Writing Images Able to save content using ‘Save As’
In order to obtain more control, the below is used : no : refers to the figure number in the figure windows fileformat : refers to the format that user wish to save resno : refers to resolution value in dpi filename : refers to the name of the file that will be saved
24
1.5 Classes
25
1.6 Image Types Gray-scale images Binary images Indexed images
RGB images We are going to focus on the monochrome image processing. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing. Therefore we ignore Indexed image type and RGB image type.
26
1.6.1 Gray-scale images Gray-scale images is a data matrix whose values represent shades of gray. When the elements of a gray-scale image are of class uint8 or uint16, they have integer values in the range [0, 255] or [0, 65535] First, let’s check out gray-scale images. Gray-scale images only have intensity variables. This is the difference between RGB images. MATLAB
27
1.6.2 Binary images A binary image is a logical array of 0s and 1s.
A numerical array can be converted to binary using function logical. B = logical(A) We can use islogical function to test whether an array is of class logical or not. islogical(C) Binary images is a array that is consisted of 0s and 1s. And we can make binary images by using logical function. Furthermore using islogical function to determine whether array is logical or not.
28
1.6.3 A Note on Terminology Class Image type
Data type for pixel values Image type Image is characterized both class and image type For instance, “uint8 gray-scale image” refers to a gray scale image whose pixels are of class uint8. Previous chapters, we used terminologies for class and image type. and we should know that they have different meaning. Class is a type of array’s element.
29
1.7 Converting between Classes (1)
Converting images from one class to another is a common operation. When converting between classes, keep mind the value ranges of the classes being converted. The general syntax for class conversion is like below. B = class_name(A) We used to convert data types in C language. MATLAB is not different and converting syntax is also same.
30
1.7 Converting between Classes (2)
MATLAB provides specific functions that perform the scaling and other bookkeeping necessary to convert images from one class to another. Name Converts Input to : Valid Input Image Data Classes im2uint8 uint8 logical, uint8, uint16, int16, single, and double Im2uint16 uint16 im2double double im2single single mat2gray double in the range [0, 1] logical, uint8, int8, uint16, int16, uint32, int32, single, and double im2bw logical uint8, uint16, int16, single, and double Using above table’s functions we can convert class of matrix to another.
31
1.7 Converting between Classes (3)
Toolbox function mat2gray converts an image of any of the classes in table to an array of class double scaled to the range [0, 1]. g = mat2gray(A, [Amin, Amax]) Sets the values of Amin and Amax to the actual minimum and maximum values in A. Function logical converts an input array to a logical array. Alternative conversion procedure that often is more useful is to use a relationship operator, such as >, with a threshold value. g = f > T
32
1.7 Converting between Classes (3)
Toolbox function im2bw performs thresholding operation in a way that automatically scales the specified threshold in different way, depending on the class of the input image. The syntax is G = im2bw(f, T) Values specified for the threshold T must be in the range [0, 1], regardless of the class of the input.
33
1.7 Converting between Classes (4)
Example for converting between image classes. >> f = [1 2; 3 4] f = 1 2 3 4 >> g = mat2gray(f) g = >> gb = im2bw(g, 0.6) gb = 0 0 1 1 >> logic = logical(f) logic =
34
1.8 Array Indexing MATLAB supports a number of powerful indexing schemes that simplify array manipulation and improve the efficiency of programs. In this section, we discuss and illustrate basic indexing in one and two dimensions, as well as indexing techniques useful with binary images. In MATLAB there are many ways to indexing arrays. Let’s check out those ways.
35
1.8.1 Indexing Vectors (1) The elements of such a vector can be accessed using a single index value (also called subscript). A row vector is converted to a column vector (and vice versa) using the transpose operator(.’); In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
36
1.8.1 Indexing Vectors (2) To access blocks of elements, we use MATLAB’s colon notation. Indexing is not restricted to contiguous elements. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
37
1.8.2 Indexing Matrices (1) We select elements in a matrix just as we did for vectors, but now we need two indices: one to establish a row location, and the other for the corresponding column. A submatrix of A can be extracted by specifying a vector of values for both the row and the column indices. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
38
1.8.2 Indexing Matrices (2) The notation A([a b], [c d]) selects the elements in A with coordinates (a, c), (a, d), (b, c), (b, d). The row or column index can also be a single colon. A colon in the row or column index position is shorthand notation for selecting all rows or all columns. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
39
1.8.3 Indexing with a Single Colon
The use of a single colon as an index into a matrix selects all the elements of the array and arranges them into a single column vector. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
40
1.8.4 Logical indexing A logical indexing expression has the form A(D), where A is an array and D is logical array of the same size as A. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
41
1.8.5 Linear Indexing (1) A linear indexing expression is one that uses a single subscript to index a matrix or higher dimensional array. Linear indexing treats order of matrix like below. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
42
1.8.5 Linear Indexing (2) Example of linear indexing
In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
43
1.8.5 Linear Indexing (3) MATLAB functions sub2in and ind2sub convert back and forth between row-column subscripts and linear indices. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
44
Example Original Image
In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
45
1.8.6 Selecting Array Dimensions
In order to get information about dimensions of array, we can use MATLAB function size and ndims etc. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
46
1.8.7 Sparse Matrices What is the definition of sparse matrices?
Sparse matrix is a matrix populated primarily with zeros. Function sparse converts a matrix to sparse form by “squeezing out” all zero elements. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
47
1.9 Some Important Standard Arrays
zeros(M, N) generates an M by N matrix of 0s of class double. ones(M, N) generates an M by N matrix of 1s of class double. true(M, N) generates an M by N logical matrix of 1s. false(M, N) generates an M by N logical matrix of 0s. magic(M) generates an M by M “magic square”. eye(M) generates M by M identity matrix. rand(M, N) generates an M by N matrix whose entries are uniformly distributed random numbers in the interval [0, 1] randn(M, N) generates an M by N matrix whose entries are normally distributed random numbers with mean 0 and variance 1. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
48
1.10 Introduction to M-Function Programing
One of the most powerful features of MATLAB is the capability it provides users to program their own new functions. As you will learn shortly, MATLAB function programming is flexible and particularly easy to learn M-file is similar to .c-file or .cpp-file for C/C++ language In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
49
M-Files (1) M-files in MATLAB can be scripts that simply execute a series of MATLAB statements, or they can be functions that can accept arguments and can produce on or more outputs. The components of a function M-file are The function definition line The H1 line Help text The function body Comments In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
50
M-Files (2) The function definition line has the form like below. function [outputs] = name(inputs) The H1 line is the first text line. It is a single comment line that follows the function definition line. %SUMPROD Computes the sum and product of two images Help text is a text block that follows the H1 line, without any blank lines in between the two. cf) H1 line and Help text are used for help prompt command. Function body contains all the MATLAB code that performs computations and assigns values to output arguments. All lines preceded by the symbol “%” that are not the H1 line or Help text are considered function comment lines and are not considered part of the Help text block In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
51
M-Files (3) In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
52
1.10.2 Operators (Arithmetic)
In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
53
1.10.2 Operators (Relational, Logical)
Name < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to Operator Description & Elementwise AND | Elementwise OR ~ Elementwise and scalar NOT && Scalar AND || Scalar OR In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
54
1.10.2 Logical Functions Operator Comments xor(exclusive OR)
The xor function returns a 1 only if both operands are logically different; otherwise xor returns a 0; all The all function returns a 1 if all the elements in a vector are nonzero; otherwise all returns a 0. This function operates columnwise on matrices any The any function returns a 1 if any of the elements in a vector is nonzero; otherwise any returns a 0. This function operates columnwise on matrices In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
55
Logical Functions Consider the arrays A = [1 2 3; 4 5 6] and B = [0 -1 1; 0 0 2]. >> xor(A, B) ans = >> all(A) >> all(B) ans = >> any(B) In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
56
1.10.2 Floating Point Number Representation
Floating point is consist of three parts. Sign Exponent Mantissa(Significant) Number is represented like this. (-1)S * (1. + M) * 2E (S : sign, M : Mantissa, E : Exponent) In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
57
1.10.2 Some important functions and constants
Value Returned ans Most recent answer (variable) if no output variable is assigned to an expression, MATLAB automatically stores the result in ans. eps Floating point relative accuracy. This is the distance between 1.0 and the next largest number representable using double-precision floating point. i (or j) Imaginary unit, as in 1 + 2i. NaN or nan Stands for Not-a-Number. pi realmax The largest floating-point number that your computer can represent. realmin The smallest positive floating-point number that your computer can represent. computer Your computer type. version Version number for MATLAB ver Version information for all installed MATLAB products. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
58
Formats (1) The format function is used to control how numerical data is displayed in the Command Window. format format type format(‘type’) In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
59
Formats (2) Type Result short Scaled fixed point format, with 4 digits after the decimal point. long Scaled fixed point format, with 8 digits after the decimal point. short e Floating point format, with 4 digits after the decimal point. long e Floating point format, with 8 digits after the decimal point. short g Best of fixed or floating point with 4 digits after the decimal point. long g Best of fixed or floating point, with 14 to 15 digits after the decimal point. short eng Engineering format that has 4 digits after the decimal point. long eng Engineering format that has 8 digits after the decimal point. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
60
Formats (3) Example of format change
61
1.10.3 Flow Control Statement Description
if if, together with else and elseif, executes a group of statements based on a specified logical condition. for Executes a group of statements a fixed number of times. while Executes a group of statements an indefinite number of times, based on a specified logical condition. break Terminates execution of a for or while loop. continue Passes control to the next iteration of a for or while loop, skipping any remaining statements in the body of the loop. switch switch, together with case and otherwise, executes different groups of statements depending on a specified value or string. return Causes execution to return to the invoking function. try…catch Changes flow control if an error is detected during execution. In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
62
Function Handles A function handle is a MATLAB data type that contains information used in referencing a function. One of the principal advantages of using function handles is that you can pass a function handle as an argument in a call to another function. >> f >> f(pi/4) ans = 0.7071 >> sin(pi/4) ans = 0.7071 In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
63
Code Optimization In this section we discuss two important approaches for MATLAB code optimization preallocating arrays vectorizing loops In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
64
Preallocating Arrays Preallocation refers to initializing arrays before entering a for loop that computes the elements of the array. Right code (using preallocation) is far faster than left one. function y = sinfun1(M) x = 0 : M – 1; for k = 1 : numel(x) y(k) = sin(x(k) / (100*pi)); end function y = sinfun2(M) x = 0 : M – 1; y = zeros(1, numel(x)); for k = 1 : numel(x) y(k) = sin(x(k) / (100*pi)); end In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
65
Vectorizing Loops Vectorization in MATLAB refers to techniques for eliminating loops altogether, using a combination of matrix/vector operators, indexing techniques, and existing MATLAB or toolbox functions. function y = sinfun3(M) x = 0 : M – 1; y = sin(x ./ (100 * pi)); In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
66
Interactive I/O In this section, we establish a foundation for writing interactive M-functions that display information and instructions to users and accept inputs from a keyboard. display(argument) t = input(‘message’) >> A = [1 2; 3 4]; >> display(A) A = >> t = input('Enter your data:'); Enter your data: 25 >> t t = 25 In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
67
1.10.7 An Introduction to Cell Arrays and Structures (1)
Cell arrays provide a way to combine a mixed set of objects under one variable name. >> C = {A, B, t}; >> C C = [2x2 double] [2x3 double] [25] >> C{1} ans = In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
68
1.10.7 An Introduction to Cell Arrays and Structures (2)
Structures are similar to cell arrays in that they allow grouping of a collection of dissimilar data into a single variable. However, unlike cell arrays, in which cells are addressed by numbers, the elements of structures are addressed by user-defined names called fields function s = image_stats(f) s.dm = size(f); s.AI = mean2(f); s.AIrows = mean(f, 2); s.AIcols = mean(f, 1); end >> image_stats(A) ans = dm: [2 2] AI: AIrows: [2x1 double] AIcols: [2 3] In this chapter we are going to focus on the monochrome image processing which can simply say black and white image processing.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.