how is edge detection done in matlab??
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?
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
AFFY
on 13 Mar 2015
Image Analyst
on 13 Mar 2015
I'm not sure you understand. The cropping I did was to just get rid of some garbage you had at the top of your image. We can get rid of that in two ways. 1) you fix your image capture situation so that you don't have any clutter in the background. Or (2) you threshold to get everything and just extract the biggest blob, which should be the money and not the clutter. Anyway, in the end you'll have a binary mask like I showed. If you want me to not crop and extract the biggest blob, I can do that so let me know.
But it seems like your problem now is how to align the image to the axes and then crop it in a perfect rectangle. Correct me if I'm wrong. Is so, why do you need it straightened? Again, it can be fixed with a proper image capture setup so that the notes are aligned in the first place. As always, it's better to start with good images than to start with bad images and try to fix them afterwards.
AFFY
on 13 Mar 2015
Image Analyst
on 13 Mar 2015
OK and can you do that? My attached function may help you to extract the largest blob.
AFFY
on 13 Mar 2015
Image Analyst
on 13 Mar 2015
What are the errors? It runs fine for me. Do you have the Image Processing Toolbox?
AFFY
on 13 Mar 2015
AFFY
on 13 Mar 2015
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
Categories
Find more on Image Processing Toolbox 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!