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 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