Automatilly read image files and apply filtre in Matlab

조회 수: 1 (최근 30일)
Rooter Boy
Rooter Boy 2020년 11월 3일
댓글: Subhadeep Koley 2020년 11월 4일
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.

답변 (1개)

Subhadeep Koley
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
Rooter Boy
Rooter Boy 2020년 11월 3일
편집: Rooter Boy 2020년 11월 3일
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 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 CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by