Improving and Optimizing Blurring Function

3 vues (au cours des 30 derniers jours)
William Rowe
William Rowe le 21 Juil 2020
Modifié(e) : Matt J le 22 Juil 2020
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

Réponse acceptée

Matt J
Matt J le 21 Juil 2020
Modifié(e) : Matt J le 22 Juil 2020
Using sepblockfun from the File Exchange,
n=inputArg2;
outImg = repelem( sepblockfun(inputArg1,[n,n,1],'mean') , n,n,1);

Plus de réponses (0)

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by