使用 JavaScript 实现绿幕算法
绿幕图像会被更改并替换为任何特效或其他图像,方法是使用绿幕算法(也称为色键算法)。简而言之,我们所做的是将前景图像中的所有绿色像素替换为背景图像中与其对应的像素。
此外,我们需要注意输出图像的大小应与前景图像的大小相匹配。在接下来的步骤中,前景图像中的像素将被复制到新图像中。不用复制绿色像素,而是使用背景图像中对应的像素。
在应用以下代码之前,请务必将以下源文件包含到您的 HTML 代码中:
<script src=”https://www.dukelearntoprogram.com/course1/common/js/image/SimpleImage.js”></script>
实现此算法所需的 JavaScript 代码如下所示。要使用它,您必须自己创建 HTML 代码。
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()">
CSS 源代码
接下来,在 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>
JavaScript 源代码
您必须在 HTML 文档的 <script> 标签中添加以下 JavaScript 代码
<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 在接收两张输入图像后将它们组合起来;因此,背景图像取代了前景图像的绿色背景。为了实现绿幕算法,以上提供了代码。
广告