js_watershed_watershed.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Image Watershed Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Image Watershed 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 = new cv.Mat();
  45. let gray = new cv.Mat();
  46. let opening = new cv.Mat();
  47. let coinsBg = new cv.Mat();
  48. let coinsFg = new cv.Mat();
  49. let distTrans = new cv.Mat();
  50. let unknown = new cv.Mat();
  51. let markers = new cv.Mat();
  52. // gray and threshold image
  53. cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
  54. cv.threshold(gray, gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU);
  55. // get background
  56. let M = cv.Mat.ones(3, 3, cv.CV_8U);
  57. cv.erode(gray, gray, M);
  58. cv.dilate(gray, opening, M);
  59. cv.dilate(opening, coinsBg, M, new cv.Point(-1, -1), 3);
  60. // distance transform
  61. cv.distanceTransform(opening, distTrans, cv.DIST_L2, 5);
  62. cv.normalize(distTrans, distTrans, 1, 0, cv.NORM_INF);
  63. // get foreground
  64. cv.threshold(distTrans, coinsFg, 0.7 * 1, 255, cv.THRESH_BINARY);
  65. coinsFg.convertTo(coinsFg, cv.CV_8U, 1, 0);
  66. cv.subtract(coinsBg, coinsFg, unknown);
  67. // get connected components markers
  68. cv.connectedComponents(coinsFg, markers);
  69. for (let i = 0; i < markers.rows; i++) {
  70. for (let j = 0; j < markers.cols; j++) {
  71. markers.intPtr(i, j)[0] = markers.ucharPtr(i, j)[0] + 1;
  72. if (unknown.ucharPtr(i, j)[0] == 255) {
  73. markers.intPtr(i, j)[0] = 0;
  74. }
  75. }
  76. }
  77. cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0);
  78. cv.watershed(src, markers);
  79. // draw barriers
  80. for (let i = 0; i < markers.rows; i++) {
  81. for (let j = 0; j < markers.cols; j++) {
  82. if (markers.intPtr(i, j)[0] == -1) {
  83. src.ucharPtr(i, j)[0] = 255; // R
  84. src.ucharPtr(i, j)[1] = 0; // G
  85. src.ucharPtr(i, j)[2] = 0; // B
  86. }
  87. }
  88. }
  89. cv.imshow('canvasOutput', src);
  90. src.delete(); dst.delete(); gray.delete(); opening.delete(); coinsBg.delete();
  91. coinsFg.delete(); distTrans.delete(); unknown.delete(); markers.delete(); M.delete();
  92. </script>
  93. <script type="text/javascript">
  94. let utils = new Utils('errorMessage');
  95. utils.loadCode('codeSnippet', 'codeEditor');
  96. utils.loadImageToCanvas('coins.jpg', 'canvasInput');
  97. utils.addFileInputHandler('fileInput', 'canvasInput');
  98. let tryIt = document.getElementById('tryIt');
  99. tryIt.addEventListener('click', () => {
  100. utils.executeCode('codeEditor');
  101. });
  102. utils.loadOpenCv(() => {
  103. tryIt.removeAttribute('disabled');
  104. });
  105. </script>
  106. </body>
  107. </html>