綠色背景影像已變更並替換為使用綠色的任何效果或其他影像 螢幕演算法,也稱為色鍵演算法。簡而言之,我們正在做的是 將前向影像中的所有綠色像素與其在後向影像中的匹配對應部分交換 背景圖片。
此外,我們需要記住,輸出圖像的大小應與輸出圖像的大小相匹配 向前圖像。在接下來的步驟中,將前向影像中的像素複製到新影像中 圖像。使用背景影像的匹配像素,而不是複製綠色像素。
在應用以下內容之前,請不要錯過將以下原始檔案包含到您的 HTML 程式碼中 程式碼 -
<script src=”https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js”></script>
下面提供了實作該演算法所需的 JavaScript 程式碼。要使用它,您必須創建 自己編寫 HTML 程式碼。
您必須將此 HTML 程式碼新增至 HTML 文件的元素中。
<h1>Green Screen Algorithm Implementation using JavaScript with TutorialsPoint </h1> <canvas id="image1"></canvas> <canvas id="image2"></canvas> <br /> <p> First Image: <input type="file" id="myImageFile" multiple="false" onChange="frontimg()"> </p> <p> Background Image: <input type="file" id="bgImageFile" multiple="false" onChange="backimg()"> </p> <input type="button" value="Merge Image" onClick="merge()">
接下來,HTML文檔中的CSS程式碼
<style> canvas { background: rgb(214, 235, 176); border: 1px solid rgb(13, 109, 160); width: 420px; height: 290px; margin: 30px; } h1{ color: rgb(13, 109, 160); } body { background-color: #bbb6ab; } </style>
您必須在 HTML 文件的 <script> 標籤中新增以下 JavaScript 程式碼</script>
<script type="text/javascript"> let forwdImage = null; let secImage = null; // This function accepts an input of a forward picture function frontimg() { let fileInput = document.getElementById("myImageFile"); let canvas = document.getElementById("image1"); forwdImage = new SimpleImage(fileInput); forwdImage.drawTo(canvas); } // Background picture is output from this function function backimg() { let fileInput = document.getElementById("bgImageFile"); let canvas = document.getElementById("image2"); secImage = new SimpleImage(fileInput); secImage.drawTo(canvas); } // This function combines the two images and outputs the // merged image as the final result. The Green Screen // Algorithm is implemented function merge() { clear(); let image1 = document.getElementById("image1"); let outputImage = new SimpleImage( forwdImage.width, forwdImage.height); for (let pixel of forwdImage.values()) { if (pixel.getGreen() > pixel.getRed() + pixel.getBlue()) { let x = pixel.getX(); let y = pixel.getY(); let newPixel = secImage.getPixel(x, y); outputImage.setPixel(x, y, newPixel); } else { outputImage.setPixel(pixel.getX(), pixel.getY(), pixel); } } outputImage.drawTo(image1); } // The output and input from earlier // fetches are cleared by this function. function clear() { let image1 = document.getElementById("image1"); let image2 = document.getElementById("image2"); let context = image1.getContext("2d"); context.clearRect(0, 0, image1.width, image1.height); context = image2.getContext("2d"); context.clearRect(0, 0, image2.width, image2.height); } </script>
現在讓我們檢查以下程式碼中的完整程式碼和輸出。
<!DOCTYPE html> <html> <title>Implement Green Screen Algorithm using JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js"></script> <style> canvas { background: rgb(214, 235, 176); border: 1px solid rgb(13, 109, 160); width: 420px; height: 290px; margin: 30px; } h1 { color: rgb(13, 109, 160); } body { background-color: #bbb6ab; } </style> </head> <body> <h1>Green Screen Algorithm Implementation using JavaScript with TutorialsPoint </h1> <canvas id="image1"></canvas> <canvas id="image2"></canvas> <br /> <p> First Image: <input type="file" id="myImageFile" multiple="false" onChange="frontimg()"> </p> <p> Background Image: <input type="file" id="bgImageFile" multiple="false" onChange="backimg()"> </p> <input type="button" value="Merge Image" onClick="merge()"> <script type="text/javascript"> let forwdImage = null; let secImage = null; // This function accepts an input of a forward picture function frontimg() { let fileInput = document.getElementById("myImageFile"); let canvas = document.getElementById("image1"); forwdImage = new SimpleImage(fileInput); forwdImage.drawTo(canvas); } // Background picture is output from this function function backimg() { let fileInput = document.getElementById("bgImageFile"); let canvas = document.getElementById("image2"); secImage = new SimpleImage(fileInput); secImage.drawTo(canvas); } // This function combines the two images and outputs the // merged image as the final result. The Green Screen // Algorithm is implemented function merge() { clear(); let image1 = document.getElementById("image1"); let outputImage = new SimpleImage( forwdImage.width, forwdImage.height); for (let pixel of forwdImage.values()) { if (pixel.getGreen() > pixel.getRed() + pixel.getBlue()) { let x = pixel.getX(); let y = pixel.getY(); let newPixel = secImage.getPixel(x, y); outputImage.setPixel(x, y, newPixel); } else { outputImage.setPixel(pixel.getX(), pixel.getY(), pixel); } } outputImage.drawTo(image1); } // The output and input from earlier // fetches are cleared by this function. function clear() { let image1 = document.getElementById("image1"); let image2 = document.getElementById("image2"); let context = image1.getContext("2d"); context.clearRect(0, 0, image1.width, image1.height); context = image2.getContext("2d"); context.clearRect(0, 0, image2.width, image2.height); } </script> </body> </html>
您將看到此輸出螢幕,而無需添加任何圖像。
接下來,新增「第一個影像」和「背景影像」影像後,您將看到此輸出畫面。
現在您將看到單擊“合併圖像”按鈕後的最終輸出。兩張圖片都是 組合如下圖所示。
兩張圖片作為此演算法的輸入。第一個是背景為的第一張圖像 綠色,第二個是應該用來代替綠色的背景圖像 背景。
JavaScript 在接收兩個影像作為輸入後將這兩個影像組合起來;因此,落後的 影像取代前向影像的綠色背景。為了貫徹綠色 篩選演算法,上面提供了程式碼。
以上是使用 JavaScript 實作綠屏演算法的詳細內容。更多資訊請關注PHP中文網其他相關文章!