当前位置:网站首页>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
边栏推荐
- Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
- 软件质量评估的通用模型
- Mysql_13 事务
- How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
- QSunSync 七牛云文件同步工具,批量上传
- Flutter启动流程(Skia引擎)介绍与使用
- KT148A电子语音芯片ic方案适用的场景以及常见产品类型
- [230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
- 克服项目管理中恐惧心理
- LeetCode Hot 100
猜你喜欢
子连接中的参数传递
英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps
什么是次世代建模(附学习资料)
【LeetCode】矩阵模拟相关题目汇总
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
KT148A语音芯片ic工作原理以及芯片的内部架构描述
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
oracle创建用户
导入JankStats检测卡帧库遇到问题记录
建模师经验分享:模型学习方法
随机推荐
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
对写作的一些感悟
The master teaches you the 3D real-time character production process, the game modeling process sharing
【LeetCode】图解 904. 水果成篮
【七夕情人节特效】-- canvas实现满屏爱心
leetcode:267. 回文排列 II
大师教你3D实时角色制作流程,游戏建模流程分享
Mathematical Principles of Matrix
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
Cython
Essential knowledge for entry-level 3D game modelers
【LeetCode】矩阵模拟相关题目汇总
OpenCV:10特征检测
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
matlab中rcosdesign函数升余弦滚降成型滤波器
.net(C#)获取两个日期间隔的年月日
typeScript - Partially apply a function
2022年华数杯数学建模
【unity编译器扩展之模型动画拷贝】
手写分布式配置中心(1)