Hello,
I have to blur an image with use of a kernel. Currently I have several for loops, however, this is slow. I was wondering how I could go about vectorising it while only using functions from base matlab (no toolboxes unfortunatly). I'm very new to matlab so any help would be greatly appreciated. My current code is below:
inputImage = imread("testImage2.jpg");inputImage = double(inputImage);w = 5; % specifies the size of the box (w = 3 means a 3x3 box)
r = fix(w/2); % 'radius' of box, excluding centre pixel
[x,y,z] = size(inputImage); % 3D sizes of image
blurredImage = zeros(size(inputImage)); % initialize output array
for i=(r+1):x-(r+1) % scan through every pixel in inputImage
for j=(r+1):y-(r+1) % '(rad+1)' leaves a border so data isn't read outside array bounds
for (k=1:3) % scan through rgb values of each pixel
kernel = inputImage([i-r:i+r],[j-r:j+r], k); % array index the pixel data within the box around the current pixel
blurredImage(i,j,k) = mean(kernel(:)); % apply mean of pixel data within box to blurredImage pixel
end endendfigure(1)image(uint8(inputImage))figure(2)image(uint8(blurredImage))
Best Answer