-
Notifications
You must be signed in to change notification settings - Fork 809
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
base: main
Are you sure you want to change the base?
[FEAT] Add Gaussian Blur to RawImage #1103
Conversation
// 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.'); | ||
} |
There was a problem hiding this comment.
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.
This is an attempt to parallelise the function.
const horizontalPass = new Float32Array(this.data.length); | ||
const verticalPass = new Uint8ClampedArray(this.data.length); | ||
|
||
const numChunks = navigator.hardwareConcurrency || 4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was originally a hard coded value, passed into the function, however this approach makes more sense.
Looking at the table in the opening post, it shows that (at least for my machine) there is no benefit to increase the chunks above 4.
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.
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.
EDIT: This has since been updated so that a number of chunks are not passed, and instead we will use the
navigator.hardwareConcurrency
property to find the ideal number of chunks.As you can see above, there was no time save between 4 and 12 because my PC had 4 cores.