-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
199 lines (162 loc) · 6.86 KB
/
demo.html
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Image Similarity Test</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/smoothness/jquery-ui.css" type="text/css" rel="stylesheet" />
<style type="text/css">
div#controls {
border: 1px solid black;
margin-bottom: 10px;
margin-top: 10px;
padding: 10px;
}
div#results div.matches img {
width: 100px;
height: 100px;
}
div#threshold {
margin-left: 20px;
width: 200px;
margin-bottom: 20px;
}
div#catalog img {
width: 400px;
height: 400px;
}
.clear { clear: both; }
</style>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script src="js/jquery.imgsimilarity.js"></script>
<div id="controls">
<div>
<p>This is the demo for the <a
href="http://ryanwmoore.github.com/jquery-imgsimilarity/">jQuery
imgSimilarity plugin</a>. It requires a browser that supports the
canvas element. Please note that the images used in this demo are
rather large and so, the page may initially load slowly. If you
choose to use this library, I would recommend using smaller
images.</p>
<p>Use the slider below to set the amount of required similarity.
Images which are not similar enough will not be shown. For this
plugin, similarity means similar colors. Shape similarities and
texture similarities are not considered.</p>
<p>Similarity Threshold:
<span id="thresholdvalue">??</span>/<span id="divisor">??</span>.
</p>
<div id="threshold"></div>
<div id="recompute">Recompute Matches</div>
</div>
<div>
<div id="results"></div>
</div>
</div>
<div>
<p>The following images are the base images.</p>
<div id="catalog"></div>
</div>
<p>Photos taken from <a href="http://www.pdphoto.org">PD Photo</a>. All
images are in the public domain.</p>
<script>
var baseDirectory = "demo/";
var catalogImages = ["bridge1.jpg", "bridge2.jpg", "car.jpg", "cheese1.jpg",
"cheese2.jpg", "chili.jpg", "mountain.jpg"];
$(document).ready(function()
{
$("#divisor").text(100);
$("#recompute").button().click(function()
{
recomputeMatches();
});
var slider = $("#threshold").slider({
max: 500,
slide: function(event, ui){
$("#thresholdvalue").text(ui.value);
recomputeMatches();
}
});
$("#thresholdvalue").text($("#threshold").slider("value"));
var catalog = $("#catalog");
var results = $("#results");
$.each(catalogImages, function(index, value) {
//Build up images and have them automatically calculate their
//similarity information.
var image = comparableImage(baseDirectory + value, value);
catalog.append(image);
//jQuery UI Accordion to show results
results.append($("<h3/>").text(value + "'s similar images"));
matches = $("<div/>").addClass("matches").attr("id", "match"+index);
results.append(matches);
});
results.accordion({ autoHeight: false, clearStyle: true});
});
function comparableImage(url, title)
{
//Given a URL return a created img element. The image will
//automatically calculate its similarity histogram once its data is
//loaded. Then, matches will be recomputed because there is a new
//loaded image.
var element = $("<img/>");
element.attr("alt", title);
element.attr("title", title);
element.load(function() {
//When the image loads, automatically calculate image
//comparison information.
$(this).imgSimilarity();
recomputeMatches();
});
element.attr("src", url);
return element;
}
function recomputeMatches()
{
var threshold = parseInt($("#thresholdvalue").text())/parseInt($("#divisor").text());
var allData = $("#catalog img").map(function(index, element) {
return $(element);
});
allData = $.grep(allData, function(element)
{
//Has loaded histogram information.
return typeof (element.data('imgSimilarity')) !== "undefined";
});
//algorithm could be slightly improved by not reexamining pairs.
for (var i = 0; i < allData.length; i++)
{
var matches = new Array();
var matchData = allData[i].data('imgSimilarity').findClosest(allData);
//Find appropriate div to list our matches.
var container = $("#match" + i);
//Remove existing results, we're building up new results next.
container.empty();
for (var j = 0; j < matchData.length; j++)
{
//Close enough!
if (matchData[j] <= threshold)
{
var matchImg = $("<img/>");
matchImg.attr('src', allData[j].attr('src'));
matches.push({ amount: matchData[j],
img: matchImg,
data: allData[j].data('imgSimilarity')
});
}
//Sort by closest matches first.
matches.sort(function(a,b) { return a.amount - b.amount; });
$.each(matches, function(index, element) {
//Show each match.
container.append(element.img);
var similarityInfo = "Similarity: " + element.amount.toFixed(4);
element.img.attr("title", similarityInfo);
});
}
}
}
</script>
</body>
</html>