Filters Main Function in Image Processing
Show older comments

I want to applied this three filter to a gray level image.
After that i want to save this image with using this file names (imsobel, imlaplas) in same current file.
I tried this but i can't do this.
I need a main fucntion.
%img =imread('cameraman.tif');
%Sobel filters
%Sx=[-1 0 1; -2 0 2; -1 0 1]
%Sy=[-1 -2 -1; 0 0 0; 1 2 1]
%T1=imfilter(img, Sx)
%T2=imfilter(img, Sy)
%imshow(T1)
%imshow(T2)
%Laplacian filters
&H= [0, -1, 0; -1, 4, -1; 0, -1, 0];
&T=imfilter(img, H)
%imshow(T)
% "imagefiles" variable contails each file detail (e.g. filename, size, etc.)
imagefiles = dir(['C:\Users\metec\Desktop\aa\', '*.tif']);
% Count total number of files
nfiles = length(imagefiles);
% Display total number of the files
disp(['Total number of file: ', num2str(nfiles)])
% Define filter kernel
%Laplacian filters
H = [0, -1, 0; -1, 4, -1; 0, -1, 0];
T=imfilter(img, H)
%imshow(T)
for idx = 1:nfiles
T=imfilter(img, H)
% 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, T, 'same');
% Save the filtered image
imwrite(uint8(result),...
['C:\Users\metec\Desktop\aa\', 'imgLaplas', imagefiles(idx).name, '.tif'])
end
Answers (1)
At first I thought this was an issue of misusing uint8(), but no. This is just a collage of nonsense and dead code. Most of it can simply be deleted
% get filenames and paths
querypath = fullfile(matlabroot,'toolbox/images/imdata/car_*.jpg');
imagefiles = dir(querypath);
% define filter kernel
H = [0, -1, 0; -1, 4, -1; 0, -1, 0]; % laplacian
for idx = 1:numel(imagefiles)
% construct full filename and read the image
infname = fullfile(imagefiles(idx).folder,imagefiles(idx).name);
inpict = imread(infname);
% apply the filter
outpict = imfilter(inpict,H,'symmetric');
% save the dumb thing
[~,basename] = fileparts(imagefiles(idx).name);
outfname = sprintf('laplace_%s.png',basename);
imwrite(outpict,outfname)
end
Categories
Find more on Image Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!