0

I am using sobel filter for edge detection. How to illustrate the gradient direction with color coding. For example, horizontal edges with blue and vertical edges with yellow?

Thank you.

indu
  • 177
  • 2
  • 11

1 Answers1

1

Since you can specify whether you want horizontal or vertical edge detected (check here), you could perform 2 filtering operations (one horizontal and the other vertical) and save each resulting image, then concatenating them to form a final, 3-channels RGB image.

The RGB color code for yellow is [1 1 0] and that of blue is [0 0 1], so in your case the vertical edge image will occupy the first 2 channels whereas the horizontal edge image will occupy the last channel.

Example:

clear
clc
close all

A = imread('circuit.tif');

[r,c,~] = size(A);

EdgeH = edge(A,'Sobel','Horizontal');
EdgeV = edge(A,'Sobel','Vertical');

%// Arrange the binary images to form a RGB color image.
FinalIm = zeros(r,c,3,'uint8');

FinalIm(:,:,1) = 255*EdgeV;
FinalIm(:,:,2) = 255*EdgeV;
FinalIm(:,:,3) = 255*EdgeH;

figure;

subplot(1,2,1)
imshow(A)

subplot(1,2,2)
imshow(FinalIm)

Output:

enter image description here

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
  • Hi Thank you so much.That worked perfectly.But if say I have extracted diagonal edge also and plot with another color.What should I do? – indu May 08 '15 at 12:38
  • Great glad to help! To display another set of edges with a different color, you will need to play with the Red, Green and Blue channels of `FinalIm`. Please have a look at my answer at the end of [this](http://stackoverflow.com/questions/27550252/change-color-of-a-specific-region-in-eye-image-matlab/27554155#27554155) post, in which the user selects a color to fill a region with. The same methodology would apply for you; in your current case the `mask` would be every the vertical, horizontal and diagonal edge images. – Benoit_11 May 08 '15 at 12:45