brief.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>tracking.js - feature matching</title>
  6. <link rel="stylesheet" href="assets/demo.css">
  7. <script src="../build/tracking-min.js"></script>
  8. <script src="../node_modules/dat.gui/build/dat.gui.min.js"></script>
  9. <style>
  10. .demo-container {
  11. background-color: black;
  12. }
  13. #image1, #image2 {
  14. position: absolute;
  15. left: -1000px;
  16. top: -1000px;
  17. }
  18. #canvas {
  19. position: absolute;
  20. left: 50%;
  21. top: 50%;
  22. margin-left: -393px;
  23. margin-top: -147px;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="demo-title">
  29. <p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - match similar feature points in two images</p>
  30. </div>
  31. <div class="demo-frame">
  32. <div class="demo-container">
  33. <img id="image1" src="assets/brief1.png" />
  34. <img id="image2" src="assets/brief2.png" />
  35. <canvas id="canvas" width="786" height="295"></canvas>
  36. </div>
  37. </div>
  38. <script>
  39. window.onload = function() {
  40. var width = 393;
  41. var height = 295;
  42. var canvas = document.getElementById('canvas');
  43. var context = canvas.getContext('2d');
  44. var image1 = document.getElementById('image1');
  45. var image2 = document.getElementById('image2');
  46. window.descriptorLength = 256;
  47. window.matchesShown = 30;
  48. window.blurRadius = 3;
  49. var doMatch = function() {
  50. tracking.Brief.N = window.descriptorLength;
  51. context.drawImage(image1, 0, 0, width, height);
  52. context.drawImage(image2, width, 0, width, height);
  53. var imageData1 = context.getImageData(0, 0, width, height);
  54. var imageData2 = context.getImageData(width, 0, width, height);
  55. var gray1 = tracking.Image.grayscale(tracking.Image.blur(imageData1.data, width, height, blurRadius), width, height);
  56. var gray2 = tracking.Image.grayscale(tracking.Image.blur(imageData2.data, width, height, blurRadius), width, height);
  57. var corners1 = tracking.Fast.findCorners(gray1, width, height);
  58. var corners2 = tracking.Fast.findCorners(gray2, width, height);
  59. var descriptors1 = tracking.Brief.getDescriptors(gray1, width, corners1);
  60. var descriptors2 = tracking.Brief.getDescriptors(gray2, width, corners2);
  61. var matches = tracking.Brief.reciprocalMatch(corners1, descriptors1, corners2, descriptors2);
  62. matches.sort(function(a, b) {
  63. return b.confidence - a.confidence;
  64. });
  65. for (var i = 0; i < Math.min(window.matchesShown, matches.length); i++) {
  66. var color = '#' + Math.floor(Math.random()*16777215).toString(16);
  67. context.fillStyle = color;
  68. context.strokeStyle = color;
  69. context.fillRect(matches[i].keypoint1[0], matches[i].keypoint1[1], 4, 4);
  70. context.fillRect(matches[i].keypoint2[0] + width, matches[i].keypoint2[1], 4, 4);
  71. context.beginPath();
  72. context.moveTo(matches[i].keypoint1[0], matches[i].keypoint1[1]);
  73. context.lineTo(matches[i].keypoint2[0] + width, matches[i].keypoint2[1]);
  74. context.stroke();
  75. }
  76. };
  77. doMatch();
  78. var gui = new dat.GUI();
  79. gui.add(window, 'descriptorLength', 128, 512).step(32).onChange(doMatch);
  80. gui.add(window, 'matchesShown', 1, 100).onChange(doMatch);
  81. gui.add(window, 'blurRadius', 1.1, 5).onChange(doMatch);
  82. }
  83. </script>
  84. </body>
  85. </html>