fork download
  1. function sobel(image, width, height) {
  2. var temp = new Array(image.length / 4);
  3. function get_index(_x, _y) {
  4. return (width * _y + _x) * 4;
  5. }
  6. // モノクロにする
  7. for (var i = 0; i < image.length; i += 4) {
  8. image[i] = Math.floor((image[i] + image[i+1] + image[i+2]) / 3);
  9. }
  10. for (var y = 1; y < height - 1; y++) {
  11. var index = width * y * 4;
  12. for (var x = 1; x < width - 1; x++) {
  13. var ghs = image[ index + (x - 1 - width) * 4 ] * -1
  14. + image[ index + (x + 1 - width) * 4 ]
  15. + image[ index + (x - 1) * 4 ] * -2
  16. + image[ index + (x + 1) * 4 ] * 2
  17. + image[ index + (x - 1 + width) * 4 ] * -1
  18. + image[ index + (x + 1 + width) * 4 ];
  19. var gvs = image[ index + (x - 1 - width) * 4 ] * -1
  20. + image[ index + (x - width) * 4 ] * -2
  21. + image[ index + (x + 1 - width) * 4 ] * -1
  22. + image[ index + (x - 1 + width) * 4 ]
  23. + image[ index + (x + width) * 4 ] * 2
  24. + image[ index + (x + 1 + width) * 4 ];
  25. var g = Math.floor(Math.sqrt(ghs * ghs + gvs * gvs));
  26. index = get_index(x, y) / 4;
  27. temp[ index ] = 255 - g;
  28. }
  29. }
  30. for (var i = 0; i < image.length; i += 4) {
  31. image[i] = image[i+1] = image[i+2] = temp[i / 4];
  32. }
  33. }
  34.  
  35. function gaussian3x3(image, width, height) {
  36. var temp = new Array(image.length);
  37. function get_index(_x, _y) {
  38. return (width * _y + _x) * 4;
  39. }
  40. function filter(_x, _y, _i) {
  41. return (
  42. image[ get_index(_x-1, _y-1) + _i ]
  43. + image[ get_index(_x , _y-1) + _i ] * 2
  44. + image[ get_index(_x+1, _y-1) + _i ]
  45. + image[ get_index(_x-1, _y ) + _i ] * 2
  46. + image[ get_index(_x , _y ) + _i ] * 4
  47. + image[ get_index(_x-1, _y ) + _i ] * 2
  48. + image[ get_index(_x-1, _y+1) + _i ]
  49. + image[ get_index(_x , _y+1) + _i ] * 2
  50. + image[ get_index(_x+1, _y+1) + _i ]
  51. ) / 16;
  52. }
  53. for (var i = 0; i < image.length; i += 4) {
  54. temp[i ] = image[i ];
  55. temp[i + 1] = image[i + 1];
  56. temp[i + 2] = image[i + 2];
  57. }
  58. for (var y = 1; y < height - 1; y++) {
  59. for (var x = 1; x < width - 1; x++) {
  60. var index = get_index(x, y);
  61. temp[index ] = filter(x, y, 0);
  62. temp[index + 1] = filter(x, y, 1);
  63. temp[index + 2] = filter(x, y, 2);
  64. }
  65. }
  66. for (var i = 0; i < image.length; i += 4) {
  67. image[i ] = temp[i ];
  68. image[i+1] = temp[i+1];
  69. image[i+2] = temp[i+2];
  70. }
  71. }
  72.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty