Images and MATLAB Matlab Advantages n n n
Images and MATLAB
Matlab Advantages n n n Every variable in Matlab is a multidimensional matrix. Highly modular. No memory allocation is necessary. Matlab enables its own garbage collection. Simple interface for complex mathematical concepts.
Image Types n Intensity images n Binary images n Indexed images n RGB images scaled to represent intensities (uint 8 – [0, 255], double [0, 1]) logical array of 0 s and 1 s Look up table [x, map] truecolor, array of (m*n*3) Checking the image type : isind, isbw, isgray, isrgb Converting image types: rgb 2 ind, rgb 2 gray, gray 2 ind, ind 2 gray, ….
Data Classes Converting between types : B = data_class_name(A) for example: B = double(A)
Conversions n When converting between data classes and types it is important to keep the value range for each data class >> img = double(img)/255; >> img = im 2 double(img);
MATLAB supported image formats n n n JPEG: Joint Photographics Experts Group TIFF: Tagged Image File Format GIF: Graphics Interchange Format BMP: Microsoft Bitmap Format PNG: Portable Network Graphics …
Displaying an image(cont. ) n Spatial domain
Matlab Basics n Digital image representation : 2 D function f(x, y) -> finite discrete quantities n n Coordinate Conventions img(r, c) r – rows (height) c – cols (width) The first pixel: img(1, 1)
Different Image Types n n Indexed images Intensity (grayscale) images Binary images RGB (true-color) images
Reading an image imread() n 功用: 將影像載入並存成array格式備用 n 用法: [I, map] = imread(filename); I = imread(filename); ex: I = imread('pout. tif'); I為指向影像的變數 不指定變數,則為ans n
Displaying an image n n n imshow() 功用: 開啟一個視窗顯示影像 用法: imshow(I) imshow(I, map) Figure, imshow() 功用: 開啟一個新視窗顯示影像 用法: figure, imshow(I)
Displaying an image(cont. ) imshow(I, [low, high]) n imshow(I, [ ]) 功用: displays I as a grayscale intensity image, specifying the data range for I. The minimum value in I is displayed as black, and the maximum value is displayed as white. n pixval : 功能: cursor on image to show pixel values 用法: imshow(I), pixval n
Displaying an image(cont. ) colorbar n 功能: To display an image with a colorbar that indicates the range of intensity values. n 用法: imshow(I), colorbar ex: I = imread('pout. tif'); imshow(I) , colorbar n
Writing an image imwrite() n 功能: 將影像寫入成檔案 n 用法: imwrite(I, filename, format) ex: imwrite(I, 'pout. jpg', 'JPEG'); n
Image information Image size: size() ex: I= imread('saturn. png'); size(I) [M, N] = size(I) n M=影像I的高 N=影像I的寬
Image information whos 功用: display information about an image. n ex: whos I n Imfinfo( filename ) 功用: display information about image file. n ex: info = imfinfo('saturn. png') n
Digital Image processing n 影像二元化 g = im 2 bw(I, T); n 功用: Convert intensity image I to binary image g using threshold T, where T must be in range [0, 1]. ex: I= imread('pout. tif'); g = im 2 bw(I, 0. 4); imshow(g); colorbar n
Digital Image processing(cont. ) 彩色轉灰階 n Rgb 2 gray() n 功用: 將RBG彩色影像轉換成gray-level影像。 ex: I 2= imread ('onion. png'); figure, imshow(I 2); colorbar g 2 = rgb 2 gray(I 2); figure, imshow(g 2); colorbar n
Digital Image processing(cont. ) 反相 n imcomplement( ) n 功用: The negative of an image. ex: I 2= imread ('onion. png'); figure, imshow(I 2); colorbar J 2 = imcomplement(g 2); figure, imshow(J 2); colorbar n
Grayscale images 灰階影像 n Matlab example: n n n w=imread('pout. tif'); figure, imshow(w), pixval on figure: create a window to place graphic object imshow: display matrix Data type of w? n 291 x 240 uint 8 (unsigned integer 8 bits
Binary image 二元影像 n Matlab example: n n n w 2=imread('circles. png'); figure, imshow(w 2), pixval on Data type of w? n n 256 x 256 logical Pixel value is 0 or 1
RGB (true color) images 全彩影像 n Matlab example: n n n w 3=imread('peppers. png'); figure, imshow(w 3), pixval on size(w 3) w 3(100, 2) w 3(100, 200, 1: 3) w 3(100, 200, : )
RGB color model
Pixel depth n n Pixel depth: the number of bits used to represent each pixel in RGB space Full-color image: 24 -bit RGB color image n (R, G, B) = (8 bits, 8 bits)
Indexed color image 彩色索引影 像 n Matlab example: n n n w. I=imread('trees. tif'); figure, imshow(w), pixval on What’s wrong?
Indices Color Map
Indexed color image n Matlab example: n n n [w. I, wmap]=imread('trees. tif'); figure, imshow(w. I, wmap) How do we know it’s an indexed image?
Indexed color image 彩色索引影 像 n Matlab example: n n n w=imread(‘emu. tif’); figure, imshow(w), pixval on What’s wrong?
Indexed color image 6 10 15 12 5 11 20 10 4 6 10 7 0. 1211 0. 1416 0. 1807 0. 2549 0. 1729 0. 2197 0. 3447 0. 1807 0. 1611 0. 1768 0. 1924 0. 2432 0. 2471 0. 1924 0. 2119 0. 1963 0. 2002 … indices Color map
Indexed color image n Matlab example: n n n [w, wmap]=imread(‘emu. tif’); imshow(w, wmap) How do we know it’s an indexed image?
Get information about image n imfinfo('emu. tif'); Filename: 'emu. tif' File. Mod. Date: '12 -Jul-2004 11: 40: 00' File. Size: 119804 Format: 'tif' Format. Version: [] Width: 331 Height: 384 Bit. Depth: 8 Color. Type: 'indexed' Byte. Order: 'little-endian' New. Subfile. Type: 0 Bits. Per. Sample: 8 Colormap: [256 x 3 double]
Get information about image n imfinfo(‘emu. tif’); Filename: 'emu. tif' File. Mod. Date: '12 -Jul-2004 11: 40: 00' File. Size: 119804 Format: 'tif' Format. Version: [] Width: 331 Height: 384 Bit. Depth: 8 Color. Type: 'indexed' Byte. Order: 'little-endian' New. Subfile. Type: 0 Bits. Per. Sample: 8 Colormap: [256 x 3 double]
Data types and conversion n uint 8 image must be converted to double before any arithmetic operation n n w=imread('pout. tif'); w=w+1; % fail w=double(w); % data type is also conversion func. w=w+1; % ok
Write image matrix to file n Matlab code n n n w=imread('pout. tif'); imwrite(w, 'pout. jpg', 'jpg'); General form n imwrite(X, map, ‘filename’, ‘format’);
Zooming and Shrinking Digital Images n Zooming: n Create a new pixel location. n Assign a gray-level to those new locations n n Nearest neighbor interpolation n Pixel replication: a checkboard effect Bilinear interpolation using four nearest neighbors n n n v(x’, y’)=ax’+by’+cx’y’+d where a, b, c, and d are determined from the graylevel of the four neighbors. Shrinking: n Direct shrinking causes aliasing n Expansion then Shrinking: blurring the image before shrinking it and reduce aliasing.
Sampling Methods of Inverse Mapping If transformed pixel ‘X’ locates among P 1, P 2, P 3 and P 4 P 1 P 3 P 2 P 4 v Nearest neighbor method: I(‘X’) = I(P 3 ) where I(p) is the intensity value of pixel p v Bi-linear interpolation: I(‘X’) = (1 -a)(1 -b)I( P 1 ) + a(1 -b)I(P 2 ) + (1 -a)b. I(P 3 ) + ab. I(P 4 ) where a, b are the fractional parts of ‘X’ v Bi-cubic interpolation: based on cubic splines
Nearest neighbor interpolation n The closest neighbor is chosen , by rounding the “new” indexes to original image’s coordinates.
Digital Image processing(cont. ) 變更影像大小 n imresize(I, scale, method); n 功用: To change the size of an image. n interpolation Method: -'nearest‘ : Nearest-neighbor interpolation -'bilinear‘ : Bilinear (the default) -'bicubic‘ : Bicubic interpolation n
Digital Image processing(cont. ) 變更影像大小 n imresize(I, scale, method); n 功用: To change the size of an image. n interpolation Method: -'nearest‘ : Nearest-neighbor interpolation -'bilinear‘ : Bilinear (the default) -'bicubic‘ : Bicubic interpolation n
Digital Image processing(cont. ) ex: I 3 = imread('circuit. tif'); J 3 = imresize(I 3, 1. 25); figure, imshow(I 3) figure, imshow(J 3) n ex: I 3 = imread('circuit. tif'); J 4= imresize(I 3, [100 150], 'bilinear'); figure, imshow(I 3) figure, imshow(J 4) n
Digital Image processing(cont. ) 旋轉影像 n imrotate(I, angle); n 功用: To rotate an image. n ex: I = imread('pout. tif'); J 5 = imrotate(I, 35); figure, imshow(J 5) n
Contents n n n Histogram transformation Histogram equalization Contrast streching Applications
Histogram The (intensity or brightness) histogram shows how many times a particular grey level (intensity) appears in an image. For example, 0 - black, 255 – white 0 1 1 2 4 2 1 0 0 2 5 2 0 0 4 1 1 2 4 1 image histogram
Histogram equalization (HE) transforms the intensity values so that the histogram of the output image approximately matches the flat (uniform) histogram
Histogram equalization II. As for the discrete case the following formula applies: ·(L-1) k = 0, 1, 2, . . . , L-1 L: number of grey levels in image (e. g. , 255) nj: number of times j-th grey level appears in image n: total number of pixels in the image ?
Histogram equalization III
histogram n n p=imread('pout. tif'); imshow(p), figure, imhist(p), axis tight
Histogram equalization n n ph=histeq(p); imshow(ph), figure, imhist(ph), axis tight
Histogram equalization (cont. ) n n [ph, t]=histeq(p); plot(t), title('transform function'); Exercise#1. Apply histogram equalization to tire image
- Slides: 70