private void buildD(BitmapData srcData) { int Dcols = (srcData.Width - blockDSize) / Dstep + 1; int Drows = (srcData.Height - blockDSize) / Dstep + 1; double Dcount = 0; double Dsize = Dcols * Drows; int centerOffset = blockSize; D = new BlockImage[Dcols, Drows][]; for (int j = 0; j < Drows; j++) { for (int i = 0; i < Dcols; i++) { Dcount++; if (monitor != null) { monitor.OnValueChanged(new ValueEventArgs() { value = Dcount / Dsize }); } BlockImage tD = new BlockImage(blockDSize); tD.fill(srcData, i * blockDSize + centerOffset, j * blockDSize + centerOffset, MyFilter.BorderMethod.NEAR); D[i, j] = new BlockImage[8]; D[i, j][0] = tD.subSample(); buildDT(D[i, j]); } } }
private void buildDT(BlockImage[] d) { if (d[0] != null) { for (int t = 1; t < 8; t++) { d[t] = BlockImage.T(d[0], t); } } }
private static double compare(BlockImage R, BlockImage D, double s, double o) { double distance = 0; for (int j = 0; j < R.Size; j++) { for (int i = 0; i < R.Size; i++) { distance += Math.Pow(R[i, j] - s * D[i, j] - o, 2); } } return(distance); }
public BlockImage SO(double s, double o) {//生成新亮度block BlockImage nBlock = new BlockImage(Size); for (int j = 0; j < Size; j++) { for (int i = 0; i < Size; i++) { nBlock[i, j] = _pixels[i, j] * s + o; } } return(nBlock); }
private void findMatch(out FractalFile file) { file = new FractalFile(R.GetLength(0), R.GetLength(1)); double Rcount = 0; double Rsize = R.GetLength(1) * R.GetLength(0); for (int j = 0; j < R.GetLength(1); j++) { for (int i = 0; i < R.GetLength(0); i++) { Rcount++; if (monitor != null) { monitor.OnValueChanged(new ValueEventArgs() { value = Rcount / Rsize }); } // refer R double md = Double.PositiveInfinity; int x = 0, y = 0, t = 0; double s = 1.0, o = 0; for (int Dr = 0; Dr < D.GetLength(1); Dr++) { for (int Dc = 0; Dc < D.GetLength(0); Dc++) { if (EncodeDraw && (viewer != null) && (VG != null)) { matchDraw(i, j, Dc, Dr); } //compare D for (int tt = 0; tt < 8; tt++) { double ts, to; double distance = BlockImage.mse(R[i, j], D[Dc, Dr][tt], out ts, out to); if (md > distance) { md = distance; x = Dc; y = Dr; t = tt; s = ts; o = to; } } } } file.pairs[i, j] = new FractalData(x, y, t, s, o); } } }
private static BlockImage T7(BlockImage src) {//mirror 135 int size = src.Size; BlockImage nBlock = new BlockImage(size); for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { nBlock[i, j] = src[j, i]; } } return(nBlock); }
private static BlockImage T3(BlockImage src) {//rotate 270 int size = src.Size; BlockImage nBlock = new BlockImage(size); for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { nBlock[i, j] = src[size - 1 - j, i]; } } return(nBlock); }
private void buildR(BitmapData srcData) { int Rcols = srcData.Width / blockSize; int Rrows = srcData.Height / blockSize; int centerOffset = blockSize / 2; R = new BlockImage[Rcols, Rrows]; for (int j = 0; j < Rrows; j++) { for (int i = 0; i < Rcols; i++) { R[i, j] = new BlockImage(blockSize); R[i, j].fill(srcData, i * blockSize + centerOffset, j * blockSize + centerOffset, MyFilter.BorderMethod.NEAR); } } }
public BlockImage subSample() {//縮減至 1/4 大小 int subSize = Size / 2; BlockImage blockS = new BlockImage(subSize); for (int j = 0; j < subSize; j++) { for (int i = 0; i < subSize; i++) { int Tx = 2 * i; int Ty = 2 * j; blockS[i, j] = this.getP(Tx, Ty, MyFilter.BorderMethod.NEAR); blockS[i, j] += this.getP(Tx + 1, Ty, MyFilter.BorderMethod.NEAR); blockS[i, j] += this.getP(Tx, Ty + 1, MyFilter.BorderMethod.NEAR); blockS[i, j] += this.getP(Tx + 1, Ty + 1, MyFilter.BorderMethod.NEAR); blockS[i, j] /= 4; } } return(blockS); }
public static double mse(BlockImage R, BlockImage D, out double s, out double o) {//return the min compare rate & s & o double distance = Double.PositiveInfinity; double td, ts, to; s = 1.0; o = 0; for (int cs = 0; cs < MSE_S.Length; cs++) { ts = MSE_S[cs]; to = R.avg() - ts * D.avg(); td = compare(R, D, ts, to); if (td < distance) { distance = td; s = ts; o = to; } } return(distance); }
public static BlockImage T(BlockImage src, int type) { ///type: /// 0 : no change /// 1 : rotate 90 /// 2 : rotate 180 /// 3 : rotate 270 /// 4 : mirror 0 /// 5 : mirror 90 /// 6 : mirror 45 /// 7 : mirror 135 switch (type) { case 0: return(src); case 1: //rotate 90 return(T1(src)); case 2: //rotate 180 return(T2(src)); case 3: //rotate 270 return(T3(src)); case 4: //mirror 0 return(T4(src)); case 5: //mirror 90 return(T5(src)); case 6: //mirror 45 return(T6(src)); case 7: //mirror 135 return(T7(src)); } return(null); }