-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ApplyBoxBlur.m
67 lines (56 loc) · 2.33 KB
/
ApplyBoxBlur.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function [ mOutputImage ] = ApplyBoxBlur( mInputImage, boxRadius )
% ----------------------------------------------------------------------------------------------- %
% [ mBlurredImage ] = ApplyBoxBlur( mInputImage, boxBlurKernelRadius )
% Applies Box Blur using Integral Images.
% Input:
% - mInputImage - Input Image.
% Structure: Image Matrix (Single Channel)
% Type: 'Single' / 'Double'.
% Range: [0, 1].
% - boxRadius - Box Radius.
% The radius of the box neighborhood for the
% summation process.
% Structure: Scalar.
% Type: 'Single' / 'Double'.
% Range: {1, 2, ..., }.
% Output:
% - mOutputImage - Output Image.
% Structure: Image Matrix (Single Channel)
% Type: 'Single' / 'Double'.
% Range: [0, 1].
% Remarks:
% 1. References: "..."
% 2. Prefixes:
% - 'm' - Matrix.
% - 'v' - Vector.
% TODO:
% 1. F
% Release Notes:
% - 1.0.000 14/03/2015 Royi Avital
% * First release version.
% ----------------------------------------------------------------------------------------------- %
numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);
boxBlurKernelLength = (2 * boxRadius) + 1;
boxBlurKernelNumPixels = boxBlurKernelLength * boxBlurKernelLength;
normalizationFactor = 1 / boxBlurKernelNumPixels;
% Another Row / Column is needed as "Initial State" of the Integral.
% The added Row / Column at the end isn't required.
mInputImagePad = padarray(mInputImage, [(boxRadius + 1), (boxRadius + 1)], 'both', 'replicate');
mInputImagePad(:, 1) = 0;
mInputImagePad(1, :) = 0;
% Integrating to create the Integral Image
mIntegralImage = cumsum(mInputImagePad, 1);
mIntegralImage = cumsum(mIntegralImage, 2);
% Defining the indices
vRows = 1:numRows;
vCols = 1:numCols;
vValidRows = boxBlurKernelLength + vRows;
vValidCols = boxBlurKernelLength + vCols;
mOutputImage = mIntegralImage(vValidRows, vValidCols) - ...
mIntegralImage(vValidRows, vCols) - ...
mIntegralImage(vRows, vValidCols) + ...
mIntegralImage(vRows, vCols);
% Normalization to calculate the mean o fthe box
mOutputImage = mOutputImage * normalizationFactor;
end