% Alex Atkins' Presentation on Image Processing % % This guide will use all pre-stocked images from matlab, or self-generated % images % Basic IO % you can read any image in the class paths or the current directory of % matlab, and you can also output in multiple fomats %imread takes a string arguement as a filename for the file ot read inand %returns its pixel values pout = imread('pout.tif'); %imwrite takes 3 arguements, (image, filename with extension, extension) imwrite(pout, 'pout2.jpg', 'jpg'); %this method can also be used toeasily changed the file type, %for instance the new picture is saved as a jpeg %% Image Transformations- % Resizing an image: % First we increase theimage size by a specified factor, and in the second % the pixel dimensions are explicitly defined. The third option in the % interpolation method, MATLAB supports three different types, nearest, % bilinear, and bicubic circuit = imread('circuit.tif'); circuit2 = imresize(circuit, 1.5, 'bilinear'); imshow(circuit) figure(2), imshow(circuit2) circuit3 = imresize(circuit, [50 75], 'bicubic'); figure(3), imshow(circuit3) %% rotating an image: % follows the same interpolation options as resize, but rotates the image X % degrees counter-clockwise circuit2 = imrotate(circuit2, 72, 'bilinear'); figure, imshow(circuit2) %% Morphologicaln Operations % Morphologicl operations use sructure elements to modify an image. MATLAB % uses these for dililation and erosian % Structure Elements % These are structures that are used for filtering, made of ones and % zeroes. You can choose different shapes and sizes. This example uses % a diamond shape fil = strel('diamond', 3) %% Dililation/erosion % best used on binary photos, but ithas some really neat effects. % imclose() will dialtae then erode and imopen() erode then dilate (as % opposed to entering each command spereately) bw = imread('circbw.tif'); se = strel('rectangle', [40 30]); bw2 = imerode(bw,se); bw3 = imdilate(bw2,se); bw4 = imdilate(bw,se); ske = bwmorph(bw, 'skel', Inf); perim = bwperim(bw); imshow(bw) figure, imshow(bw2) figure, imshow(bw3) figure, imshow(bw4) figure, imshow(ske) figure, imshow(perim) % by doing a set(bwmorph) you can get each of the possible filter % perameters and under help you can see exactly what each one does, and %% although many are similar they produce very different results bw = imread('circbw.tif'); imshow(bw) bw1 = bwmorph(bw, 'thin', Inf); figure, imshow(bw1) bw2 = bwmorph(bw, 'majority', Inf); figure, imshow(bw2) %% Filtering with imfilter BW4 = im2bw(imread('coins.png')); BW5 = imfill(BW4,'holes'); figure, imshow(BW4), figure, imshow(BW5) %there are also special filters such as "unsharp" that can be called %% Specialfilters % these filters are built forfiltering out certain errors, such as motion, % contrast and edge emphasis I = imread('moon.tif'); h = fspecial('unsharp'); I2 = imfilter(I,h); imshow(I), title('Original Image') figure, imshow(I2), title('Filtered Image') %% Distance Transformation- % This is a transfmoration basedon the distanceformacentral pixel % Draws two intercecting circles center1 = -10; center2 = -center1; dist = sqrt(2*(2*center1)^2); radius = dist/2 * 1.4; lims = [floor(center1-1.2*radius) ceil(center2+1.2*radius)]; [x,y] = meshgrid(lims(1):lims(2)); bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius; bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius; bw = bw1 | bw2; figure, imshow(bw), title('bw') E = bwdist(bw); figure, imshow(E, []); D = bwdist(~bw); figure, imshow(D, []), title('Distance transform of ~bw') % now running the distance calculator will isolate the connecting % path and the centers of the circle coins = im2bw(imread('coins.png')); coins = imfill(coins,'holes'); % imshow(coins) coin = bwdist(coins); coin2 = bwdist(~coins); figure, imshow(coin, []) figure, imshow(coin2,[]) %% Regions and Features % MATLAB allows you to isolate certain areas. bwlabel finds the areas of % max value in a function and iolates them using a mask BW = [0 0 0 0 0 0 0 0; 0 1 1 0 0 1 1 1; 0 1 1 0 0 0 1 1; 0 1 1 0 0 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 1 1 0 0 0; 0 0 0 0 0 0 0 0]; X = bwlabel(BW,4) RGB = label2rgb(X, @jet, 'k'); imshow(RGB,'notruesize') %% Selecting objects/regions BW1 = imread('text.png'); c = [43 185 212]; r = [38 68 181]; BW2 = bwselect(BW1,c,r,4); imshow(BW1), figure, imshow(BW2) %% Area of image foreground %finds the totalnumberof 'on' pixels in a binary image. This example shows %the increase of area after a dilation BW = imread('circbw.tif'); SE = ones(5); BW2 = imdilate(BW,SE); increase = (bwarea(BW2) - bwarea(BW))/bwarea(BW) %% Adjusting image intensity %changing the values- the function imadjust can be used to remap certain %image aspects. The first vector imput chooses the color range to be %adjusted (between 0 and 1) and then what colors to map them to. I = imread('cameraman.tif'); J = imadjust(I,[0 0.2],[0.5 1]); imshow(I) figure, imshow(J) %% Gamma Correction % MATLAB can also perform gamma correction by adding an additional option % to imadjust. If this value is <1 it weights the gamma to the high side % brightening the image, if it =1 it scales linearly, and if its >1 it % weights it toward the lower end (darkening) [X,map] = imread('forest.tif'); I = ind2gray(X,map); J = imadjust(I,[],[],0.5); imshow(I) figure, imshow(J) j1 = imadjust(I,[],[],1.5); figure, imshow(j1) %% Decolorlation stretching % isolates different color groups and displays them in groups, effectively % isolating similar A = multibandread('littlecoriver.lan', [512, 512, 7], ... 'uint8=>uint8', 128, 'bil', 'ieee-le', ... {'Band','Direct',[3 2 1]}); B = decorrstretch(A); imshow(A); figure; imshow(B) %linear stretching to give more in depth results C = decorrstretch(A,'Tol',0.01); figure, imshow(C) %% Noise removal %there are multiple forms of noise removal. Using different filter types, %linear, adaptive and median filtering. Linear while fastest is inferior to %adaptive. % Median filtering I = imread('eight.tif'); imshow(I) J = imnoise(I,'salt & pepper',0.02); figure, imshow(J) K = filter2(fspecial('average',3),J)/255; figure, imshow(K) L = medfilt2(J,[3 3]); figure, imshow(K) figure, imshow(L) %% adaptive %works best with white noise, adapts changes based onlocal variance, %perserving many finer features of thep icture. RGB = imread('saturn.png'); I = rgb2gray(RGB); J = imnoise(I,'gaussian',0,0.005); K = wiener2(J,[5 5]); imshow(J) figure, imshow(K) %% Transformations %fourier f = zeros(30,30); f(5:24,13:17) = 1; imshow(f,'notruesize') F = fft2(f); F2 = log(abs(F)); imshow(F2,[-1 5],'notruesize'); colormap(jet); colorbar F = fft2(f,256,256); figure, imshow(log(abs(F)),[-1 5]); colormap(jet); colorbar %% Discrete cosine tranformation % Divides te picture into small sectors and represents each as these as a % sum of sinusodial functions. Used for compressing JPEG's. I = imread('cameraman.tif'); I = im2double(I); T = dctmtx(8) B = blkproc(I,[8 8],'P1*x*P2',T,T'); mask = [1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; B2 = blkproc(B,[8 8],'P1.*x',mask); I2 = blkproc(B2,[8 8],'P1*x*P2',T',T); imshow(I), figure, imshow(I2) %% more RGB = imread('autumn.tif'); I = rgb2gray(RGB); J = dct2(I); imshow(log(abs(J)),[]), colormap(jet), colorbar J(abs(J)<10) = 0; K = idct2(J); figure, imshow(I) figure, imshow(K,[0 255])