高斯模糊代码

  1. function gaussBlur(imgData) {
  2. var pixes = imgData.data;
  3. var width = imgData.width;
  4. var height = imgData.height;
  5. var gaussMatrix = [],
  6. gaussSum = 0,
  7. x, y,
  8. r, g, b, a,
  9. i, j, k, len;
  10. var radius = 10;
  11. var sigma = 5;
  12. a = 1 / (Math.sqrt(2 * Math.PI) * sigma);
  13. b = -1 / (2 * sigma * sigma);
  14. //生成高斯矩阵
  15. for (i = 0, x = -radius; x <= radius; x++, i++) {
  16. g = a * Math.exp(b * x * x);
  17. gaussMatrix[i] = g;
  18. gaussSum += g;
  19. }
  20. //归一化, 保证高斯矩阵的值在[0,1]之间
  21. for (i = 0, len = gaussMatrix.length; i < len; i++) {
  22. gaussMatrix[i] /= gaussSum;
  23. }
  24. //x 方向一维高斯运算
  25. for (y = 0; y < height; y++) {
  26. for (x = 0; x < width; x++) {
  27. r = g = b = a = 0;
  28. gaussSum = 0;
  29. for (j = -radius; j <= radius; j++) {
  30. k = x + j;
  31. if (k >= 0 && k < width) {//确保 k 没超出 x 的范围
  32. //r,g,b,a 四个一组
  33. i = (y * width + k) * 4;
  34. r += pixes[i] * gaussMatrix[j + radius];
  35. g += pixes[i + 1] * gaussMatrix[j + radius];
  36. b += pixes[i + 2] * gaussMatrix[j + radius];
  37. // a += pixes[i + 3] * gaussMatrix[j];
  38. gaussSum += gaussMatrix[j + radius];
  39. }
  40. }
  41. i = (y * width + x) * 4;
  42. // 除以 gaussSum 是为了消除处于边缘的像素, 高斯运算不足的问题
  43. // console.log(gaussSum)
  44. pixes[i] = r / gaussSum;
  45. pixes[i + 1] = g / gaussSum;
  46. pixes[i + 2] = b / gaussSum;
  47. // pixes[i + 3] = a ;
  48. }
  49. }
  50. //y 方向一维高斯运算
  51. for (x = 0; x < width; x++) {
  52. for (y = 0; y < height; y++) {
  53. r = g = b = a = 0;
  54. gaussSum = 0;
  55. for (j = -radius; j <= radius; j++) {
  56. k = y + j;
  57. if (k >= 0 && k < height) {//确保 k 没超出 y 的范围
  58. i = (k * width + x) * 4;
  59. r += pixes[i] * gaussMatrix[j + radius];
  60. g += pixes[i + 1] * gaussMatrix[j + radius];
  61. b += pixes[i + 2] * gaussMatrix[j + radius];
  62. // a += pixes[i + 3] * gaussMatrix[j];
  63. gaussSum += gaussMatrix[j + radius];
  64. }
  65. }
  66. i = (y * width + x) * 4;
  67. pixes[i] = r / gaussSum;
  68. pixes[i + 1] = g / gaussSum;
  69. pixes[i + 2] = b / gaussSum;
  70. }
  71. }
  72. //end
  73. return imgData;
  74. }

