%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % COMP 435 Image Processing Examples % Z. Li, created 02/01/2009 % 1. image read and display % 2. color to gray conversion % 3. filtering % 4. smoothing and edge detection %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function []=image_processing_demo() clear; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % read and display image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% im = imread('lena.bmp'); figure(1); imshow(im); hold on; [x1, y1]=ginput(1); plot(x1, y1, '+r'); [x2, y2]=ginput(1); plot(x2, y2, '+r'); xOffs=x1; yOffs=y1; w=x2-x1+1; h=y2-y1+1; %%%%%%%%%%%%%%%%%%%%%%% % access image data %%%%%%%%%%%%%%%%%%%%%%% im2 = double(im(xOffs:xOffs+w-1, yOffs:yOffs+h-1)); figure(2); colormap('gray'); subplot(2,2,1); stem(im2(1,:), '.'); title('row 1'); subplot(2,2,2); stem(im2(:,1), '.'); title('col 1'); subplot(2,2,3); imagesc(im2); subplot(2,2,4); meshz(im2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % point operations- histogram equ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% h1 = imhist(im); im3 = histeq(im); h3=imhist(im3); figure(3); subplot(2,2,1); imhist(im); subplot(2,2,2); imshow(im); subplot(2,2,3); imhist(im3); subplot(2,2,4); imshow(im3); input('\n color conversion...'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % color conversion %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% im1 = imread('lake-superior.jpg'); im2 = imread('lake-zurich-1.jpg'); subplot(2,2,1); imshow(im1); subplot(2,2,2); imshow(rgb2gray(im1)); subplot(2,2,3); imshow(im2); subplot(2,2,2); imshow(rgb2gray(im2)); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % filter operations with fspecial %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure; k1 = fspecial('average', 5); k2 = fspecial('disk', 5); k3 = fspecial('log'); k4 = fspecial('gaussian', 5, 0.8); subplot(2,2,1); colormap('gray'); stem3(k1, '.'); subplot(2,2,2); colormap('gray'); stem3(k2, '.'); subplot(2,2,3); colormap('gray'); stem3(k3, '.'); subplot(2,2,4); colormap('gray'); stem3(k4, '.'); figure; subplot(2,2,1); imshow(imfilter(im, k1)); subplot(2,2,2); imshow(imfilter(im, k2)); subplot(2,2,3); imshow(imfilter(im, k3)); subplot(2,2,4); imshow(imfilter(im, k4)); %%%%%%%%%%%%%%%%%%%%%%% % filter operations - smoothing %%%%%%%%%%%%%%%%%%%%%%% % build a Gaussian smooth filter: gf1 = computeGaussianKernel(2); gf2 = computeGaussianKernel(8); gf3 = computeGaussianKernel(16); im_gf1 = imfilter(im, gf1, 'symmetric', 'conv'); im_gf2 = imfilter(im, gf2, 'symmetric', 'conv'); im_gf3 = imfilter(im, gf3, 'symmetric', 'conv'); figure(4); subplot(2,2,1), imshow(im); title('original lena'); subplot(2,2,2), imshow(im_gf1); title('blured, gaussian, std 4, '); subplot(2,2,3), imshow(im_gf2); title('blured, gaussian, std 8, '); subplot(2,2,4), imshow(im_gf3); title('blured, gaussian, std 16, '); % laplacian filter - edge detection: kLap1 = fspecial('laplacian', 0.2); kLap2 = fspecial('laplacian', 0.5); kLap3 = fspecial('laplacian', 0.8); im_lap1 = imfilter(im, kLap1, 'symmetric', 'conv'); im_lap2 = imfilter(im, kLap2, 'symmetric', 'conv'); im_lap3 = imfilter(im, kLap3, 'symmetric', 'conv'); figure(5); subplot(2,2,1), imshow(im); title('original lena'); subplot(2,2,2), imshow(im_lap1); title('alpha=0.2'); subplot(2,2,3), imshow(im_lap1); title('alpha=0.5'); subplot(2,2,4), imshow(im_lap1); title('alpha=0.8'); % edge detector h1 = [-1 -1 -1; -1 8 -1; -1 -1 -1]; h2 = [-1 -1 -1; 2 2 2; -1 -1 -1]; h3 = [-1 2 -1; -1 2 -1; -1 2 -1]; h4 = [-1 -1 2; -1 2 -1; 2 -1 -1]; h5= h4'; figure(6); subplot(2,3,1); imshow(im); title('original lena'); subplot(2,3,2); im1 = imfilter(im, h1); imshow(im1); title('h1'); subplot(2,3,3); im2 = imfilter(im, h2); imshow(im2); title('h2'); subplot(2,3,4); im3 = imfilter(im, h3); imshow(im3); title('h3'); subplot(2,3,5); im4 = imfilter(im, h4); imshow(im4); title('h4'); subplot(2,3,6); im5 = imfilter(im, h5); imshow(im5); title('h5'); %%%%%%%%%%%%%%%%%%%%%%% % filter operations - smoothing % average, median vs gaussian %%%%%%%%%%%%%%%%%%%%%%% % median filter 4x4 im2 = imnoise(im,'salt & pepper',0.04); im3 = medfilt2(im2, [4, 4]); h6 = ones(4,4)./(4*4); % box filter im4 = imfilter(im2, h6); h7 = fspecial('gaussian'); im5 = imfilter(im2, h7); figure; subplot(2,2,1); imshow(im2); title('noisy lena'); subplot(2,2,2); imshow(im3); title('median filter'); subplot(2,2,3); imshow(im4); title('average'); subplot(2,2,4); imshow(im5); title('gaussian'); %%%%%%%%%%%%%%%%%%%%%%% % filter operations - edge detection % DoG %%%%%%%%%%%%%%%%%%%%%%% % more edge detectors: LoG, DoG % DoG figure(8); sgm = 2; t = [0.2 0.4 0.8 0.95]; for k=1:length(t) edge_map=edge(im, 'canny', t(k), sgm); subplot(2,2,k); imshow(edge_map); str=sprintf('DoG: t=%1.1f', t(k)); title(str); end %%%%%%%%%%%%%%%%%%%%%%% % filter operations - edge detection % sobel, laplacian of gaussian, prewitt %%%%%%%%%%%%%%%%%%%%%%% figure; im1 = imread('lena.bmp'); im2 = imread('cameraman.tif'); k1 = fspecial('log'); k2 = fspecial('sobel'); k3 = fspecial('prewitt'); subplot(2,3,1); imshow(imfilter(im1, k1)); title('LoG'); subplot(2,3,2); imshow(imfilter(im1, k2)); title('Sobel'); subplot(2,3,3); imshow(imfilter(im1, k3)); title('Prewitt'); subplot(2,3,4); imshow(imfilter(im2, k1)); title('LoG'); subplot(2,3,5); imshow(imfilter(im2, k2)); title('Sobel'); subplot(2,3,6); imshow(imfilter(im2, k3)); title('Prewitt'); return;