当前位置:网站首页>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
边栏推荐
- "Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
- 【LeetCode】图解 904. 水果成篮
- uinty lua 关于异步函数的终极思想
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- tiup uninstall
- 电子行业MES管理系统的主要功能与用途
- 性能测试如何准备测试数据
- 软件测试面试题:LoadRunner 分为哪三个模块?
- [LeetCode] Summary of Matrix Simulation Related Topics
- Detailed explanation of common DNS resource record types
猜你喜欢

node使用redis

Mysql_13 事务

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

进程间通信和线程间通信

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

Mysql_14 存储引擎

node uses redis

子连接中的参数传递

数据类型及输入输出初探(C语言)
![Couple Holding Hands [Greedy & Abstract]](/img/7d/1cafc000dc58f1c5e2e92150be7953.png)
Couple Holding Hands [Greedy & Abstract]
随机推荐
Mysql_13 事务
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
gorm联表查询-实战
怎样进行在不改变主线程执行的时候,进行日志的记录
性能测试如何准备测试数据
导入JankStats检测卡帧库遇到问题记录
电子行业MES管理系统的主要功能与用途
电赛必备技能___定时ADC+DMA+串口通信
[Cloud Native--Kubernetes] Pod Controller
【LeetCode】Summary of Two Pointer Problems
2022牛客多校第三场 A Ancestor
【云原生--Kubernetes】Pod控制器
仿网易云音乐小程序-uniapp
2022杭电多校第一场 1004 Ball
软件测试面试题:网络七层协仪具体?
软件开发工具的技术要素
tiup update
MAUI Blazor 权限经验分享 (定位,使用相机)
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
NMS原理及其代码实现