Search code examples
matlabimage-processingedge-detectioncanny-operator

contour line edge detection in MATLAB


I have a project to detect the contour lines in this image , but when I run my code with the canny edge detection algorithm , one line in the image transforms to two lines, because of two times the change in grey values of the line before and after that .

i= imread('path');
imgG= rgb2gray(i);

PSF = fspecial('gaussian',7,7);
Blurred = imfilter(imgG,PSF,'symmetric','conv');
figure ,imshow(Blurred)

edgeimg = edge(Blurred , 'canny');
figure ,imshow(edgeimg)

I have no idea to solve this, please help me.


Solution

  • The best answer depends on what you want to do with the edges after detecting them, but let's assume you just want to generate an image where the lines are pure black and everything else is pure white...

    The simplest approach is to threshold the image so that lighter grey pixels become white and darker grey pixels become black. You can also erode the image to try and reduce the thickness of lines -- although you'll find that this will get rid of the fine contours in your sample image.

    Here is the code to do this (assumes you have the image G4.jpg in your working folder).

    % load image and convert to double (0 to 1) as well as flip black and white
    imG4 = imread('G4.jpg');
    imG4_gs = 1 - mean(double(imG4)/255,3);
    
    figure
    image(64*(1 - imG4_gs))
    colormap('gray');
    axis equal
    
    % image grayscale threshold
    img_thr = 0.25;
    
    % apply threshold to image
    imG4_thr = imG4_gs >= img_thr;
    
    figure
    image(64*(1 - imG4_thr))
    colormap('gray');
    axis equal
    
    % erode image (try "help imerode" in the MATLAB console)
    imG4_ero = imerode(imG4_thr,strel('disk',1));
    
    figure
    image(64*(1 - imG4_ero))
    colormap('gray');
    axis equal;