Repository navigation
feat(WASM): add bilateral filter #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ryan-Millard
merged 15 commits into
Ryan-Millard:feat/kmean-preprocessing/bilateral-filter
from
fransafu:feature/bilateral-filter
Jan 3, 2026
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9df4747
feat(WASM): add bilateral filter
fransafu 073e6f8
test: add call method test, and custom parameters test
fransafu 227dd6a
refactor: rename image parameter to pixels to match lib conventions
fransafu 5c3b6c4
test: add bilateralFilter as part of helper methods return test
fransafu 009e0a7
refactor: remove unused variable MAX_PIXEL_VAL
fransafu df40601
feat: add headers to be used by WASM (best practice)
fransafu 9f30fb0
docs(WASM): add bilateral_filter documentation (overview, explained, …
fransafu a8ff686
feat(bilateral_filter): add upper bound validation for sigma_spatial
fransafu a16b186
docs: improve bilateral headers documentation
fransafu 1bd0962
docs(bilateral filter): explain use of Gaussian kernels inside formula
Ryan-Millard 6dc5097
docs(bilateral filter): better styling
Ryan-Millard 35f4e0d
docs(bilateral filter): better styling
Ryan-Millard ebefadb
docs(bilateral filter): correct sidebar_position
Ryan-Millard e5a0c9b
docs(bilateral filter): mobile accessibility
Ryan-Millard 39718f4
docs(bilateral filter): explicit description in module overview
Ryan-Millard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #include "image_utils.h" | ||
| #include <cmath> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| #include <cstring> | ||
|
|
||
| static constexpr double SIGMA_RADIUS_FACTOR = 3.0; | ||
| static constexpr int MAX_PIXEL_VAL = 255; | ||
| // Max possible squared Euclidean distance in a 3-channel 8-bit image: 255^2 * 3 = 195075 | ||
| // Means max delta between images (imageA - imageB) in RGB channels (255^2 * 3) | ||
| static constexpr int MAX_RGB_DIST_SQ = 255 * 255 * 3; | ||
|
|
||
| /* | ||
| The Bilateral Filter applies a composite weight based on both spatial distance and radiometric difference (intensity) to return an image that is smoothed while preserving edges. | ||
| It reduces noise in flat regions while preserving edges by assigning near-zero weight to pixels across high-contrast boundaries. | ||
|
|
||
| Parameters: | ||
| - image: Pointer to RGBA pixel buffer | ||
| - width, height: Image dimensions (px) | ||
| - sigma_spatial: Gaussian standard deviation for spatial proximity (spatial decay) | ||
| - sigma_range: Gaussian standard deviation for intensity difference (radiometric decay) | ||
| */ | ||
| void bilateral_filter(uint8_t *image, size_t width, size_t height, | ||
| double sigma_spatial, double sigma_range) { | ||
| if (sigma_spatial <= 0.0 || sigma_range <= 0.0) return; | ||
|
|
||
| const int radius = static_cast<int>(std::ceil(SIGMA_RADIUS_FACTOR * sigma_spatial)); | ||
| const int kernel_width = 2 * radius + 1; | ||
| const size_t stride = width * 4; | ||
| std::vector<uint8_t> result(width * height * 4); | ||
|
|
||
| // NOTE: precompute Spatial Weights (Gaussian Kernel) | ||
| std::vector<double> spatial_weights(kernel_width * kernel_width); | ||
| double two_sigma_space_sq = 2 * sigma_spatial * sigma_spatial; | ||
|
|
||
| for (int ky = -radius; ky <= radius; ++ky) { | ||
| for (int kx = -radius; kx <= radius; ++kx) { | ||
| double dist2 = static_cast<double>(kx * kx + ky * ky); | ||
| spatial_weights[(ky + radius) * kernel_width + (kx + radius)] = | ||
| std::exp(-dist2 / two_sigma_space_sq); | ||
| } | ||
| } | ||
|
|
||
| // NOTE: precompute Range Weights | ||
| std::vector<double> range_lut(MAX_RGB_DIST_SQ + 1); | ||
| double two_sigma_range_sq = 2 * sigma_range * sigma_range; | ||
|
|
||
| for (int i = 0; i <= MAX_RGB_DIST_SQ; ++i) { | ||
| range_lut[i] = std::exp(-static_cast<double>(i) / two_sigma_range_sq); | ||
| } | ||
|
|
||
| int h = static_cast<int>(height); | ||
| int w = static_cast<int>(width); | ||
|
|
||
| for (int y = 0; y < h; ++y) { | ||
| for (int x = 0; x < w; ++x) { | ||
| size_t center_idx = (y * width + x) * 4; | ||
|
|
||
| uint8_t r0 = image[center_idx]; | ||
| uint8_t g0 = image[center_idx + 1]; | ||
| uint8_t b0 = image[center_idx + 2]; | ||
| uint8_t a0 = image[center_idx + 3]; | ||
|
|
||
| double r_acc = 0.0, g_acc = 0.0, b_acc = 0.0, weight_acc = 0.0; | ||
|
|
||
| for (int ky = -radius; ky <= radius; ++ky) { | ||
| int ny = std::clamp(y + ky, 0, h - 1); | ||
|
|
||
| for (int kx = -radius; kx <= radius; ++kx) { | ||
| int nx = std::clamp(x + kx, 0, w - 1); | ||
|
|
||
| size_t neighbor_idx = (ny * width + nx) * 4; | ||
|
|
||
| uint8_t r = image[neighbor_idx]; | ||
| uint8_t g = image[neighbor_idx + 1]; | ||
| uint8_t b = image[neighbor_idx + 2]; | ||
|
|
||
| double w_space = spatial_weights[(ky + radius) * kernel_width + (kx + radius)]; | ||
|
|
||
| int dr = static_cast<int>(r) - r0; | ||
| int dg = static_cast<int>(g) - g0; | ||
| int db = static_cast<int>(b) - b0; | ||
| int dist_sq = dr*dr + dg*dg + db*db; | ||
|
|
||
| double w_range = range_lut[dist_sq]; | ||
| double w = w_space * w_range; | ||
|
|
||
| r_acc += r * w; | ||
| g_acc += g * w; | ||
| b_acc += b * w; | ||
| weight_acc += w; | ||
| } | ||
| } | ||
|
|
||
| result[center_idx] = static_cast<uint8_t>(std::clamp(r_acc / weight_acc, 0.0, 255.0)); | ||
| result[center_idx + 1] = static_cast<uint8_t>(std::clamp(g_acc / weight_acc, 0.0, 255.0)); | ||
| result[center_idx + 2] = static_cast<uint8_t>(std::clamp(b_acc / weight_acc, 0.0, 255.0)); | ||
| result[center_idx + 3] = a0; | ||
| } | ||
| } | ||
|
|
||
| std::memcpy(image, result.data(), result.size()); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.