4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AS3で類似した画像を判定

4
Last updated at Posted at 2014-10-02

・画像比較をして差分を取得
・差分に対してグレースケール、ブラーのフィルタをかける
・指定のしきい値を超えた部分を黒く塗りつぶす
・黒く塗りつぶした箇所があれば差異ありと判定

/** グレースケール用フィルタ */
private static var GRAY_SCALE_FILTER:ColorMatrixFilter = new ColorMatrixFilter(
	[
	0.3086, 0.6094, 0.0820, 0, 0,
	0.3086, 0.6094, 0.0820, 0, 0,
	0.3086, 0.6094, 0.0820, 0, 0,
	0, 0, 0, 1, 0
	] );
/** ブラー用フィルタ */
private static var BLUR_FILTER:BlurFilter = new BlurFilter( 10, 10 );

/**
* 画像の差異をチェックする
* @param item1 ベース画像
* @param item2 比較対象の画像
* @param threshold しきい値 0~1.0
* @return true:差異あり false:差異なし
*/
public function diff( item1:BitmapData, item2:BitmapData, threshold:Number ):Boolean {
	// 画像の比較
	var result:Object = item1.compare( item2 );
	if ( result is BitmapData ) {
		var bmpdResult:BitmapData = result as BitmapData;
		// 差分に対してグレースケール、ブラーのフィルタを適応する
		bmpdResult.applyFilter( bmpdResult, bmpdResult.rect, new Point(), GRAY_SCALE_FILTER );
		bmpdResult.applyFilter( bmpdResult, bmpdResult.rect, new Point(), BLUR_FILTER );

		// 指定されたしきい値と比較してイメージ内のピクセル値をテストし、テストに適合したピクセルに新しいカラー値を設定します
		var container:BitmapData = new BitmapData( item1.width, item1.height, true, 0x000000 );
		var changedPixels:Number = container.threshold( bmpdResult, bmpdResult.rect, new Point(), ">", threshold * 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, false );

		if ( changedPixels > 0 ) {
			// しきい値を超えたピクセルが存在しない場合は差異なしとする
			return true;
		}
		return false;
	}

	if ( result == 0 ) {
		// 画像自体に差異なし
		return false;
	} else {
		// -3:幅が違う  -4:高さが違う
	return true;
	}
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?