JavaScript实现256色转灰度图
一幅完整的图像,是由红色、绿色、蓝色三个通道组成的。红色、绿色、蓝色三个通道的缩览图都是以灰度显示的。用不同的灰度色阶来表示" 红,绿,蓝"在图像中的比重。通道中的纯白,代表了该色光在此处为最高亮度,亮度级别是255。
灰度化方法:
1.浮点算法:Gray = R*0.299 + G*0.587 + B*0.114
2.整数方法:Gray = (R*299 + G*587 + B*114 + 500) / 1000
3.移位方法:Gray =(R*28+G*151+B*77)>>8;
4.平均值法:Gray=(R+G+B)/3;
5.仅取绿色:Gray=G;
只要将原来的RGB(R,G,B)中的R,G,B统一用Gray替换,形成新的RGB(Gray,Gray,Gray)就完成灰度化了;
效果图:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ImgBase</title>
<style type="text/css">
.scream{
width:400px;
height:300px;
position: absolute;
top:60px;
border: 1px solid;
}
#canvas{
position: absolute;
top:60px;
left:500px;
border: 1px dashed;
}
</style>
</head>
<body>
<input type="file" onchange="loadImg()"/>
<input type="button" value="灰度化" onclick="Convert256toGray()"/>
<br/><br/>
<div class="scream">
<img id="scream" width="400px" height="300px" alt="Image preview...">
</div>
<canvas id="canvas" width="400px;" height="300px;">
your browser does not support canvas!
</canvas>
<script>
function Convert256toGray(){
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var imgData = ctx.getImageData(0,0,c_w,c_h);
for (var i=0;i<imgData.data.length;i+=4)
{
var R = imgData.data[i]; //R(0-255)
var G = imgData.data[i+1]; //G(0-255)
var B = imgData.data[i+2]; //G(0-255)
var Alpha = imgData.data[i+3]; //Alpha(0-255)
//浮点算法
var gray = R*0.299 + G*0.587 + B*0.114;
//整数算法
// var gray = (R*299 + G*587 + B*114 + 500) / 1000;
//移位算法
// var gray =(R*76+G*151+B*28)>>8;
//平均值算法
// var gray = (R+G+B)/3;
//仅取绿色
// var gray=G;
imgData.data[i] = gray;
imgData.data[i+1] = gray;
imgData.data[i+2] = gray;
imgData.data[i+3] = Alpha;
}
ctx.putImageData(imgData,0,0);
}
</script>
<!--base-->
<script>
//canvas图像的宽高
var c_w = 400; var c_h = 300;
//加载img图像
function loadImg(){
var img = document.getElementById("scream");
var file = document.querySelector('input[type=file]').files[0];
if(!/image\/\w+/.test(file.type)){
alert("文件必须为图片!");
return false;
}
var reader = new FileReader();
reader.addEventListener("load", function () {
img.src = reader.result;
}, false);
if(file) {
reader.readAsDataURL(file);
loadCanvas();
}
}
//加载canvas图像
function loadCanvas(){
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var img = document.getElementById("scream");
img.onload = function() {
ctx.drawImage(img,0,0,c_w,c_h);
}
}
</script>
</body>
</html>
以上是 JavaScript实现256色转灰度图 的全部内容, 来源链接: utcz.com/z/329642.html