两张图片相似度比较

时间:2023-01-20 17:36:51


package  {
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	public class HashClass {

		public function HashClass() {
			// constructor code
		}
		public function compareBitmapData(bmpData1:BitmapData,bmpData2:BitmapData):Number{
			var str1 =  process(bmpData1);
			var str2 =  process(bmpData2);
			var arr1:Array = str1.split("");
			var arr2:Array = str2.split("");
 			var diffCount:int=0;
 			for(var i1:int=0; i1<64;i1++){ 
	 				if(arr1[i1]==arr2[i1]){
		 				diffCount=diffCount+1;
	 				} 
 			} 

			return diffCount/64; 
		}
		public static function process(bmpData:BitmapData):String{
			//processing the image
			//   trace("Start processing...\n");
			//scaling and converting
			var resizedData:BitmapData = reduceSize(bmpData,8,8);
			//转换为灰度
			var greyBmp:BitmapData = reduceColor(resizedData);
			//计算灰度平均值
			var avgGrey:uint = calcAvgGrey(greyBmp);
 			//trace("Average GreyScale:0x"+avgGrey.toString(16)+"\n");

			//比较灰度值与平均值,建立哈希指纹
			var hashArr:Array = calcAvgHash(resizedData, avgGrey);
			//   trace("hashArr: ",hashArr.join(""));
			return hashArr.join("");
		}	
 
		public static function reduceSize(source:BitmapData,width:Number = 8, height:Number=8):BitmapData{
			var newData:BitmapData = new BitmapData(width,height);
			var matrix:Matrix = new Matrix();
			//缩小至 8x8
			matrix.scale(newData.width/source.width, newData.height/source.height);
			newData.draw(source,matrix);
 
			return newData;
		}
 
		public static function reduceColor(source:BitmapData):BitmapData{
			var result:BitmapData = new BitmapData(source.width,source.height);
			for(var i:int = 0; i < source.height; i++){
					for(var j:uint = 0; j < source.width; j++){
                        var color:uint = source.getPixel(i, j);
                        var red:uint = (color & 0xFF0000) >> 16;
						var green:uint = (color & 0x00FF00) >> 8;
						var blue:uint = (color & 0x0000FF) >> 0;
						//trace(red+"+"+green+"+"+blue);
						//var bwColor:uint = (red + green + blue) / 3;
						var bwColor:uint = (red * 30 + green * 59 + blue * 11) / 100;
						// puts the average in each channel
						bwColor = (bwColor << 16) + (bwColor << 8) + bwColor; 
						result.setPixel(i, j, bwColor);
    				}
  			}
  			return result;
		}
                /*
		public static function calcAvgGrey(bmpData:BitmapData):uint{
			var vecGrey:Vector.<uint> = bmpData.getVector(bmpData.rect);
			var total:uint = 0;
			var length:uint = vecGrey.length;
			for(var i:int = 0; i< length;i++){
					total += (vecGrey[i] & 0x00FFFFFF);
  			}
  			return uint(total/vecGrey.length);
		}
 
		//计算哈希
		public static function calcAvgHash(bmpData:BitmapData, avgValue:uint):Array {
			var vecGrey:Vector.<uint>  = bmpData.getVector(bmpData.rect);
			var length:uint = vecGrey.length;
			var hashArr:Array = [];
			for(var i:int = 0; i< length;i++) {
				//ARGB 32位数据,只取RGB
				var pxColor:uint = vecGrey[i] & 0x00FFFFFF;	
				//是否小于灰度均值,小于记0,否则记0
				var value:uint =  pxColor > avgValue ? 0:1;
				hashArr.push(value);
  			}
  			return hashArr;
		}*/
		//求平均灰度值
		public static function calcAvgGrey(bmpData:BitmapData):uint{
			var vecGrey:Vector.<uint> = bmpData.getVector(bmpData.rect);
			var total:uint = 0;
			var length:uint = vecGrey.length;
			for(var i:int = 0; i< length;i++){ 
				var color:uint = vecGrey[i];
				var red:uint = (color & 0xFF0000) >> 16;
				var green:uint = (color & 0x00FF00) >> 8;
				var blue:uint = (color & 0x0000FF) >> 0; 
				var bwColor:uint = (red * 30 + green * 59 + blue * 11) / 100;
				total = total + bwColor;
  			}
  			return uint(total/vecGrey.length);
		}
 
		//计算哈希
		public static function calcAvgHash(bmpData:BitmapData, avgValue:uint):Array {
			var vecGrey:Vector.<uint>  = bmpData.getVector(bmpData.rect);
			var length:uint = vecGrey.length;
			var hashArr:Array = [];
			for(var i:int = 0; i< length;i++) {
				var color:uint = vecGrey[i];
				var red:uint = (color & 0xFF0000) >> 16;
				var green:uint = (color & 0x00FF00) >> 8;
				var blue:uint = (color & 0x0000FF) >> 0; 
				var bwColor:uint = (red * 30 + green * 59 + blue * 11) / 100;
				//ARGB 32位数据,只取RGB
				var pxColor:uint = bwColor;	
				//是否小于灰度均值,小于记0,否则记0  减2可以提高检测的容错性
				var value:uint =  pxColor-2 > avgValue ? 0:1;
				//trace(pxColor + "----" +avgValue);
				hashArr.push(value);
  			}
  			return hashArr;
		}

	}
	
}