Teachers open the door but You must enter by yourself.

Open Media Lab.
オープンメディアラボ

2階調化
Binarization

ランダムディザ法による変換式

if ( Lij > (0~255の乱数) ) Lij’= 255(白)
else Lij’= 0(黒)

単純2値化による変換式

if ( Lij > 127 ) Lij’= 255(白)
else Lij’= 0(黒)

ソースコード

window.addEventListener('load', function(){
	const canvas=Array(3);
	const context=Array(3);
	for(let c=0;c < 3; c++){
		canvas[c]=document.getElementById('canvas'+c.toString());
		canvas[c].height = canvas[c].width*1.333;
		context[c]= canvas[c].getContext('2d');
	}
	const image = new Image();
	image.src = 'ip/original.jpg';
	image.addEventListener("load", function(){
		for(let c=0;c < 3; c++){
			context[c].drawImage(image,0,0);
			const x = context[c].getImageData(0,0,
					canvas[c].width,canvas[c].height);
			for (let i=0; i < x.data.length; i+=4) {
				const gray=x.data[i]*0.299
					+x.data[i+1]*0.587+x.data[i+2]*0.114;
				if(c==0) x.data[i]=x.data[i+1]=x.data[i+2]=gray;
				else if(c==1) x.data[i]=x.data[i+1]=x.data[i+2]
					=gray>Math.random()*255?255:0;
				else x.data[i]=x.data[i+1]=x.data[i+2]=gray>127?255:0;
			}
			context[c].putImageData(x,0,0);
		}
	});
});
This site is powered by
Powered by MathJax