当前位置:网站首页>canvas 高斯模糊效果
canvas 高斯模糊效果
2022-08-05 00:15:00 【最凶残的小海豹】
// imgData:canvas 的 getImageData 方法返回值
// radius:模糊的半径
function gaussBlur(imgData, radius) {
radius *= 3; //不知为什么,我的模糊半径是 css中 filter:bulr 值的三倍时效果才一致。
//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
边栏推荐
- 克服项目管理中恐惧心理
- 三大技巧让你成功入门3D建模,零基础小白必看
- Essential knowledge for entry-level 3D game modelers
- 头脑风暴:完全背包
- 数据类型-整型(C语言)
- The role of the annotation @ EnableAutoConfiguration and how to use
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- Getting started with 3D modeling for games, what modeling software can I choose?
- 学会反射后,我被录取了(干货)
- leetcode:267. 回文排列 II
猜你喜欢
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
资深游戏建模师告知新手,游戏场景建模师必备软件有哪些?
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
Metasploit-域名上线隐藏IP
golang 协程的实现原理
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
图解 Canvas 入门
随机推荐
一、爬虫基本概念
leetcode:266. 回文全排列
SQL关联表更新
gorm联表查询-实战
对写作的一些感悟
D - I Hate Non-integer Number (选数的计数dp
Privacy Computing Overview
Essential knowledge for entry-level 3D game modelers
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
Modelers experience sharing: model study method
【云原生--Kubernetes】调度约束
Cloud native - Kubernetes 】 【 scheduling constraints
怎样进行在不改变主线程执行的时候,进行日志的记录
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
LeetCode Hot 100
动态上传jar包热部署
E - Distance Sequence (前缀和优化dp
Mysql based
关于使用read table 语句