Improving and Optimizing Blurring Function

3 ビュー (過去 30 日間)
William Rowe
William Rowe 2020 年 7 月 21 日
編集済み: Matt J 2020 年 7 月 22 日
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 Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by