綠色背景圖像已更改并替換為使用綠色的任何效果或其他圖像
屏幕算法,也稱為色鍵算法。簡而言之,我們正在做的是
將前向圖像中的所有綠色像素與其在后向圖像中的匹配對應部分交換
背景圖片。
此外,我們需要記住,輸出圖像的大小應與輸出圖像的大小相匹配
向前圖像。在接下來的步驟中,將前向圖像中的像素復制到新圖像中
圖像。使用背景圖像的匹配像素,而不是復制綠色像素。
在應用以下內容之前,請不要錯過將以下源文件包含到您的 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 文檔的 標記中添加以下 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 在接收到兩個圖像作為輸入后將這兩個圖像組合起來;因此,落后的
圖像取代前向圖像的綠色背景。為了貫徹落實綠色
篩選算法,上面提供了代碼。
以上就是使用 JavaScript 實現綠屏算法的詳細內容,更多請關注www.92cms.cn其它相關文章!






