Skip to content
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

[FEAT] Add Gaussian Blur to RawImage #1103

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BritishWerewolf
Copy link
Contributor

@BritishWerewolf BritishWerewolf commented Dec 19, 2024

The reason for this, is for models like U2Net where the input image size is 320x320, meaning that the resulting output needs to be resized, and can have sharp edges; blurring before resizing can smooth this out.

This is the code that I used to generate the images.

const kernels = [5, 9, 17];
const sigmas = [2, 3, 5];
const chunk = 4;
for (let i = 0; i < kernels.length; i++) {
    const kernel = kernels[i];
    const sigma = sigmas[i];
    image
        .gaussianBlur(kernel, sigma, chunk)
        then(image => image.save(`blurred-(${kernel}-${sigma}).png`));
}

Sigma should roughly be a third of kernel size (from what I found researching).
Kernel must be odd, since we want an even square around the pixel.
For example, if we had a 3x3 kernel grid, then when we place this on our pixel there is an even space all around that pixel (in this case 1 pixel).

Here is a demo of what this implementation currently looks like.

Image Kernel Sigma 1 chunk 4 chunks 12 chunks
The original image. N/A N/A N/A N/A N/A
The image slightly more blurred than the original. 5 2 9 seconds 3 seconds 3 seconds
The slightly more blurred image, slightly more blurred again. 9 3 31 seconds 4 seconds 5 seconds
The final image that is blurred even more! 17 5 1 minute, 42 seconds 8 seconds 7 seconds

Comment on lines +661 to +664
// Kernel must be odd because each pixel must sit evenly in the middle.
if (kernelSize % 2 === 0) {
throw new Error('Kernel size must be odd.');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this, is because a kernel is essentially a square that is applied to every pixel in an image.

Consider a grid of 5x5, it would look like this, with our pixel (P) in the middle.

. . . . .
. . . . .
. . P . .
. . . . .
. . . . .

Here, it is clear to see that there is an even spacing around the pixel.

Now, consider what would happen if we used an even kernel size of 4.

. . . .
. . . .
. . P .
. . . .

The pixel does not sit neatly in the middle of the kernel and this is a problem.

@BritishWerewolf BritishWerewolf marked this pull request as draft December 19, 2024 09:59
This is an attempt to parallelise the function.
@BritishWerewolf BritishWerewolf marked this pull request as ready for review December 19, 2024 22:30
@BritishWerewolf BritishWerewolf changed the title [WIP] Add Gaussian Blur to RawImage [FEAT] Add Gaussian Blur to RawImage Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant