Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[FEAT] Add Gaussian Blur to RawImage#1103

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
BritishWerewolf wants to merge5 commits intohuggingface:main
base:main
Choose a base branch
Loading
fromBritishWerewolf:add-gaussian-blur

Conversation

BritishWerewolf
Copy link
Contributor

@BritishWerewolfBritishWerewolf commentedDec 19, 2024
edited
Loading

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.

constkernels=[5,9,17];constsigmas=[2,3,5];constchunk=4;for(leti=0;i<kernels.length;i++){constkernel=kernels[i];constsigma=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.

ImageKernelSigma1 chunk4 chunks12 chunks
The original image.N/AN/AN/AN/AN/A
The image slightly more blurred than the original.529 seconds3 seconds3 seconds
The slightly more blurred image, slightly more blurred again.9331 seconds4 seconds5 seconds
The final image that is blurred even more!1751 minute, 42 seconds8 seconds7 seconds

EDIT: This has since been updated so that a number of chunks are not passed, and instead we will use thenavigator.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.

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
ContributorAuthor

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 of4.

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

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

@BritishWerewolfBritishWerewolf marked this pull request as draftDecember 19, 2024 09:59
@BritishWerewolfBritishWerewolf marked this pull request as ready for reviewDecember 19, 2024 22:30
@BritishWerewolfBritishWerewolf changed the title[WIP] Add Gaussian Blur to RawImage[FEAT] Add Gaussian Blur to RawImageDec 19, 2024
const horizontalPass = new Float32Array(this.data.length);
const verticalPass = new Uint8ClampedArray(this.data.length);

const numChunks = navigator.hardwareConcurrency || 4;
Copy link
ContributorAuthor

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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@BritishWerewolf

[8]ページ先頭

©2009-2025 Movatter.jp