f8g

自動コントラスト

画像濃度変換処理
濃度の最小値が0, 最大値が255になるようにする濃度変換。与える関数は、

var f = function(x){
	var a = 255 / (max - min);
	var b = - a * min;
	return a * x + b;
};

一般的な画像では最大値≒255、最小値≒0ということが多いので、見た目で変わってくることはほとんどない、と思う。明るさの調節と一緒に使うと効果が分かりやすい。
http://gyazo.com/80bb62e01db51c19c82100ea02265a5d.png
これ使用

ImageProcessing.Color.prototype.intensity = function(v){
	return new Color(this.r + v, this.g + v, this.b + v);
};

ImageProcessing.prototype.autoContrast = function(){
	var self = this;

	var _f = function(max, min){
		if(max >= 255 && min <= 0)
			return function(x){ return x; };

		var a = 255 / (max - min);
		var b = - a * min;

		return function(x){
			return a * x + b;
		};
	};

	var max = this.max();
	var min = this.min();

	if(max.toString() == (new ImageProcessing.Color(255, 255, 255)).toString() &&
	   min.toString() == (new ImageProcessing.Color(  0,   0,   0)).toString())
		return this;

	var f = {
		r: _f(max.r, min.r),
		g: _f(max.g, min.g),
		b: _f(max.b, min.b)
	};

	this.each(function(px, x, y){
		self.setPixel(x, y, new ImageProcessing.Color(f.r(px.r), f.g(px.g), f.b(px.b)));
	});

	return this;
};

var ip = ImageProcessing.load("img.png");
ip.lock()
	.each(function(px, x, y){
		ip.setPixel(x, y, px.intensity(50));
	})
	.autoContrast()
.update();