-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcdf.html
More file actions
153 lines (138 loc) · 3.28 KB
/
cdf.html
File metadata and controls
153 lines (138 loc) · 3.28 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="d3.js"></script>
<style>
body {
font: 10px sans-serif;
}
#main {
position: relative;
width: 50%;
margin: auto;
}
canvas {
padding: 0;
margin: 0;
}
svg {
position: absolute;
top: 0;
bottom: 0;
}
.histo {
fill-opacity: .4;
stroke-width: 2;
}
</style>
<title></title>
</head>
<body>
<div id="main"></div>
<script>
'use strict';
function load() {
var image = this,
width = image.width,
height = image.height,
data = d3.range(3).map(function() { return [] }),
rgb = d3.scale.ordinal().domain(d3.range(3)).range(["red", "green", "blue"]),
x = d3.scale.linear().domain([0, 255]).range([0, width]),
y = d3.scale.linear().range([0, height / 4]),
histos = ["red", "green", "blue"];
var svg, histo, pixels, canvas;
var empty = Array(256);
for (var i = 0; i < 256; i++) { empty[i] = 0; }
function histogram(p, i) {
var d = empty.slice();
for (; i < p.data.length; i += 4) {
d[p.data[i]]++;
}
return d;
}
function normalize(p, i) {
var H = histogram(p, i),
c = cdf(H, p.data.length / 4);
for (; i < p.data.length; i += 4) {
p.data[i] = (H.length - 1) * c[p.data[i]];
}
return p;
}
function cdf(histo, size) {
var cdf = [histo[0] / size];
for (var i = 1; i < histo.length; i++) {
cdf[i] = histo[i] / size + cdf[i - 1];
}
return cdf;
}
var area = d3.svg.area()
.x(function(d, i) { return x(i); })
.y0(height)
.y1(function(d) { return height - y(d); })
.interpolate("basis");
svg = d3.select("#main")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
d3.select("#main")
.append("canvas")
.attr("width", width)
.attr("height", height)
.each(function() {
var context = this.getContext('2d');
context.drawImage(image, 0, 0);
pixels = function() {
return context.getImageData(0, 0, width, height);
}
canvas = function(pixels) {
context.putImageData(pixels, 0, 0);
}
});
d3.select("#main").selectAll("input")
.data(histos).enter().append("input")
.attr("type", "checkbox")
.each(function(d, i) {
data[i] = histogram(pixels(), i);
})
.on("change", function(d, i) {
var h, n;
if (this.checked) {
n = normalize(pixels(), i);
h = histogram(n, i);
canvas(n);
} else {
h = histogram(pixels(), i);
}
data[i] = h;
redraw();
})
function draw() {
var max = Math.max(d3.max(data[0]), d3.max(data[1]), d3.max(data[2]));
y.domain([0, max]);
histo = svg.selectAll(".histo")
.data(data).enter().append("path")
.attr("class", "histo")
.attr("fill", function(d, i) { return rgb(i); })
.attr("stroke", function(d, i) { return rgb(i); })
.attr("d", area);
}
function redraw() {
var max = Math.max(d3.max(data[0]), d3.max(data[1]), d3.max(data[2]));
y.domain([0, max]);
histo.data(data)
.transition()
.duration(1000)
.attr("d", area);
}
draw();
}
document.addEventListener('DOMContentLoaded', function() {
var image = new Image();
image.src = 'l.jpg';
image.addEventListener("load", load);
});
</script>
</body>
</html>