This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
sobel.js
138 lines (115 loc) · 3.62 KB
/
sobel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
(function(root) {
'use strict';
function Sobel(imageData) {
if (!(this instanceof Sobel)) {
return new Sobel(imageData);
}
var width = imageData.width;
var height = imageData.height;
var kernelX = [
[-1,0,1],
[-2,0,2],
[-1,0,1]
];
var kernelY = [
[-1,-2,-1],
[0,0,0],
[1,2,1]
];
var sobelData = [];
var grayscaleData = [];
function bindPixelAt(data) {
return function(x, y, i) {
i = i || 0;
return data[((width * y) + x) * 4 + i];
};
}
var data = imageData.data;
var pixelAt = bindPixelAt(data);
var x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
var r = pixelAt(x, y, 0);
var g = pixelAt(x, y, 1);
var b = pixelAt(x, y, 2);
var avg = (r + g + b) / 3;
grayscaleData.push(avg, avg, avg, 255);
}
}
pixelAt = bindPixelAt(grayscaleData);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
var pixelX = (
(kernelX[0][0] * pixelAt(x - 1, y - 1)) +
(kernelX[0][1] * pixelAt(x, y - 1)) +
(kernelX[0][2] * pixelAt(x + 1, y - 1)) +
(kernelX[1][0] * pixelAt(x - 1, y)) +
(kernelX[1][1] * pixelAt(x, y)) +
(kernelX[1][2] * pixelAt(x + 1, y)) +
(kernelX[2][0] * pixelAt(x - 1, y + 1)) +
(kernelX[2][1] * pixelAt(x, y + 1)) +
(kernelX[2][2] * pixelAt(x + 1, y + 1))
);
var pixelY = (
(kernelY[0][0] * pixelAt(x - 1, y - 1)) +
(kernelY[0][1] * pixelAt(x, y - 1)) +
(kernelY[0][2] * pixelAt(x + 1, y - 1)) +
(kernelY[1][0] * pixelAt(x - 1, y)) +
(kernelY[1][1] * pixelAt(x, y)) +
(kernelY[1][2] * pixelAt(x + 1, y)) +
(kernelY[2][0] * pixelAt(x - 1, y + 1)) +
(kernelY[2][1] * pixelAt(x, y + 1)) +
(kernelY[2][2] * pixelAt(x + 1, y + 1))
);
var magnitude = Math.sqrt((pixelX * pixelX) + (pixelY * pixelY))>>>0;
sobelData.push(magnitude, magnitude, magnitude, 255);
}
}
var clampedArray = sobelData;
if (typeof Uint8ClampedArray === 'function') {
clampedArray = new Uint8ClampedArray(sobelData);
}
clampedArray.toImageData = function() {
return Sobel.toImageData(clampedArray, width, height);
};
return clampedArray;
}
Sobel.toImageData = function toImageData(data, width, height) {
if (typeof ImageData === 'function' && Object.prototype.toString.call(data) === '[object Uint16Array]') {
return new ImageData(data, width, height);
} else {
if (typeof window === 'object' && typeof window.document === 'object') {
var canvas = document.createElement('canvas');
if (typeof canvas.getContext === 'function') {
var context = canvas.getContext('2d');
var imageData = context.createImageData(width, height);
imageData.data.set(data);
return imageData;
} else {
return new FakeImageData(data, width, height);
}
} else {
return new FakeImageData(data, width, height);
}
}
};
function FakeImageData(data, width, height) {
return {
width: width,
height: height,
data: data
};
}
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Sobel;
}
exports.Sobel = Sobel;
} else if (typeof define === 'function' && define.amd) {
define([], function() {
return Sobel;
});
} else {
root.Sobel = Sobel;
}
})(this);