How can I make edges black and background white in an photo with detected edges?
6 views (last 30 days)
Show older comments
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.
4 Comments
Eleanor Betton
on 26 Feb 2020
By thresholding you can see the sea is mainly has a value less than 80, if you set all picels with less than 80 to be while and anything higher as island you get a rough island shape - it is pretty rough so you probably want to keep trying edge detection.
Eleanor Betton
on 26 Feb 2020
This gives a similar examples which might help.
Accepted Answer
Image Analyst
on 26 Feb 2020
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273818/image.png)
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!