Download doc/js_tutorials/js_assets/js_contour_features_fitLine.html from tgollogly/test: direct link, hf CLI and curl.
- Browser
- Download file 2.86 kB
-
https://huggingface.co/tgollogly/test/resolve/main/doc/js_tutorials/js_assets/js_contour_features_fitLine.html
- Command line
-
hf download hf://tgollogly/test/doc/js_tutorials/js_assets/js_contour_features_fitLine.html
-
curl -L -o js_contour_features_fitLine.html https://huggingface.co/tgollogly/test/resolve/main/doc/js_tutorials/js_assets/js_contour_features_fitLine.html
2.86 kB
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Fit Line Example</title> | |
| <link href="js_example_style.css" rel="stylesheet" type="text/css" /> | |
| </head> | |
| <body> | |
| <h2>Fit Line Example</h2> | |
| <p> | |
| <canvas> elements named <b>canvasInput</b> and <b>canvasOutput</b> have been prepared.<br> | |
| Click <b>Try it</b> button to see the result. You can choose another image.<br> | |
| You can change the code in the <textarea> to investigate more. | |
| </p> | |
| <div> | |
| <div class="control"><button id="tryIt" disabled>Try it</button></div> | |
| <textarea class="code" rows="9" cols="100" id="codeEditor" spellcheck="false"> | |
| </textarea> | |
| <p class="err" id="errorMessage"></p> | |
| </div> | |
| <div> | |
| <table cellpadding="0" cellspacing="0" width="0" border="0"> | |
| <tr> | |
| <td> | |
| <canvas id="canvasInput"></canvas> | |
| </td> | |
| <td> | |
| <canvas id="canvasOutput"></canvas> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| <div class="caption">canvasInput <input type="file" id="fileInput" name="file" accept="image/*" /></div> | |
| </td> | |
| <td> | |
| <div class="caption">canvasOutput</div> | |
| </td> | |
| </tr> | |
| </table> | |
| </div> | |
| <script src="utils.js" type="text/javascript"></script> | |
| <script id="codeSnippet" type="text/code-snippet"> | |
| let src = cv.imread('canvasInput'); | |
| let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3); | |
| cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0); | |
| cv.threshold(src, src, 177, 200, cv.THRESH_BINARY); | |
| let contours = new cv.MatVector(); | |
| let hierarchy = new cv.Mat(); | |
| let line = new cv.Mat(); | |
| cv.findContours(src, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE); | |
| let cnt = contours.get(0); | |
| // You can try more different parameters | |
| cv.fitLine(cnt, line, cv.DIST_L2, 0, 0.01, 0.01); | |
| let contoursColor = new cv.Scalar(255, 255, 255); | |
| let lineColor = new cv.Scalar(255, 0, 0); | |
| cv.drawContours(dst, contours, 0, contoursColor, 1, 8, hierarchy, 100); | |
| let vx = line.data32F[0]; | |
| let vy = line.data32F[1]; | |
| let x = line.data32F[2]; | |
| let y = line.data32F[3]; | |
| let lefty = Math.round((-x * vy / vx) + y); | |
| let righty = Math.round(((src.cols - x) * vy / vx) + y); | |
| let point1 = new cv.Point(src.cols - 1, righty); | |
| let point2 = new cv.Point(0, lefty); | |
| cv.line(dst, point1, point2, lineColor, 2, cv.LINE_AA, 0); | |
| cv.imshow('canvasOutput', dst); | |
| src.delete(); dst.delete(); contours.delete(); hierarchy.delete(); line.delete(); cnt.delete(); | |
| </script> | |
| <script type="text/javascript"> | |
| let utils = new Utils('errorMessage'); | |
| utils.loadCode('codeSnippet', 'codeEditor'); | |
| utils.loadImageToCanvas('shape.jpg', 'canvasInput'); | |
| utils.addFileInputHandler('fileInput', 'canvasInput'); | |
| let tryIt = document.getElementById('tryIt'); | |
| tryIt.addEventListener('click', () => { | |
| utils.executeCode('codeEditor'); | |
| }); | |
| utils.loadOpenCv(() => { | |
| tryIt.removeAttribute('disabled'); | |
| }); | |
| </script> | |
| </body> | |
| </html> | |