Search code examples
matlabimage-processingedge-detection

Need to apply edge filtering algorithm on series of images


i wanna write a code in matlab which will accept images from an implicitly given directory and apply Sobel edge detection algorithm on each image on-the-go. Please Help


Solution

  • Sobel edge detection in MATLAB only works on grayscale images so you would need to convert any images into a grayscale representation. If you have a folder on desktop where all the images reside, then the following snippet of code can do what you wanted.

    dirname = '~/Desktop/imgs/';
    files = dir([dirname, '*.png']);
    files = {files.name}';
    for ifile = 1:numel(files)
        imfile = [dirname, files{ifile}];
        im = imread(imfile);               % Read the image file
        img = rgb2gray(im);                % Convert to grayscale
        ime = edge(img, 'sobel');          % Edge detection
    
        figure(1)
        imshow(ime);
        outname = ['edge_', files{ifile}];
        imwrite(ime, outname);
    end
    

    Hope that helps.