当前位置:网站首页>canvas Gaussian blur effect
canvas Gaussian blur effect
2022-08-05 00:21:00 【The most brutal baby seals】
// imgData:canvas 的 getImageData 方法返回值
// radius:模糊的半径
function gaussBlur(imgData, radius) {
radius *= 3; //不知为什么,My blur radius is css中 filter:bulr The effect is the same when the value is tripled.
//Copy图片内容
let pixes = new Uint8ClampedArray(imgData.data);
const width = imgData.width;
const height = imgData.height;
let gaussMatrix = [],
gaussSum,
x, y,
r, g, b, a,
i, j, k,
w;
radius = Math.floor(radius);
const sigma = radius / 3;
a = 1 / (Math.sqrt(2 * Math.PI) * sigma);
b = -1 / (2 * sigma * sigma);
//生成高斯矩阵
for (i = -radius; i <= radius; i++) {
gaussMatrix.push(a * Math.exp(b * i * i));
}
//x 方向一维高斯运算
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
r = g = b = a = gaussSum = 0;
for (j = -radius; j <= radius; j++) {
k = x + j;
if (k >= 0 && k < width) {
i = (y * width + k) * 4;
w = gaussMatrix[j + radius];
r += pixes[i] * w;
g += pixes[i + 1] * w;
b += pixes[i + 2] * w;
a += pixes[i + 3] * w;
gaussSum += w;
}
}
i = (y * width + x) * 4;
//计算加权均值
imgData.data.set([r, g, b, a].map(v => v / gaussSum), i);
}
}
pixes.set(imgData.data);
//y 方向一维高斯运算
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
r = g = b = a = gaussSum = 0;
for (j = -radius; j <= radius; j++) {
k = y + j;
if (k >= 0 && k < height) {
i = (k * width + x) * 4;
w = gaussMatrix[j + radius];
r += pixes[i] * w;
g += pixes[i + 1] * w;
b += pixes[i + 2] * w;
a += pixes[i + 3] * w;
gaussSum += w;
}
}
i = (y * width + x) * 4;
imgData.data.set([r, g, b, a].map(v => v / gaussSum), i);
}
}
return imgData;
};
详细内容请看下文:
本文转载:https://blog.csdn.net/xuanhun521/article/details/109362541
边栏推荐
- leetcode: 267. Palindromic permutations II
- 2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
- 2022杭电多校第三场 L题 Two Permutations
- 典型相关分析CCA计算过程
- 简单的顺序结构程序(C语言)
- "Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
- 僵尸进程和孤儿进程
- Huggingface入门篇 II (QA)
- gorm joint table query - actual combat
- 【idea】idea配置sql格式化
猜你喜欢
随机推荐
What is next-generation modeling (with learning materials)
【LeetCode】滑动窗口题解汇总
Cloud native - Kubernetes 】 【 scheduling constraints
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
Redis visual management software Redis Desktop Manager2022
jenkins send mail system configuration
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
找不到DiscoveryClient类型的Bean
leetcode:266. 回文全排列
软件测试面试题:系统测试的策略有?
gorm的Raw与scan
2022杭电多校第三场 K题 Taxi
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
matlab中rcosdesign函数升余弦滚降成型滤波器
2022牛客多校第三场 J题 Journey
《MySQL入门很轻松》第2章:MySQL管理工具介绍
软件测试面试题:网络七层协仪具体?
电子行业MES管理系统的主要功能与用途
2022杭电多校第三场 L题 Two Permutations