how is edge detection done in matlab??

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)

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

Thanks for your help but the problem with the above code is that cropping is done manually by putting the co-ordinates. I want the cropping part to be done independently irrespective of where the image is placed in the white background. Also the resultant image should be free of any white part from the background, for instance it should be like the image which i've attached.
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.
You are correct to say that a good image capture will reduce the efforts of clutter and straightening but i need to consider all these possibilities also to eliminate any kind of error(as i am taking input image from the scanner). For that i have to extract only the currency image(with no visible background) as well straighten it to align with the horizontal axis. Only then i can use it for matching.
OK and can you do that? My attached function may help you to extract the largest blob.
I am unable to run your code. I am getting multiple errors in that.
What are the errors? It runs fine for me. Do you have the Image Processing Toolbox?
Yes i do have image processing toolbox
these are the errors. I could not make out the meaning. so i'm directly sending what i got in my command window.
??? Error using ==> iptcheckinput Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 281 iptcheckinput(a, {'double','uint8','int8','logical','uint16','int16','single','uint32', 'int32'}, ...
Error in ==> imhist at 59 [a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in ==> ExtractBiggestBlob at 41 [pixelCount, grayLevels] = imhist(grayImage);

Sign in to comment.

Categories

Asked:

on 10 Mar 2015

Commented:

on 13 Mar 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!