js_grabcut_grabCut.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Image GrabCut Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Image GrabCut 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. cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
  45. let mask = new cv.Mat();
  46. let bgdModel = new cv.Mat();
  47. let fgdModel = new cv.Mat();
  48. let rect = new cv.Rect(50, 50, 260, 280);
  49. cv.grabCut(src, mask, rect, bgdModel, fgdModel, 1, cv.GC_INIT_WITH_RECT);
  50. // draw foreground
  51. for (let i = 0; i < src.rows; i++) {
  52. for (let j = 0; j < src.cols; j++) {
  53. if (mask.ucharPtr(i, j)[0] == 0 || mask.ucharPtr(i, j)[0] == 2) {
  54. src.ucharPtr(i, j)[0] = 0;
  55. src.ucharPtr(i, j)[1] = 0;
  56. src.ucharPtr(i, j)[2] = 0;
  57. }
  58. }
  59. }
  60. // draw grab rect
  61. let color = new cv.Scalar(0, 0, 255);
  62. let point1 = new cv.Point(rect.x, rect.y);
  63. let point2 = new cv.Point(rect.x + rect.width, rect.y + rect.height);
  64. cv.rectangle(src, point1, point2, color);
  65. cv.imshow('canvasOutput', src);
  66. src.delete(); mask.delete(); bgdModel.delete(); fgdModel.delete();
  67. </script>
  68. <script type="text/javascript">
  69. let utils = new Utils('errorMessage');
  70. utils.loadCode('codeSnippet', 'codeEditor');
  71. utils.loadImageToCanvas('lena.jpg', 'canvasInput');
  72. utils.addFileInputHandler('fileInput', 'canvasInput');
  73. let tryIt = document.getElementById('tryIt');
  74. tryIt.addEventListener('click', () => {
  75. utils.executeCode('codeEditor');
  76. });
  77. utils.loadOpenCv(() => {
  78. tryIt.removeAttribute('disabled');
  79. });
  80. </script>
  81. </body>
  82. </html>