How do i find edges in true colored image

I need to extract red plane of true color image. Then need to apply 8 directions sobel mask on this red plane image. then need to perform sobel edge detection using threshold value.
This is need to repeat for blue and green plane image as well
Can anyone please guide me how can i do that in matlab.
I am able to extract true red plane image by keeping blue and green as 0.But inbut filter2 function doesnt work on this.

1 Comment

Can anyone please reply me with some code syntax

Sign in to comment.

Answers (2)

This ought to give you a good start:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 4, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display them
subplot(2,4,2);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(2,4,3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(2,4,4);
imshow(blueChannel);
title('Blue Channel', 'FontSize', fontSize);
% Compute Sobel Filters and threshold them
threshRed = 1;
threshGreen = 3;
threshBlue = 4;
[sobelRed threshRed] = edge(redChannel,'sobel');
[sobelGreen threshGreen] = edge(greenChannel,'sobel');
[sobelBlue threshBlue] = edge(blueChannel,'sobel');
% Display them
subplot(2,4,6);
imshow(sobelRed, []);
title('Sobel Filter on Red Channel', 'FontSize', fontSize);
subplot(2,4,7);
imshow(sobelGreen, []);
title('Sobel Filter on Green Channel', 'FontSize', fontSize);
subplot(2,4,8);
imshow(sobelBlue, []);
title('Sobel Filter on Blue Channel', 'FontSize', fontSize);

4 Comments

Thanks alot for your prompt reply.Actually my main concern is with applying 8 direction masks to extracted red channel.I have done below so far .But the leads to error that image size is too large to dispaly.Can you please guide me where i am going wrong.
clear;
close all;
m=imread('abc.bmp');
figure;
subplot(3,4,1);
imshow(m),title('Input Image'); % Display the original picture
r=m(:,:,1); %extract the red color
m(:,:,2)=0;
m(:,:,3)=0;
subplot(3,4,2);
imshow(m),title('Red plane of image');
r = imnoise(r,'salt & pepper',0.02);
8 Direction masks
a1=[1 2 1;0 0 0;-1 -2 -1];
%%% a1 2
a2=[2 1 0; 1 0 -1;0 -1 -2];
%%% a1 3
a3=[1 0 -1;2 0 -2; 1 0 -1];
%%%% a1 4
a4=[0 -2 -2;1 0 -1;2 1 0];
%%% a1 5
a5=[-1 -2 -1;0 0 0;1 2 1];
%%% a1 6
a6=[-2 -1 0; -1 0 1;0 1 2];
%%% a1 7
a7=[-1 0 1;-2 0 2;-1 0 1];
%%%% a1 7
a8=[0 1 2;-1 0 1;-2 -1 0];
a1r=filter2(a1,r);
ta1=uint8(a1r);
a2r=filter2(a2,r);
ta2=uint8(a2r);
a3r=filter2(a3,r);
ta3=uint8(a3r);
a4r=filter2(a4,r);
ta4=uint8(a4r);
a5r=filter2(a5,r);
ta5=uint8(a5r);
a6r=filter2(a6,r);
ta6=uint8(a6r);
a7r=filter2(a7,r);
ta7=uint8(a7r);
a8r=filter2(a8,r);
ta8=uint8(a8r);
t3=(ta1+ta2+ta3+ta4+ta5+ta6+ta7+ta8);
figure,imshow(t3),title('Red Compoenent Image after sOBEL');
That's not an error, it's a warning. This snippet of code will help you to turn it off.
% Turn off this warning "Warning: Image is too big to fit on screen; displaying at 33% "
% To set the warning state, you must first know the message identifier for the one warning you want to enable.
% Right after your line of code generates the error,
% query the last warning to discover the warning identifier. For example:
% warnStruct = warning('query', 'last');
% messageID = warnStruct.identifier
% In the command window, this will show:
% messageID =
% MATLAB:concatenation:integerInteraction
% Pass that into the warning function:
warning('off', 'Images:initSize:adjustingMag');
I agree that doesnt stop me to proceed.But i only got a single staright line rather that contactenated image.Thats the issue.Can you please try to run this problem.Idea is to find sobel edges after applying 8 direction mask values to red channel image only.
uint8's clip when they go beyond 255. Cast to double before you add:
t3=(double(ta1)+double(ta2)+double(ta3)+double(ta4)+double(ta5)+double(ta6)+double(ta7)+double(ta8));
figure,imshow(t3, []); % Make sure you use [] to properly scale.

Sign in to comment.

Why have we done thresholding here? And if i want to apply a 3x3 sobel filter then what change is to be done in the code?

Asked:

on 24 May 2012

Answered:

on 10 Dec 2018

Community Treasure Hunt

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

Start Hunting!