Search code examples
matlabimage-processingfilepathedge-detectioncanny-operator

How to automatically save the output image file in a specified folder?


I am using Canny edge detection on images. Since my original 10 images are in the path folder, C:\Users\X\Desktop\FoodRGB as 01.jpg, 02.jpg, 03.jpg and so on, I want to save all my output images in the folder C:\Users\X\Desktop\FoodCanny as 01.jpg, 02.jpg, 03.jpg.

I figured that I have to use imwrite() function to write the output images in a specific folder but I am not sure about the big idea.

The following code I am using is saving the images as 0%d.jpg in FoodCanny Folder I manually created.

for k = 1:10        
img = sprintf('C:\Users\X\Desktop\4\Food Canny\0%d.jpg',k); 
imwrite(canny_image, 'C:\Users\X\Desktop\4\Food Canny\0%d.jpg');
end

Solution

  • Supposing you want to get canny edges of all screenshots in the C:\Users\Asus\Pictures\Screenshots and save them to the other folder D:\. You can do this by :

    clc;clear all;
    fpath = fullfile('C:\Users\Asus\Pictures\Screenshots','*.png');
    img_dir = dir(fpath);
    for k=1:length(img_dir)
        input_image=strcat('C:\Users\Asus\Pictures\Screenshots\',img_dir(k).name);
        original = rgb2gray(imread(input_image));
        original= edge(original,'canny');
        imwrite(original,strcat('D:\',sprintf('edge(%d).jpg',k)));
    end