Image Processing - Matlab Homework II
Submitted By:
- Adarsh Chandran (Student Id No: 3617720)
- Ravishankar Sivalingam (Student Id No: 3617709)
Contents
Question Number 1
Morphology.
* Inbuilt Matlab functions have been made use of in implementing the below code.
clear all close all clc % Initializing the Parameters imageSize = 2000; % number of 3x3 squares num3s = 100; % number of 5x5 squares num5s = 20; % Generating the image I = uint8(zeros(imageSize)); % randomly generate the square centers coords_3x3=randint(num3s,2,[3 imageSize+2]); coords_5x5=randint(num5s,2,[3 imageSize+2]); % zero-pad the image padI=padarray(I,[2 2]); % fill in the squares for i=1:length(coords_3x3) padI(coords_3x3(i,1)-1:coords_3x3(i,1)+1,coords_3x3(i,2)-1:coords_3x3(i,2)+1)=255; end for i=1:length(coords_5x5) padI(coords_5x5(i,1)-2:coords_5x5(i,1)+2,coords_5x5(i,2)-2:coords_5x5(i,2)+2)=255; end % remove the zero-padding I=padI(3:imageSize+2,3:imageSize+2); % Defining the structure elements % 1 % 1 se21 = strel('rectangle',[2 1]); % 1 1 se12 = strel('rectangle',[1 2]); % 0 0 1 % 0 0 0 % 1 0 0 serd101 = strel('arbitrary',[0 0 1; 0 0 0; 1 0 0]); % 1 0 0 % 0 0 0 % 0 0 1 seld101 = strel('arbitrary',[1 0 0; 0 0 0; 0 0 1]); % 0 1 % 1 0 serd11 = strel('arbitrary',[0 1; 1 0]); % 1 0 % 0 1 seld11 = strel('arbitrary',[1 0; 0 1]); % 0 1 % 0 0 % 1 0 serdstr = strel('arbitrary',[0 1; 0 0; 1 0]); % 1 0 % 0 0 % 0 1 seldstr = strel('arbitrary',[1 0; 0 0; 0 1]); % 0 0 1 % 1 0 0 serdstr1 = strel('arbitrary',[0 0 1; 1 0 0]); % 1 0 0 % 0 0 1 seldstr1 = strel('arbitrary',[1 0 0; 0 0 1]); % 3x3 square structuring element se3=strel('square',3); % 5x5 square structuring element se5=strel('square',5); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % a. all the squares that are touching the image border %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 5x5 squares touching the border borders_5x5=0*I; % create a border mask borders_5x5([1:3 end-2:end],:)=1; borders_5x5(:,[1:3 end-2:end])=1; borderI5=imdilate((imerode(I,se5)).*borders_5x5,se5); % 3x3 squares touching the border borders_3x3=uint8(zeros(imageSize)); % create a border mask borders_3x3([1 2 end-1 end],:)=1; borders_3x3(:,[1 2 end-1 end])=1; borderI3=imdilate((imerode(I,se3).*borders_3x3),se3); % All the squares touching the image border borderSquares = borderI5 + borderI3; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % b. all the 3x3 squares that do not overlap with any other square %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Find all the 5x5 squares in the image squares_5x5 = imdilate(imerode(I,se5),se5); % Find all the 3x3 squares in the image by subtracting from the original % image IbTemp = I; % Determine the 3x3 squares which are not overlapping with any of the 3x3 % squares IbTemp21 = imdilate(imdilate(imerode(imerode(IbTemp,se3),se21),se21),se3); IbTemp12 = imdilate(imdilate(imerode(imerode(IbTemp,se3),se12),se12),se3); IbTemprd101 = imdilate(imdilate(imerode(imerode(IbTemp,se3),serd101),serd101),se3); IbTempld101 = imdilate(imdilate(imerode(imerode(IbTemp,se3),seld101),seld101),se3); IbTemprd11 = imdilate(imdilate(imerode(imerode(IbTemp,se3),serd11),serd11),se3); IbTempld11 = imdilate(imdilate(imerode(imerode(IbTemp,se3),seld11),seld11),se3); IbTemprdstr = imdilate(imdilate(imerode(imerode(IbTemp,se3),serdstr),serdstr),se3); IbTempldstr = imdilate(imdilate(imerode(imerode(IbTemp,se3),seldstr),seldstr),se3); IbTemprdstr1 = imdilate(imdilate(imerode(imerode(IbTemp,se3),serdstr1),serdstr1),se3); IbTempldstr1 = imdilate(imdilate(imerode(imerode(IbTemp,se3),seldstr1),seldstr1),se3); ov3x3Squares = IbTemp21+IbTemp12+IbTemprd101+IbTempld101+IbTemprd11+IbTempld11+IbTemprdstr+IbTempldstr+IbTemprdstr1+IbTempldstr1; for cnt = 1:(imageSize-2) if (ov3x3Squares(cnt:(cnt+2),(imageSize-2):imageSize) == [0 255 255; 0 255 255; 0 255 255]) ov3x3Squares(cnt:(cnt+2),(imageSize-2):imageSize) = zeros(3); end if (ov3x3Squares((imageSize-2):imageSize, cnt:(cnt+2)) == [0 255 255; 0 255 255; 0 255 255]') ov3x3Squares((imageSize-2):imageSize, cnt:(cnt+2)) = zeros(3); end end notNonOv3x3Squares = ov3x3Squares + squares_5x5; nonOv3x3Squares = I - notNonOv3x3Squares; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % c. all the 5x5 squares, overlapping and non-overlapping %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% squares_5x5 = imdilate(imerode(I,se5),se5); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % d. all the overlapping squares %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ov3x3Squares; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Display the Results %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% figure;imagesc(I);colormap(gray) title('Fig 1. Original Image') figure;imagesc(borderSquares);colormap(gray) title('Fig 1.A. All the squares that are touching the image border') figure;imagesc(nonOv3x3Squares);colormap(gray) title('Fig1.B. All the 3x3 squares that do not overlap with any other square') figure;imagesc(squares_5x5);colormap(gray) title('Fig1.C. All the 5x5 squares, overlapping and non-overlapping') figure;imagesc(ov3x3Squares);colormap(gray) title('Fig1.D. All the overlapping squares')
Question Number 2
Active Contours.
* Inbuilt Matlab functions have been made use of in implementing the below code.
close all clear all clc % read the input image inpImage =imread('coin_1.jpg'); % size of image [rows cols dims] = size(inpImage); if dims==3 inpImage=double(rgb2gray(inpImage)); else inpImage=double(inpImage); end % Gaussian filter parameter sigma=1.2; % Gaussian filter G=fspecial('gaussian',15,sigma); % Gaussian smoothed image inpImage=conv2(inpImage,G,'same'); % gradient of image [gradIX,gradIY]=gradient(inpImage); absGradI=sqrt(gradIX.^2+gradIY.^2); % higher dimensional embedding function phi whose zero level set is our % contour % radius of circle - initial embedding function % radius=min(floor(0.45*rows),floor(0.45*cols)); [u,v] = meshgrid(1:cols, 1:rows); phi = ((u-cols/2)/(floor(0.45*cols))).^2+((v-rows/2)/(floor(0.45*rows))).^2-1; % edge-stopping function g = 1./(1+absGradI.^2); % gradient of edge-stopping function [gx,gy]=gradient(g); % gradient descent step size dt=.4; % number of iterations after which we reinitialize the surface num_reinit=10; phiOld=zeros(rows,cols); % number of iterations iter=0; while(sum(sum(abs(phi-phiOld)))~=0) % gradient of phi [gradPhiX gradPhiY]=gradient(phi); % magnitude of gradient of phi absGradPhi=sqrt(gradPhiX.^2+gradPhiY.^2); % normalized gradient of phi - eliminating singularities normGradPhiX=gradPhiX./(absGradPhi+(absGradPhi==0)); normGradPhiY=gradPhiY./(absGradPhi+(absGradPhi==0)); [divXnormGradPhiX divYnormGradPhiX]=gradient(normGradPhiX); [divXnormGradPhiY divYnormGradPhiY]=gradient(normGradPhiY); % curvature is the divergence of normalized gradient of phi K = divXnormGradPhiX + divYnormGradPhiY; % dPhiBydT dPhiBydT =( g.*K.*absGradPhi + g.*absGradPhi + (gx.*gradPhiX+gy.*gradPhiY) ); phiOld=phi; % level set evolution equation phi = phi + ( dt * dPhiBydT ); iter=iter+1; if mod(iter,num_reinit)==0 % reinitialize the embedding function after num_reinit iterations phi=sign(phi); phi = double((phi > 0).*(bwdist(phi < 0)) - (phi < 0).*(bwdist(phi > 0))); end if mod(iter,10)==0 pause(0.05) iter imagesc(inpImage) colormap(gray) hold on contour(phi,[0 0],'r') % close all % surf(phi) % pause end end