给图片是实现高斯模糊

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. body{
  9. margin: 0;
  10. overflow: hidden;
  11. }
  12. canvas{
  13. background: #000;
  14. }
  15. #back{
  16. margin: 0;
  17. position: absolute;
  18. left: 600px;
  19. top: 0px;
  20. width: 100px;
  21. height: 100px;
  22. background-size: cover;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <canvas id="canvas" width="500" height="500"></canvas>
  28. <img src="" id="img" alt="">
  29. <div id="back"></div>
  30. <script src="./高斯模糊.js"></script>
  31. <script>
  32. const img = new Image()
  33. img.src = './images/img_01.jpg';
  34. img.onload = function(e){
  35. window.player.blurImg(img,document.getElementById('back'))
  36. }
  37. </script>
  38. </body>
  39. </html>
  1. (function (root) {
  2. /**
  3. * 将图片进行高斯模糊
  4. * @param {*} imgData 图片的数据
  5. */
  6. function gaussBlur(imgData) {
  7. var pixes = imgData.data;
  8. var width = imgData.width;
  9. var height = imgData.height;
  10. var gaussMatrix = [],
  11. gaussSum = 0,
  12. x, y,
  13. r, g, b, a,
  14. i, j, k, len;
  15. var radius = 10;
  16. var sigma = 5;
  17. a = 1 / (Math.sqrt(2 * Math.PI) * sigma);
  18. b = -1 / (2 * sigma * sigma);
  19. //生成高斯矩阵
  20. for (i = 0, x = -radius; x <= radius; x++, i++) {
  21. g = a * Math.exp(b * x * x);
  22. gaussMatrix[i] = g;
  23. gaussSum += g;
  24. }
  25. //归一化, 保证高斯矩阵的值在[0,1]之间
  26. for (i = 0, len = gaussMatrix.length; i < len; i++) {
  27. gaussMatrix[i] /= gaussSum;
  28. }
  29. //x 方向一维高斯运算
  30. for (y = 0; y < height; y++) {
  31. for (x = 0; x < width; x++) {
  32. r = g = b = a = 0;
  33. gaussSum = 0;
  34. for (j = -radius; j <= radius; j++) {
  35. k = x + j;
  36. if (k >= 0 && k < width) {//确保 k 没超出 x 的范围
  37. //r,g,b,a 四个一组
  38. i = (y * width + k) * 4;
  39. r += pixes[i] * gaussMatrix[j + radius];
  40. g += pixes[i + 1] * gaussMatrix[j + radius];
  41. b += pixes[i + 2] * gaussMatrix[j + radius];
  42. // a += pixes[i + 3] * gaussMatrix[j];
  43. gaussSum += gaussMatrix[j + radius];
  44. }
  45. }
  46. i = (y * width + x) * 4;
  47. // 除以 gaussSum 是为了消除处于边缘的像素, 高斯运算不足的问题
  48. // console.log(gaussSum)
  49. pixes[i] = r / gaussSum;
  50. pixes[i + 1] = g / gaussSum;
  51. pixes[i + 2] = b / gaussSum;
  52. // pixes[i + 3] = a ;
  53. }
  54. }
  55. //y 方向一维高斯运算
  56. for (x = 0; x < width; x++) {
  57. for (y = 0; y < height; y++) {
  58. r = g = b = a = 0;
  59. gaussSum = 0;
  60. for (j = -radius; j <= radius; j++) {
  61. k = y + j;
  62. if (k >= 0 && k < height) {//确保 k 没超出 y 的范围
  63. i = (k * width + x) * 4;
  64. r += pixes[i] * gaussMatrix[j + radius];
  65. g += pixes[i + 1] * gaussMatrix[j + radius];
  66. b += pixes[i + 2] * gaussMatrix[j + radius];
  67. // a += pixes[i + 3] * gaussMatrix[j];
  68. gaussSum += gaussMatrix[j + radius];
  69. }
  70. }
  71. i = (y * width + x) * 4;
  72. pixes[i] = r / gaussSum;
  73. pixes[i + 1] = g / gaussSum;
  74. pixes[i + 2] = b / gaussSum;
  75. }
  76. }
  77. //end
  78. return imgData;
  79. }
  80. function blurImg(img, dom) {
  81. /**@type{HTMLCanvasElement}*/
  82. const canvas = document.getElementById('canvas');
  83. const ctx = canvas.getContext('2d');
  84. canvas.width = 100;
  85. canvas.height = 100;
  86. // canvas 绘制图片
  87. ctx.drawImage(img, 0, 0, img.width, img.height,0,0,canvas.width,canvas.height)
  88. // 读取画布上图片的信息
  89. const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  90. // 将其高斯模糊后的图片数据
  91. const gaussData = gaussBlur(imgData);
  92. // 将其高斯模糊后的数据覆盖到原图片的数据
  93. ctx.putImageData(gaussData, 0, 0);
  94. // 获取图片的地址
  95. const imgSrc = canvas.toDataURL();
  96. dom.style.backgroundImage='url('+imgSrc+')';
  97. }
  98. // 暴露给window.player身上
  99. root.blurImg = blurImg;
  100. })(window.player || (window.player = {}))