Automatilly read image files and apply filtre in Matlab
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Subhadeep Koley
el 3 de Nov. de 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 comentarios
Subhadeep Koley
el 4 de Nov. de 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
Ver también
Categorías
Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!