Contour Detection in Image
importScripts('https://docs.opencv.org/4.x/opencv.js'); self.onmessage = function(e) { const { imgData, minLength, maxLength } = e.data; const src = cv.matFromArray(imgData.height, imgData.width, cv.CV_8UC4, imgData.data); const gray = new cv.Mat(); cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0); const edged = new cv.Mat(); cv.Canny(gray, edged, 50, 150, 3, false); const contours = new cv.MatVector(); const hierarchy = new cv.Mat(); cv.findContours(edged, contours, hierarchy, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE); const validContours = []; for (let i = 0; i < contours.size(); ++i) { const contour = contours.get(i); const length = cv.arcLength(contour, true); if (length >= minLength && length <= maxLength) { validContours.push(contour); } } const processedImage = new ImageData(new Uint8ClampedArray(src.data), src.cols, src.rows); self.postMessage({ processedImage, validContours }); src.delete(); gray.delete(); edged.delete(); contours.delete(); hierarchy.delete(); };
0 komentar:
Posting Komentar