js_houghlines_HoughLines.html 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Hough Lines Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Hough Lines Example</h2>
  10. <p>
  11. &lt;canvas&gt; elements named <b>canvasInput</b> and <b>canvasOutput</b> have been prepared.<br>
  12. Click <b>Try it</b> button to see the result. You can choose another image.<br>
  13. You can change the code in the &lt;textarea&gt; to investigate more.
  14. </p>
  15. <div>
  16. <div class="control"><button id="tryIt" disabled>Try it</button></div>
  17. <textarea class="code" rows="9" cols="100" id="codeEditor" spellcheck="false">
  18. </textarea>
  19. <p class="err" id="errorMessage"></p>
  20. </div>
  21. <div>
  22. <table cellpadding="0" cellspacing="0" width="0" border="0">
  23. <tr>
  24. <td>
  25. <canvas id="canvasInput"></canvas>
  26. </td>
  27. <td>
  28. <canvas id="canvasOutput"></canvas>
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <div class="caption">canvasInput <input type="file" id="fileInput" name="file" accept="image/*" /></div>
  34. </td>
  35. <td>
  36. <div class="caption">canvasOutput</div>
  37. </td>
  38. </tr>
  39. </table>
  40. </div>
  41. <script src="utils.js" type="text/javascript"></script>
  42. <script id="codeSnippet" type="text/code-snippet">
  43. let src = cv.imread('canvasInput');
  44. let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
  45. let lines = new cv.Mat();
  46. cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
  47. cv.Canny(src, src, 50, 200, 3);
  48. // You can try more different parameters
  49. cv.HoughLines(src, lines, 1, Math.PI / 180,
  50. 30, 0, 0, 0, Math.PI);
  51. // draw lines
  52. for (let i = 0; i < lines.rows; ++i) {
  53. let rho = lines.data32F[i * 2];
  54. let theta = lines.data32F[i * 2 + 1];
  55. let a = Math.cos(theta);
  56. let b = Math.sin(theta);
  57. let x0 = a * rho;
  58. let y0 = b * rho;
  59. let startPoint = {x: x0 - 1000 * b, y: y0 + 1000 * a};
  60. let endPoint = {x: x0 + 1000 * b, y: y0 - 1000 * a};
  61. cv.line(dst, startPoint, endPoint, [255, 0, 0, 255]);
  62. }
  63. cv.imshow('canvasOutput', dst);
  64. src.delete(); dst.delete(); lines.delete();
  65. </script>
  66. <script type="text/javascript">
  67. let utils = new Utils('errorMessage');
  68. utils.loadCode('codeSnippet', 'codeEditor');
  69. utils.loadImageToCanvas('shape.jpg', 'canvasInput');
  70. utils.addFileInputHandler('fileInput', 'canvasInput');
  71. let tryIt = document.getElementById('tryIt');
  72. tryIt.addEventListener('click', () => {
  73. utils.executeCode('codeEditor');
  74. });
  75. utils.loadOpenCv(() => {
  76. tryIt.removeAttribute('disabled');
  77. });
  78. </script>
  79. </body>
  80. </html>