js_houghlines_HoughLinesP.html 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Image HoughLinesP Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Image HoughLinesP 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. let color = new cv.Scalar(255, 0, 0);
  47. cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
  48. cv.Canny(src, src, 50, 200, 3);
  49. // You can try more different parameters
  50. cv.HoughLinesP(src, lines, 1, Math.PI / 180, 2, 0, 0);
  51. // draw lines
  52. for (let i = 0; i < lines.rows; ++i) {
  53. let startPoint = new cv.Point(lines.data32S[i * 4], lines.data32S[i * 4 + 1]);
  54. let endPoint = new cv.Point(lines.data32S[i * 4 + 2], lines.data32S[i * 4 + 3]);
  55. cv.line(dst, startPoint, endPoint, color);
  56. }
  57. cv.imshow('canvasOutput', dst);
  58. src.delete(); dst.delete(); lines.delete();
  59. </script>
  60. <script type="text/javascript">
  61. let utils = new Utils('errorMessage');
  62. utils.loadCode('codeSnippet', 'codeEditor');
  63. utils.loadImageToCanvas('shape.jpg', 'canvasInput');
  64. utils.addFileInputHandler('fileInput', 'canvasInput');
  65. let tryIt = document.getElementById('tryIt');
  66. tryIt.addEventListener('click', () => {
  67. utils.executeCode('codeEditor');
  68. });
  69. utils.loadOpenCv(() => {
  70. tryIt.removeAttribute('disabled');
  71. });
  72. </script>
  73. </body>
  74. </html>