how is edge detection done in matlab??
5 views (last 30 days)
Show older comments
I need to remove the white part of the above image and retain the original image as it is. Is this process called edge detection? If so then by using canny edge detection I lose my color information while removing the white part because the resultant output image comes in binary form. How can it be done without losing the colors in the image?
0 Comments
Answers (1)
Image Analyst
on 13 Mar 2015
No reason for edge detection at all. I continue to be surprised when image processing beginners thing that edge detection is the way to go just because there are edges in the scene. I mean, why use a more complicated method when simple thresholding will get you what you want.
binaryImage = grayImage < someThreshold;
Here's a full demo:
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 = 14;
% Read in a standard MATLAB gray scale demo image.
folder = 'D:\Temporary Stuff';
baseFileName = 'Image (2).jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
% Crop between lines 1400 and 2400
rgbImage = imcrop(rgbImage, [1, 1400, columns, 1000]);
subplot(2, 2, 1);
imshow(rgbImage, [0 255]);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Name', 'Demo by Image Analyst');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Take the min value of R, G, or B.
grayImage = min(rgbImage, [], 3);
subplot(2, 2, 2);
imshow(grayImage, []);
title('Gray scale Image', 'FontSize', fontSize);
% Threshold the image
binaryImage = grayImage < 250;
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Clean it up
binaryImage = imfill(binaryImage, 'holes'); % Fill holes.
binaryImage = bwareaopen(binaryImage, 20000); % Get rid of small blobs.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
9 Comments
Image Analyst
on 13 Mar 2015
See this about using orientation to align an image: http://www.mathworks.com/matlabcentral/answers/97460-why-does-the-regionprops-command-not-return-the-correct-orientation-for-a-square-region
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!