Improving and Optimizing Blurring Function

1 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2020-7-21
编辑:Matt J 2020-7-22
Using sepblockfun from the File Exchange,
n=inputArg2;
outImg = repelem( sepblockfun(inputArg1,[n,n,1],'mean') , n,n,1);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by