Automatilly read image files and apply filtre in Matlab

1 view (last 30 days)
I have a 10 different gray image files in current folder.
For example:
img1.jpg
img2.jpg
img3.jpg
img4.jpg
....
image10.jpg
This script automatilly read image files and apply smoothing filtre step by step using with for loop. This code script is feedback me .
For example: there are 10 image files in current folder
this files names: img1.jpg, img2.jpg, img3.jpg..........
So, it gives me names of files and total number of files.
This scpirt code apply smoothing filter for image1 from to image50 step by step
smoothing filter:
H= ones(3)/9;
result = filter2(H,image);
after that it save this files in same current folder with different names
For example:
outimg1.jpg
outimg2.jpg
outimg3.jpg
outimg4.jpg
.....
outimg10.jpg
I don't know how to do. If you help me, i will be very happy.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 3 Nov 2020
Hi, the below code snippet might help.
% "imagefiles" variable contails each file detail (e.g. filename, size, etc.)
imagefiles = dir(['pasteYourFolderPathHere\', '*.jpg']);
% Count total number of files
nfiles = length(imagefiles);
% Define filter kernel
filterKernel = ones(3)/9;
for idx = 1:nfiles
currentfilename = [imagefiles(idx).folder , '\', imagefiles(idx).name];
currentimage = imread(currentfilename);
currentimage = im2double(currentimage);
% Apply filter to current image
result = filter2(filterKernel, currentimage);
% Save the filtered image
imwrite(uint8(rescale(result, 0, 255)),...
['pasteYourFolderPathHere\', 'out', imagefiles(idx).name, '.jpg'])
end
  3 Comments
Rooter Boy
Rooter Boy on 3 Nov 2020
Edited: Rooter Boy on 3 Nov 2020
Sir, it works just .tif files.
I can show number of image files using with fprintf('%d\n', nfiles); but i don't know how to see names of files.
Subhadeep Koley
Subhadeep Koley on 4 Nov 2020
Hi, you're facing the error because your image files are 3 channel RGB image and filter2() function can only work on 2D grayscale images. You can use the convn() function to achive the same. convn() works on both RGB and grayscale images. I've also added the code to display total number and name of image files. Refer the code below.
% "imagefiles" variable contails each file detail (e.g. filename, size, etc.)
imagefiles = dir(['pasteYourFolderPathHere\', '*.jpg']);
% Count total number of files
nfiles = length(imagefiles);
% Display total number of the files
disp(['Total number of file: ', num2str(nfiles)])
% Define filter kernel
filterKernel = ones(3)/9;
for idx = 1:nfiles
% Display name of each file
disp(imagefiles(idx).name)
currentfilename = [imagefiles(idx).folder , '\', imagefiles(idx).name];
currentimage = imread(currentfilename);
% Apply filter to current image using convn
result = convn(currentimage, filterKernel, 'same');
% Save the filtered image
imwrite(uint8(result),...
['pasteYourFolderPathHere\', 'out', imagefiles(idx).name, '.jpg'])
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!