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

Convolution is slow #327

Open
targos opened this issue May 10, 2023 · 2 comments
Open

Convolution is slow #327

targos opened this issue May 10, 2023 · 2 comments

Comments

@targos
Copy link
Member

targos commented May 10, 2023

There are probably multiple places where we can improve it.

Biggest bottleneck is the border handling (70% of the time is spent there):

// Apply convolution on the left and right borders
for (let channel = 0; channel < channels; channel++) {
for (let bY = 0; bY < height; bY++) {
for (let bX = 0; bX < kernelOffsetX; bX++) {
const index = bY * width + bX;
const bXopp = width - bX - 1;
const bYopp = height - bY - 1;
const indexOpp = bYopp * width + bXopp;
newImage.setValueByIndex(
index,
channel,
computeConvolutionValue(
bX,
bY,
channel,
image,
kernel,
interpolateBorder,
{ clamp },
),
);
newImage.setValueByIndex(
indexOpp,
channel,
computeConvolutionValue(
bXopp,
bYopp,
channel,
image,
kernel,
interpolateBorder,
{ clamp },
),
);
}
}
}
// apply the convolution on the top and bottom borders
for (let channel = 0; channel < channels; channel++) {
for (let bX = 0; bX < width; bX++) {
for (let bY = 0; bY < kernelOffsetY; bY++) {
const index = bY * width + bX;
const bXopp = width - bX - 1;
const bYopp = height - bY - 1;
const indexOpp = bYopp * width + bXopp;
newImage.setValueByIndex(
index,
channel,
computeConvolutionValue(
bX,
bY,
channel,
image,
kernel,
interpolateBorder,
{ clamp },
),
);
newImage.setValueByIndex(
indexOpp,
channel,
computeConvolutionValue(
bXopp,
bYopp,
channel,
image,
kernel,
interpolateBorder,
{ clamp },
),
);
}
}
}

Tested with:

import { Image } from 'image-js';

const img = new Image(2000, 1000);

img.gaussianBlur({ sigma: 10 });

console.time('blur')
img.gaussianBlur({ sigma: 10 });
console.timeEnd('blur')

console.log(img);
@targos targos self-assigned this May 11, 2023
@targos
Copy link
Member Author

targos commented May 12, 2023

First improvement: #336

@targos targos removed their assignment May 29, 2023
@Xstoudi
Copy link
Contributor

Xstoudi commented Jun 23, 2023

Still seem slow.
At some point, could we consider using WebGPU when supported in environment to handle heavy operations?

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

No branches or pull requests

2 participants