Automatilly read image files and apply filtre in Matlab
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Subhadeep Koley
2020-11-3
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 个评论
Subhadeep Koley
2020-11-4
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!