MATLAB: How to make edges black and background white in an photo with detected edges

edge detectionedge imagesimage processingImage Processing Toolboximage segmentationMATLAB

Hey, everyone! I'm studying on edge detection at MATLAB and have a sample code for that.The edges are white and the background is black.However, my teacher wants to see that edges are black and the background is white.How can I do that? .Also, edge should be only between land and sea.The edges inside land should not be shown (my code doesn't show them anyways).'erodedimg = 255 – real(erodedimg)' part does the image all white even tho I made the white edges bigger with imdilate function.
Image is attached.
The code:
clc;
I = imread('island.bmp');
BW1 = edge(I,'Prewitt',0.18);
se = strel('square',3);
erodedimg = imdilate(BW1,se);
erodedimg = 255 – real(erodedimg);
imshow(erodedimg);
Please help me about that. Thanks by now for your attention.

Best Answer

  • Try this:
    clc; % Clear the command window.
    close all; % Close all figures (except those of imtool.)
    clear; % Erase all existing variables. Or clearvars if you want.
    workspace; % Make sure the workspace panel is showing.
    format long g;
    format compact;
    fontSize = 15;
    %===============================================================================
    % Read in demo image.
    folder = pwd;
    baseFileName = 'island.bmp';
    % Get the full filename, with path prepended.
    fullFileName = fullfile(folder, baseFileName);
    % Check if file exists.
    if ~exist(fullFileName, 'file')
    % The file doesn't exist -- didn't find it there in that folder.
    % Check the entire search path (other folders) for the file by stripping off the folder.
    fullFileNameOnSearchPath = baseFileName; % No path this time.
    if ~exist(fullFileNameOnSearchPath, 'file')
    % Still didn't find it. Alert user.
    errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
    uiwait(warndlg(errorMessage));
    return;
    end
    end
    % Read in the image from disk. If storedColorMap is not empty, it's an indexed image with a stored colormap.
    [grayImage, storedColorMap] = imread(fullFileName);
    if ~isempty(storedColorMap)
    grayImage = ind2rgb(grayImage, storedColorMap);
    end
    % Get the dimensions of the image.
    % numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
    [rows, columns, numberOfColorChannels] = size(grayImage);
    if numberOfColorChannels > 1
    % It's not really gray scale like we expected - it's color.
    % Use weighted sum of ALL channels to create a gray scale image.
    grayImage = rgb2gray(grayImage);
    % ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
    % which in a typical snapshot will be the least noisy channel.
    % grayImage = grayImage(:, :, 2); % Take green channel.
    end
    % Display the image.


    hFig = figure;
    subplot(2, 3, 1);
    imshow(grayImage, []);
    title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
    hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
    drawnow;
    % Display the histogram
    subplot(2, 3, 2);
    imhist(grayImage);
    grid on;
    % Binarize the image
    binaryImage = imbinarize(grayImage);
    % Display the image.
    subplot(2, 3, 3);
    imshow(binaryImage, []);
    title('Initial Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
    axis('on', 'image');
    % Fill holes.
    binaryImage = imfill(binaryImage, 'holes');
    % Display the image.
    subplot(2, 3, 4);
    imshow(binaryImage, []);
    title('Filled Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
    axis('on', 'image');
    hp = impixelinfo();
    % Get the boundaries
    boundaries = bwboundaries(binaryImage);
    % Plot them over the inverted gray scale image.

    subplot(2, 3, 5);
    imshow(255-grayImage, []);
    numBoundaries = length(boundaries);
    caption = sprintf('Inverted Grayscale Image with %d boundaries', numBoundaries);
    title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
    hold on;
    for k = 1 : length(boundaries)
    thisBoundary = boundaries{k};
    x = thisBoundary(:, 2);
    y = thisBoundary(:, 1);
    plot(x, y, 'r-', 'LineWidth', 2);
    end
    % Now make an image that is all white so we can draw the edges (boundaries) in black.
    % Plot them over the inverted gray scale image.
    subplot(2, 3, 6);
    whiteImage = grayImage; % Initialize to same size as original image.
    whiteImage(:) = 255; % Make white.
    imshow(whiteImage, []);
    axis('on', 'image');
    caption = sprintf('White Image with %d boundaries', numBoundaries);
    title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
    hold on;
    % Draw all boundaries in black.
    for k = 1 : length(boundaries)
    thisBoundary = boundaries{k};
    x = thisBoundary(:, 2);
    y = thisBoundary(:, 1);
    plot(x, y, 'k-', 'LineWidth', 2);
    end