当前位置:网站首页>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
边栏推荐
- [Cloud Native--Kubernetes] Pod Controller
- Software testing interview questions: What are the strategies for system testing?
- 典型相关分析CCA计算过程
- Modelers experience sharing: model study method
- 【idea】idea配置sql格式化
- What is next-generation modeling (with learning materials)
- leetcode经典例题——单词拆分
- 软件测试面试题:软件验收测试的合格通过准则?
- Essential knowledge for entry-level 3D game modelers
- 软件测试面试题:负载测试、容量测试、强度测试的区别?
猜你喜欢
随机推荐
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
IDEA file encoding modification
软件质量评估的通用模型
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
STC89C52RC的P4口的应用问题
《MySQL入门很轻松》第2章:MySQL管理工具介绍
2022杭电多校 第三场 B题 Boss Rush
Brainstorm: Complete Backpack
动态上传jar包热部署
软件测试面试题:网络七层协仪具体?
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
工业物联网 —— 新型数据库的召唤
软件测试面试题:做好测试计划的关键是什么?
Couple Holding Hands [Greedy & Abstract]
2 用D435i运行VINS-fusion
tiup telemetry
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
怎样进行在不改变主线程执行的时候,进行日志的记录
leetcode: 267. Palindromic permutations II
NMS原理及其代码实现