package {
import __AS3__.vec.Vector;
import flash.display.*;
import flash.geom.Point;
import flash.utils.getTimer;
[SWF(width = "514", height = "300", backgroundColor = "#000000", frameRate = "12")]
public class POC extends Sprite {
[Embed(source = './assets/lennagray1.jpg')]
private var EmbedImage:Class;
[Embed(source = './assets/lennagray2.jpg')]
private var EmbedImage2:Class;
private var srcBmp:Bitmap;
private var srcBmp2:Bitmap;
private var imgData:BitmapData;
private var imgData2:BitmapData;
private var re:Vector.<Number>;
private var im:Vector.<Number>;
private var re2:Vector.<Number>;
private var im2:Vector.<Number>;
private var rePOC:Vector.<Number>;
private var imPOC:Vector.<Number>;
private var w:int = 0;
private var h:int = 0;
public function POC() {
srcBmp = new EmbedImage() as Bitmap;
imgData = srcBmp.bitmapData.clone();
srcBmp2 = new EmbedImage2() as Bitmap;
imgData2 = srcBmp2.bitmapData.clone();
w = imgData.width;
h = imgData.height;
var ts:int = getTimer();
// 画像データの直列化
re = Vector.<Number>(imgData.getVector(imgData.rect));
re2 = Vector.<Number>(imgData2.getVector(imgData2.rect));
im = new Vector.<Number>(w*h, true);
im2 = im.concat();
rePOC = im.concat();
imPOC = im.concat();
// 位相限定相関
phaseOnlyCorrelation(re, im, re2, im2, rePOC, imPOC);
var te:int = getTimer();
trace("time: " + (te - ts) + " ms");
// ピーク値と座標の探索
var peak:Number = 0.0, spectrum:Number = 0.0;
var pos:Point = new Point();
for(var y:int=0; y<h; y++) {
for(var x:int=0; x<w; x++) {
//spectrum = Math.sqrt(rePOC[x + y*w]*rePOC[x + y*w] + imPOC[x + y*w]*imPOC[x + y*w]);
spectrum = rePOC[x + y*w] + Math.abs(imPOC[x + y*w]);
if(spectrum > peak){
peak = spectrum;
pos.x = x;
pos.y = y;
}
}
}
trace("peak: " + peak + ", position: " + pos);
addChild(srcBmp);
addChild(srcBmp2).x += w + 2;
}
// 位相限定相関
private function phaseOnlyCorrelation(srcRe1:Vector.<Number>, srcIm1:Vector.<Number>, srcRe2:Vector.<Number>, srcIm2:Vector.<Number>, dstRe:Vector.<Number>, dstIm:Vector.<Number>):void {
var fft:FFT = new FFT(w);
var spectrum:Number = 0.0;
// 2D-FFT
fft.fft2d(srcRe1, srcIm1);
fft.fft2d(srcRe2, srcIm2);
for(var i:int=0; i<w*h; i++) {
// 位相を算出 (振幅で正規化)
spectrum = Math.sqrt(srcRe1[i]*srcRe1[i] + srcIm1[i]*srcIm1[i]);
srcRe1[i] /= spectrum;
srcIm1[i] /= spectrum;
spectrum = Math.sqrt(srcRe2[i]*srcRe2[i] + srcIm2[i]*srcIm2[i]);
srcRe2[i] /= spectrum;
srcIm2[i] /= spectrum;
// 相互相関
dstRe[i] = srcRe1[i]*srcRe2[i];
dstIm[i] = srcIm1[i]*(-srcIm2[i]);
}
// 2D-IFFT
fft.fft2d(dstRe, dstIm, true);
}
}
}