Skip to content

Commit

Permalink
Merge pull request #74 from ljubobratovicrelja/bilateral-fix-2
Browse files Browse the repository at this point in the history
Bilateral filter fix
  • Loading branch information
ljubobratovicrelja authored Nov 11, 2016
2 parents 9d0e4d5 + 8acde56 commit 7b97737
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/filter/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(string[] args)
auto cannyEdges = gray.canny!ubyte(75);

// perform bilateral blurring
auto bilBlur = imslice.bilateralFilter(10.0f, 5);
auto bilBlur = imslice.bilateralFilter(10.0f, 10.0f, 5);

// Add salt and pepper noise at input image green channel
auto noisyImage = imslice.slice;
Expand Down
15 changes: 8 additions & 7 deletions source/dcv/imgproc/filter.d
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ Non-linear, edge-preserving and noise-reducing smoothing filtering algorithm.
Params:
bc = Boundary condition test used to index the image slice.
slice = Slice of the input image.
sigma = Smoothing strength parameter.
sigmaCol = Color sigma value.
sigmaSpace = Spatial sigma value.
kernelSize = Size of convolution kernel. Must be odd number.
prealloc = Optional pre-allocated result image buffer. If not of same shape as input slice, its allocated anew.
pool = Optional TaskPool instance used to parallelize computation.
Expand All @@ -751,7 +752,7 @@ Returns:
Slice of filtered image.
*/
Slice!(N, OutputType*) bilateralFilter(alias bc = neumann, InputType, OutputType = InputType, size_t N)(Slice!(N,
InputType*) slice, float sigma, uint kernelSize, Slice!(N,
InputType*) slice, float sigmaCol, float sigmaSpace, uint kernelSize, Slice!(N,
OutputType*) prealloc = emptySlice!(N, OutputType), TaskPool pool = taskPool) if (N == 2)
in
{
Expand Down Expand Up @@ -793,8 +794,9 @@ body
foreach (int kc; c - ksh .. c + ksh + 1)
{
auto ck = (c - kc) ^^ 2;
float c_val = exp(-0.5f * ((sqrt(cast(float)(ck + rk)) / sigma) ^^ 2));
float s_val = exp(-0.5f * ((cast(float)(bc(slice, kr, kc) - p_val) / sigma) ^^ 2));
auto cdiff = bc(slice, kr, kc) - p_val;
float c_val = exp((ck + rk) / (-2.0f * sigmaSpace * sigmaSpace));
float s_val = exp((cdiff * cdiff) / (-2.0f * sigmaCol * sigmaCol));
mask[i, j] = c_val * s_val;
++j;
}
Expand All @@ -809,7 +811,6 @@ body
foreach (kr; r - ksh .. r + ksh + 1)
{
j = 0;
auto rk = (r - kr) ^^ 2;
foreach (kc; c - ksh .. c + ksh + 1)
{
res_val += (mask[i, j] / mask_sum) * bc(slice, kr, kc);
Expand All @@ -827,7 +828,7 @@ body

/// ditto
Slice!(N, OutputType*) bilateralFilter(alias bc = neumann, InputType, OutputType = InputType, size_t N)(Slice!(N,
InputType*) slice, float sigma, uint kernelSize, Slice!(N,
InputType*) slice, float sigmaCol, float sigmaSpace, uint kernelSize, Slice!(N,
OutputType*) prealloc = emptySlice!(N, OutputType)) if (N == 3)
{
if (prealloc.empty || prealloc.shape != slice.shape)
Expand All @@ -837,7 +838,7 @@ Slice!(N, OutputType*) bilateralFilter(alias bc = neumann, InputType, OutputType

foreach (channel; 0 .. slice.length!2)
{
bilateralFilter!(bc, InputType, OutputType)(slice[0 .. $, 0 .. $, channel], sigma,
bilateralFilter!(bc, InputType, OutputType)(slice[0 .. $, 0 .. $, channel], sigmaCol, sigmaSpace,
kernelSize, prealloc[0 .. $, 0 .. $, channel]);
}

Expand Down
4 changes: 2 additions & 2 deletions source/dcv/multiview/stereo/matching.d
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ DisparityRefiner medianDisparityFilter(size_t windowSize = 3)
/**
Applies a bilateral filter to the disparity map in order to correct outliers.
*/
DisparityRefiner bilateralDisparityFilter(uint windowSize, float sigma)
DisparityRefiner bilateralDisparityFilter(uint windowSize, float sigmaCol, float sigmaSpace)
{
void disparityRefiner(const ref StereoPipelineProperties props, DisparityMap disp)
{
disp[] = bilateralFilter(disp, sigma, windowSize);
disp[] = bilateralFilter(disp, sigmaCol, sigmaSpace, windowSize);
}

return &disparityRefiner;
Expand Down

0 comments on commit 7b97737

Please sign in to comment.