Created
March 14, 2025 15:27
-
-
Save senyai/431c651cad33e2a7a6c289b3945b5bbb to your computer and use it in GitHub Desktop.
boxfilter for pytorch
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
def _diff_x(src: Tensor, w: int) -> Tensor: | |
cum_src = src.cumsum(-2) | |
left = cum_src[..., w : 2 * w + 1, :] | |
middle = cum_src[..., 2 * w + 1 :, :] - cum_src[..., : -2 * w - 1, :] | |
right = cum_src[..., -1:, :] - cum_src[..., -2 * w - 1 : -w - 1, :] | |
return torch.vstack((left, middle, right)) | |
def _diff_y(src: Tensor, w: int) -> Tensor: | |
cum_src = src.cumsum(-1) | |
left = cum_src[..., w : 2 * w + 1] | |
middle = cum_src[..., 2 * w + 1 :] - cum_src[..., : -2 * w - 1] | |
right = cum_src[..., -1:] - cum_src[..., -2 * w - 1 : -w - 1] | |
return torch.hstack((left, middle, right)) | |
def _box_filter2d(src: Tensor, wing: int) -> Tensor: | |
return _diff_y(_diff_x(src, wing), wing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment