Digital Image Processing with MATLAB Outline Reading images

Digital Image Processing with MATLAB

Outline �Reading images �Displaying images �Writing images �MATLAB data classes (data types) �Image types �Converting between data classes and image types
![Reading images �imread(‘filename’) I = imread(‘rice. png’); �size(I); �[m, n]=size(I); �whos I; Name, size, Reading images �imread(‘filename’) I = imread(‘rice. png’); �size(I); �[m, n]=size(I); �whos I; Name, size,](http://slidetodoc.com/presentation_image_h2/293ad3874cafe327bae225d86e9d60e7/image-3.jpg)
Reading images �imread(‘filename’) I = imread(‘rice. png’); �size(I); �[m, n]=size(I); �whos I; Name, size, bytes, … �NOTE: Removing the semicolon

Displaying images �imshow(I); �imshow(I, G) G = number of gray levels used to display the image If G is omitted, default is 255 �Pixval: obsolete, replaced by impixelinfo Pixel values on the figure
![Displaying images �imshow(I, [low high]) low = minimum value of I high = maximum Displaying images �imshow(I, [low high]) low = minimum value of I high = maximum](http://slidetodoc.com/presentation_image_h2/293ad3874cafe327bae225d86e9d60e7/image-5.jpg)
Displaying images �imshow(I, [low high]) low = minimum value of I high = maximum value of I Values < low displayed in black Values > high displayed in white low < Values < high displayed in default gray levels

Displaying images �Imshow(i 1); �Figure, imshow(i 2); Show another image while keeping previous ones

Writing images �imwrite(I, ‘filename. ext’); �Or �imwrite(I, ‘filename’, ‘ext’); �Or �imwrite(I, ‘filename. ext’, ‘quality’, q); q = integer between 0 and 100 representing the JPEG compression If no path is specified, this saves to current working directory

Writing images �iminfo filename Obtain image file details �i = iminfo(‘filename’) Note the 2 different ways Command-function duality

Writing images �More general imwrite() for only. tif images: �imwrite(i, ‘filename. tif’, ‘compression’, ‘parameter’, …, ‘resolution’, [colres rowres]); �parameter: none: no compression packbits: default compression for non-binary images ccitt: default compresssion for binary images �[colres rowres] 1 x 2 array indicating column and row resolution in dots -per-unit (e. g. dots-per-inch (dpi)) Default is [72 72]

MATLAB data classes �uint 8 �uint 16 �uint 32 �single double �char (2 byte) �logical (0 0 r 1)

Image types �Intensity images �Binary images �Indexed images �RGB images

Intensity images �Intensity image is a data matrix whose values are scaled to represent intensities Intensity image of class uint 8 have integer values in [0 255] Intensity image of class uint 16 have integer values in [0 65535] …

Binary images �Binary image is a logical array of 0 s and 1 s Note: In MATLAB, an array of 0 s and 1 s of class uint 8 is not considered a binary image �B = logical(A) Convert to logical array (binary image) �islogical(B) Test if B is logical Returns: 0 = not logical, 1 = logical

Converting between data classes �A = data_class_name(B) Converts between data classes For example: A = double(B)

Converting between data classes and image types �The following functions perform the necessary scaling to convert between data classes and image types: Name im 2 uint 8 im 2 uint 16 mat 2 gray Converts to uint 8 uint 16 double in range [0 , 1] im 2 double im 2 bw logical Valid input class Logical, uint 8, uint 16, and double Logical, uint 8, uint 16, and double

Converting between data classes and image types �B = im 2 uint 8(A); B: Values are integers in range [0 255] Values less than 0 become 0 Values larger than 1 become 255

Converting between data classes and image types �im 2 uint 8 example: f= -0. 5000 0. 7500 1. 5000 S: Multiply each value by 255 >> g = im 2 uint 8(f) g= 0 128 191 255
![Converting between data classes and image types �B = mat 2 gray(A, [Amin Amax]); Converting between data classes and image types �B = mat 2 gray(A, [Amin Amax]);](http://slidetodoc.com/presentation_image_h2/293ad3874cafe327bae225d86e9d60e7/image-18.jpg)
Converting between data classes and image types �B = mat 2 gray(A, [Amin Amax]); B: has values in range [0 1] A: An array of double scaled to [0 1] Amin: Values less than Amin become 0 Amax: Values larger than Amax become 1 B = mat 2 gray(A); ▪ Amin and Amax are set to the actual minimum and maximum values in A

Converting between data classes and image types �mat 2 gray example: >> f=[1 2; 3 4] f=1 2 3 4 new. Value=(old. Value-Min)/(Max-Min) >> g=mat 2 gray(f) g=0 0. 3333 0. 6667 1. 0000

Converting between data classes and image types �B = im 2 double(A); If A is integer or logical: returns a double image with values in [0 1] If A is double: return an exact copy of the input Divides each value by the maximum value of the data class (uint 8: 255, uint 16: 65535, …)

Converting between data classes and image types �im 2 double example: >> h = uint 8([25 50; 128 200]) h= 25 50 128 200 new. Value=old. Value/255 >> g = im 2 double(h) g= 0. 0980 0. 1961 0. 5020 0. 7843

Converting between data classes and image types �g = im 2 bw(f, T); f: intensity image g: binary image T: threshold ▪ T has to be in [0 1] regardless of the data class of f All values in f <= T become 0 All values in f > T become 1 ▪ If f is integer: values have to be normalized (using mat 2 gray) ▪ If f is double: the function is applied directly ▪ If f is logical: output = input

Converting between data classes and image types � im 2 bw example: >> f=[1 2; 3 4] f= 1 2 3 4 >> g=mat 2 gray(f) g= 0 0. 3333 0. 6667 1. 0000 >> gb=im 2 bw(g, . 6) gb = 0 0 1 1

Converting between data classes and image types �Alternate way to im 2 bw: Relational operators: >> binaryimage = f > 2 binaryimage = 0 0 1 1

Converting between data classes and image types Converting uint to double (CASTING): >> gb = uint 8([0 0; 1 1]); >> gbd 1 = im 2 double(gb) gbd 1 = 0 0 0. 0039 >> gbd 2 = double(gb) gbd 2 = 0 0 1 1

MATLAB language nested statements and compactness �Nested statements: >> gbd = im 2 double(im 2 bw(mat 2 gray(f), 0. 6)); �Compactness: >> gbd = double(f > 2)

To be continued …
- Slides: 27