I am trying to write a program for a grayscale image blurring without using gaussian or conv2 function.
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to multiply the Kernel matrix of Gaussian blur with the gray scale image without using the conv function.
Kernel Matrix = (0.0625*[1 2 1;2 4 2;1 2 1])
Unable to attach the file here as it is in png format.
Can someone correct this?
Img=imread('512.pgm');
fig = figure(1);
fig.ToolBar="none";
fig.MenuBar="none";
% Two plots in one window
subplot(1,2,1)
title('Original');
image(Img);
axis('image');
axis('off');
colormap(gray);
Img=double(Img);
%Gaussian Kernel
sigma=1.76
sz=4; % window size
[x,y]=meshgrid(-sz:sz,-sz:sz);
M=size(x,1)-1;
N=size(y,1)-1;
Kernel=(0.0625*[1 2 1;2 4 2;1 2 1])
mag2=zeros(size(Img))
for i = 1:size(Img,1)-M
for j = 1:size(Img,2)-N
temp=Img(i:i+M,j:j+N).*Kernel
mag2(i+1,j+1) = sum(temp)+temp;
end
end
mag2=uint8(mag2);
montage(Img,mag2)
title('Original vs Blur')
0 Commenti
Risposte (2)
DGM
il 12 Feb 2022
Modificato: DGM
il 12 Feb 2022
This should create a gaussian kernel. Padding the array is a simple and inexpensive way to deal with indexing at the image boundary.
% inputs
inpict = imread('cameraman.tif');
sigma = [1.76 1.76]; % sigma may be asymmetric
filtersize = [6 6]; % or just automatically calculate from sigma
% create gaussian filter kernel
R = floor((ceil((filtersize-1)/2)*2+1)/2);
[xx yy] = meshgrid(-R(2):R(2),-R(1):R(1));
fk = exp(-(xx.^2/(2*sigma(2)^2) + yy.^2/(2*sigma(1)^2)));
fk = fk/sum(fk(:));
filtersize = size(fk); % filter size is forced to be odd
% pad the image so that the filter can run over the edge
padsize = floor(filtersize/2);
paddedimage = padarray(inpict,padsize,'replicate','both');
s0 = size(inpict);
outpict = zeros(s0,'double');
os = filtersize-1;
paddedimage = double(paddedimage);
for m = 1:s0(1)
for n = 1:s0(2)
sample = paddedimage(m:(m+os(1)),n:(n+os(2)));
outpict(m,n) = sum(sample.*fk,'all');
end
end
outpict = uint8(outpict);
fig = figure(1);
%fig.ToolBar="none";
%fig.MenuBar="none";
% Two plots in one window
subplot(1,2,1)
imshow(inpict);
title('Original');
subplot(1,2,2)
imshow(outpict);
title('Blurred')
0 Commenti
Image Analyst
il 12 Feb 2022
Why do you have to do it without using conv2()? That sounds really bizarre (unless it's homework but you didn't tag it as homework so we're assuming it is not your homework). So you can use imfilter() instead. Or are you also banning that for some reason too?
As an absolute last choice you can use the manual way I attach. I don't think it have any of the optimizations in it like conv2() or imfilter() would have. Normally no one, except student required to by their professors, would do it this way.
If it really is your homework and you just forgot to tag it, you cannot turn in my work as your own or risk getting caught cheating.
2 Commenti
DGM
il 14 Feb 2022
FWIW, I know there are examples all over the forum and FEX for how to create a basic sliding window filter of various types. I consider the unique part of this question to be "how do I create a gaussian filter kernel". Perhaps that was only implicitly asked, and perhaps it's a minor part of the task, but hardly anyone ever asks that. They just use a boring flat square kernel.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
