Images and MATLAB
Matlab Advantages 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. Even 1D variables. In terms of matrix dimensions, and in terms of new functions Memory issues should be regarded when dealing with large data sets. Highly intuitive – most functions work for all dimension objects in the same manner.
Image Types Intensity images scaled to represent intensities (uint8 – [0,255], double [0,1]) Binary images logical array of 0s and 1s Indexed images Look up table [x, map] RGB images truecolor, array of (m*n*3) In case of logical arrays if the image is converted to logical any non zero element is converted to 1. When working in intensity image it is important to convert to double when performing numerical operations like / 2 first types – grayscale 2 last types – color images Map (3*number of colors), efficient representation Checking the image type : isind, isbw, isgray, isrgb Converting image types: rgb2ind, rgb2gray, gray2ind, ind2gray,….
Data Classes Numeric computations (8 first types) are done using double precision Uint8 – when reading data from storage device (8 bit images) Logical – output of logical operations (for example – mask threshing) Conversion – same as casting Converting between types : B = data_class_name(A) for example: B = double(A)
Conversions When converting between data classes and types it is important to keep the value range for each data class >> img = double(img)/255; >> img = im2double(img); Mat2gray – convert to full grayscale range [0 to 1] Im2double - for other data classes Im2bw – produce a binary image according to a threshold
MATLAB supported image formats 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.) Spatial domain
Matlab Basics Digital image representation : 2D function f(x,y) -> finite discrete quantities Coordinate Conventions img(r,c) r – rows (height) c – cols (width) The first pixel: img(1,1) Image is a 2D function in each location there is value representing the gray level of the image. Color image are formed by combination of 2D images. For example in the RGB color space each pixel has Red Green and Blue values. Image is a discrete both in space (sampling) and in gray level values (quantization) Matlab starts from (1,1) in comparison to other computer languages
Matlab 內建影像 C:\MATLAB7\toolbox\images\imdemos 皆為Matlab Help中範例的原始影像。 使用時只需直接在指令中輸入檔名,即可使用。 適用於觀察影像處理結果
Different Image Types Indexed images Intensity (grayscale) images Binary images RGB (true-color) images
Reading an image imread() 功用:將影像載入並存成array格式備用 用法:[I,map] = imread(filename); I = imread(filename); ex: I = imread('pout.tif'); I為指向影像的變數 不指定變數,則為ans
Displaying an image imshow() 功用:開啟一個視窗顯示影像 用法: imshow(I) imshow(I,map) Figure, imshow() 功用:開啟一個新視窗顯示影像 用法: figure,imshow(I)
Displaying an image(cont.) imshow(I, [low, high]) 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. pixval : 功能:cursor on image to show pixel values 用法: imshow(I),pixval
Displaying an image(cont.) colorbar 功能:To display an image with a colorbar that indicates the range of intensity values. 用法: imshow(I), colorbar ex: I = imread('pout.tif'); imshow(I) , colorbar
Writing an image imwrite() 功能:將影像寫入成檔案 用法: imwrite(I,filename,format) ex: imwrite(I,'pout.jpg','JPEG');
Image information Image size: size() ex: I= imread('saturn.png'); size(I) [M,N] = size(I) M=影像I的高 N=影像I的寬
Image information whos 功用:display information about an image . ex: whos I Imfinfo( filename ) 功用: display information about image file . ex: info = imfinfo('saturn.png')
Digital Image processing 影像二元化 g = im2bw(I, T); 功用: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 = im2bw(I, 0.4); imshow(g); colorbar
Digital Image processing(cont.) 彩色轉灰階 Rgb2gray() 功用:將RBG彩色影像轉換成gray-level影像。ex: I2= imread ('onion.png'); figure,imshow(I2); colorbar g2 = rgb2gray(I2); figure,imshow(g2); colorbar
Digital Image processing(cont.) 反相 imcomplement( ) 功用:The negative of an image. ex: I2= imread ('onion.png'); figure,imshow(I2); colorbar J2 = imcomplement(g2); figure, imshow(J2); colorbar
Grayscale images 灰階影像 Matlab example: imshow: display matrix w=imread('pout.tif'); figure, imshow(w), pixval on figure: create a window to place graphic object imshow: display matrix Data type of w? 291x240 uint8 (unsigned integer 8 bits
Binary image 二元影像 Matlab example: Data type of w? w2=imread('circles.png'); figure, imshow(w2), pixval on Data type of w? 256x256 logical Pixel value is 0 or 1
RGB (true color) images 全彩影像 Matlab example: w3=imread('peppers.png'); figure, imshow(w3), pixval on size(w3) w3(100,200,2) w3(100,200,1:3) w3(100,200,:)
RGB color model
Pixel depth Pixel depth: the number of bits used to represent each pixel in RGB space Full-color image: 24-bit RGB color image (R, G, B) = (8 bits, 8 bits, 8 bits)
Indexed color image 彩色索引影像 Matlab example: wI=imread('trees.tif'); figure, imshow(w), pixval on What’s wrong?
Indices Color Map
Indexed color image Matlab example: [wI,wmap]=imread('trees.tif'); figure, imshow(wI, wmap) How do we know it’s an indexed image?
Indexed color image 彩色索引影像 Matlab example: 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 indices Color map 0.1211 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 Color map … indices 6 10 15 12 5 11 20 10 4 6 10 7
Indexed color image Matlab example: [w,wmap]=imread(‘emu.tif’); imshow(w, wmap) How do we know it’s an indexed image?
Get information about image imfinfo('emu.tif'); Filename: 'emu.tif' FileModDate: '12-Jul-2004 11:40:00' FileSize: 119804 Format: 'tif' FormatVersion: [] Width: 331 Height: 384 BitDepth: 8 ColorType: 'indexed' ByteOrder: 'little-endian' NewSubfileType: 0 BitsPerSample: 8 Colormap: [256x3 double]
Get information about image imfinfo(‘emu.tif’); Filename: 'emu.tif' FileModDate: '12-Jul-2004 11:40:00' FileSize: 119804 Format: 'tif' FormatVersion: [] Width: 331 Height: 384 BitDepth: 8 ColorType: 'indexed' ByteOrder: 'little-endian' NewSubfileType: 0 BitsPerSample: 8 Colormap: [256x3 double]
Data types and conversion uint8 image must be converted to double before any arithmetic operation 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 Matlab code w=imread('pout.tif'); imwrite(w, 'pout.jpg','jpg'); General form imwrite(X, map, ‘filename’, ‘format’);
Zooming and Shrinking Digital Images Create a new pixel location. Assign a gray-level to those new locations Nearest neighbor interpolation Pixel replication: a checkboard effect Bilinear interpolation using four nearest neighbors v(x’, y’)=ax’+by’+cx’y’+d where a, b, c, and d are determined from the gray-level of the four neighbors. Shrinking: Direct shrinking causes aliasing Expansion then Shrinking: blurring the image before shrinking it and reduce aliasing.
Sampling Methods of Inverse Mapping If transformed pixel ‘X’ locates among P1, P2, P3 and P4 Nearest neighbor method: I(‘X’) = I(P3 ) where I(p) is the intensity value of pixel p Bi-linear interpolation: I(‘X’) = (1-a)(1-b)I( P1 ) + a(1-b)I(P2 ) + (1-a)bI(P3 ) + abI(P4 ) where a, b are the fractional parts of ‘X’ Bi-cubic interpolation: based on cubic splines P1 P2 P3 P4
Nearest neighbor interpolation The closest neighbor is chosen , by rounding the “new” indexes to original image’s coordinates .
Digital Image processing(cont.) 變更影像大小 imresize(I,scale,method); 功用:To change the size of an image. interpolation Method: -'nearest‘ :Nearest-neighbor interpolation -'bilinear‘ :Bilinear (the default) -'bicubic‘ :Bicubic interpolation
Digital Image processing(cont.) 變更影像大小 imresize(I,scale,method); 功用:To change the size of an image. interpolation Method: -'nearest‘ :Nearest-neighbor interpolation -'bilinear‘ :Bilinear (the default) -'bicubic‘ :Bicubic interpolation
Digital Image processing(cont.) ex: I3 = imread('circuit.tif'); J3 = imresize(I3,1.25); figure, imshow(I3) figure, imshow(J3) J4= imresize(I3,[100 150], 'bilinear'); figure, imshow(J4)
Digital Image processing(cont.) 旋轉影像 imrotate(I, angle); 功用:To rotate an image. ex: I = imread('pout.tif'); J5 = imrotate(I,35); figure, imshow(J5)
Contents Histogram 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 1 2 4 5 histogram image
Histogram equalization (HE) Histogram equalization (HE) transforms the intensity values so that the histogram of the output image approximately matches the flat (uniform) histogram
Histogram equalization II. Histogram equalization II. As for the discrete case the following formula applies: 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 ·(L-1) ?
Histogram equalization III
histogram p=imread('pout.tif'); imshow(p), figure, imhist(p), axis tight
Histogram equalization ph=histeq(p); imshow(ph), figure, imhist(ph), axis tight
Histogram equalization (cont.) [ph, t]=histeq(p); plot(t), title('transform function'); Exercise#1. Apply histogram equalization to tire image