Search code examples
matlabedge-detectionsobel

How to colour the edges after using sobel filter?


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.


Solution

  • 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