Improving and Optimizing Blurring Function

3 views (last 30 days)
I wrote a very simple sort of blurring function that takes an image and integer (n) as inputs and outputs an image of the same size that divides up the original image into n x n sections and takes the mean of each section and assigns that color to the output image. (Hopefully that makes sense?) I know my code is by no means great and there's plenty of ways to break it, but I was wondering what might be a better way to accomplish this task rather than actually looping through each section and calculating means? Just hoping to learn some tips on how to optimize a function like this or if there's a better way entirely to accomplish this. Thanks!
Here's my code:
inputArg1 is the image and inputArg2 is the integer n
function [outImg] = justChannel(inputArg1,inputArg2)
outImg = zeros(size(inputArg1));
[x,y] = size(inputArg1);
deltaX = x/inputArg2;
deltaY = y/inputArg2;
redChannel = inputArg1(:,:,1);
greenChannel = inputArg1(:,:,2);
blueChannel = inputArg1(:,:,3);
across=0;
down=0;
while(across < inputArg2)
while(down < inputArg2)
currentX = floor(across * deltaX + 1);
currentY = floor(down * deltaY + 1);
nextX = ceil(currentX + deltaX - 1);
nextY = ceil(currentY + deltaY - 1);
redMean = mean(mean(redChannel(currentX:nextX,currentY:nextY)));
outImg(currentX:nextX,currentY:nextY,1) = redMean;
greenMean = mean(mean(greenChannel(currentX:nextX,currentY:nextY)));
outImg(currentX:nextX,currentY:nextY,2) = greenMean;
blueMean = mean(mean(blueChannel(currentX:nextX,currentY:nextY)));
outImg(currentX:nextX,currentY:nextY,3) = blueMean;
down=down+1;
end
across = across + 1;
down=0;
end
outImg = uint8(outImg);
imshow(outImg);
end

Accepted Answer

Matt J
Matt J on 21 Jul 2020
Edited: Matt J on 22 Jul 2020
Using sepblockfun from the File Exchange,
n=inputArg2;
outImg = repelem( sepblockfun(inputArg1,[n,n,1],'mean') , n,n,1);

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!