% ========================================================================= % Math 336 - Image Processing - Laboratory 6 % % Image Morphology (Part 1): % Implement the grayscale morphological operations. % % Marty Kandes % Computational Science Research Center / Department of Physics % San Diego State University % Fall 2008 % ------------------------------------------------------------------------- clear all; close all; clc; % Clear Matlab workspace. F = imread('lena.jpg'); % Read in the image. F = im2double(F); % Convert image to double. F = mat2gray(F); % Normalize grayscale image. SE = ones(3,2); % Build the structure element. AP = [2,2]; % Set the active point of the SE. G = mdilate(F,SE,AP,'gray'); % Dilate the image F. G = mat2gray(G); % Rescale dilated image G to grayscale. figure; % Open new figure. imshow(G); % Display dilated image. imwrite(G,'lenadilated.jpg','jpg'); % Write dilated image to file. H = merode(F,SE,AP,'gray'); % Erode the image. H = mat2gray(H); % Rescale eroded image to grayscale. figure; % Open new figure. imshow(H); % Display eroded image. imwrite(H,'lenaeroded.jpg','jpg'); % Write eroded image to file. SE = ones(3,3); % Build square structure element. I = mopen(F,SE,AP,'gray'); % Open image with a square SE. I = mat2gray(I); % Rescale opened image to grayscale. figure; % Open new figure. imshow(I); % Display opened image. imwrite(I,'lenaopenedwithsquare.jpg','jpg'); % Write opened image to file. SE = ones(3,3); % Build disk structure element. SE(1,1) = 0; % SE(1,3) = 0; % SE(3,1) = 0; % SE(3,3) = 0; % J = mopen(F,SE,AP,'gray'); % Open image with a disk SE. J = mat2gray(J); % Rescale opened image to grayscale. figure; % Open new figure. imshow(J); % Display opened image. imwrite(J,'lenaopenedwithdisk.jpg','jpg'); % Write opened image to file. SE = zeros(3,3); % Build L-shaped structure element. SE(:,1) = 1; % SE(3,:) = 1; % K = mopen(F,SE,AP,'gray'); % Open image with a L-shaped SE. K = mat2gray(K); % Rescale opened image to grayscale. figure; % Open new figure. imshow(K); % Display opened image. imwrite(K,'lenaopenedwithlshape.jpg','jpg'); % Write opened image to file. SE = ones(3,3); % Build square structure element. L = mclose(F,SE,AP,'gray'); % Close image with a square SE. L = mat2gray(L); % Rescale closed image to grayscale. figure; % Open new figure. imshow(L); % Display closed image. imwrite(L,'lenaclosedwithsquare.jpg','jpg'); % Write closed image to file. SE = ones(3,3); % Build disk structure element. SE(1,1) = 0; % SE(1,3) = 0; % SE(3,1) = 0; % SE(3,3) = 0; % M = mclose(F,SE,AP,'gray'); % Close image with a disk SE. M = mat2gray(M); % Rescale closed image to grayscale. figure; % Open new figure. imshow(M); % Display closed image. imwrite(M,'lenaclosedwithdisk.jpg','jpg'); % Write closed image to file. SE = zeros(3,3); % Build L-shaped structure element. SE(:,1) = 1; % SE(3,:) = 1; % N = mclose(F,SE,AP,'gray'); % Close image with a L-shaped SE. N = mat2gray(N); % Rescale closed image to grayscale. figure; % Open new figure. imshow(N); % Display closed image. imwrite(N,'lenaclosedwithlshape.jpg','jpg'); % Write closed image to file. O = imread('colored_peppers.jpg'); % Read in the colored image. O = im2double(O); % Convert image to double. SE = ones(3,3); % Build a square structure element. P = mbound(O,SE,AP); % Apply boundary extraction. figure; % Open new figure. imshow(P); % Display boundary extracted image. imwrite(P,'peppersboundary.jpg','jpg'); % Write BEI to file. % =========================================================================