diff --git a/samples/css/base.css b/samples/css/base.css index ddeb6a4..d267036 100644 --- a/samples/css/base.css +++ b/samples/css/base.css @@ -21,7 +21,8 @@ body { .stats-settings, .threads-control, .jitter-limit, .edge-error, .downscale-level, .threshold-block-size, .threshold-offset, .demo-state, .lower-hue-control, .upper-hue-control, .lower-saturation-control, -.upper-saturation-control, .lower-value-control, .upper-value-control { +.upper-saturation-control, .lower-value-control, .upper-value-control, +.background-control { padding: 10px; width: 100%; border-bottom: dotted silver 1px; diff --git a/samples/invisibilityCloak/README.md b/samples/invisibilityCloak/README.md index df09e4c..9865fa4 100644 --- a/samples/invisibilityCloak/README.md +++ b/samples/invisibilityCloak/README.md @@ -10,7 +10,7 @@ Demonstrate color detection and segmentation using OpenCV.js. Camera parameters are not stable when the camera is just getting started. So we execute a loop where we capture background and use the last frame as a stable background. ```javascript -const BACKGROUND_CAPTURE_ITERATIONS = 300; +const BACKGROUND_CAPTURE_ITERATIONS = 50; for (let i = 0; i < BACKGROUND_CAPTURE_ITERATIONS; ++i) { ... @@ -22,7 +22,7 @@ for (let i = 0; i < BACKGROUND_CAPTURE_ITERATIONS; ++i) { 2. Create HSV ranges for blue color In OpenCV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. -We use 0-33 range for blue color, 100-255 for saturation and 70-255 for brigtness. +We apply range values from sliders but let's see example for blue color. ```javascript lowerRedRange = new cv.Mat(video.height, video.width, cv.CV_8UC3, new cv.Scalar(0, 100, 70, 255)); diff --git a/samples/invisibilityCloak/index.html b/samples/invisibilityCloak/index.html index 1f4e78b..8890a54 100644 --- a/samples/invisibilityCloak/index.html +++ b/samples/invisibilityCloak/index.html @@ -40,7 +40,7 @@
- +
@@ -53,35 +53,39 @@
+
+ +
+
- +
- +
- +
- +
- +
- +
diff --git a/samples/invisibilityCloak/index.js b/samples/invisibilityCloak/index.js index 3dba0eb..5743c58 100644 --- a/samples/invisibilityCloak/index.js +++ b/samples/invisibilityCloak/index.js @@ -17,7 +17,7 @@ let dst = null; // Camera parameters are not stable when the camera is just getting started. // So we execute a loop where we capture background and use the last frame // as a stable background. -const BACKGROUND_CAPTURE_ITERATIONS = 300; +const BACKGROUND_CAPTURE_ITERATIONS = 30; // In OpenCV, Hue range is [0,179], Saturation range is [0,255] // and Value range is [0,255]. We use ~0-33 range for blue color as default. @@ -81,18 +81,29 @@ function completeStyling() { canvasOutput.height = video.height; } -function captureBackground() { - for (let i = 0; i < BACKGROUND_CAPTURE_ITERATIONS; ++i) { +let backgroundCaptureCounter = 0; +function runCapturingRound() { + if (backgroundCaptureCounter < BACKGROUND_CAPTURE_ITERATIONS) { + ++backgroundCaptureCounter; + canvasInputCtx.drawImage(video, 0, 0, video.width, video.height); let imageData = canvasInputCtx.getImageData(0, 0, video.width, video.height); background.data.set(imageData.data); cv.imshow(canvasOutput, background); + + requestAnimationFrame(runCapturingRound); + return; } - document.getElementById("demoState").innerText = "Ready for magic! Put on your blue cloak!" + backgroundCaptureCounter = 0; + document.getElementById("demoState").innerText = "Ready for magic! You may adjust color range below."; requestAnimationFrame(processVideo); } +function captureBackground() { + document.getElementById("demoState").innerText = "Capturing background..."; + requestAnimationFrame(runCapturingRound); +} -function removeBlueColor(source, destination) { +function applyColorSegmentation(source, destination) { let hsv = new cv.Mat(video.height, video.width, cv.CV_8UC3); let mask = new cv.Mat(); let maskInv = new cv.Mat(); @@ -137,7 +148,7 @@ function processVideo() { let imageData = canvasInputCtx.getImageData(0, 0, video.width, video.height); src.data.set(imageData.data); - removeBlueColor(src, dst); + applyColorSegmentation(src, dst); cv.imshow(canvasOutput, dst); @@ -166,7 +177,13 @@ function onVideoStartedCustom() { document.getElementById('mainContent').classList.remove('hidden'); completeStyling(); initOpencvObjects(); - captureBackground(); + requestAnimationFrame(captureBackground); +} + +function startVideoProcessingCustom() { + videoTrack = video.srcObject.getVideoTracks()[0]; + imageCapturer = new ImageCapture(videoTrack); + requestAnimationFrame(captureBackground); } function cleanupAndStop() {