Exemplo n.º 1
0
        /// <summary>
        ///   Calculates the mean squared error of this and another DeblurFactor
        /// </summary>
        /// <param name="deblurFilter"></param>
        /// <returns></returns>
        public Complex GetMse(DeblurFilter deblurFilter)
        {
            double errorRe = 0;
            double errorIm = 0;

            //var r1 = Data.Channel1;
            //var g1 = Data.Channel2;
            //var b1 = Data.Channel3;
            //var r2 = blurFactor.Data.Channel1;
            //var g2 = blurFactor.Data.Channel2;
            //var b2 = blurFactor.Data.Channel3;

            //var ilength = r1.Width;
            //var jlength = r1.Width;

            Action <Complex, Complex, int, int> action = delegate(Complex c1, Complex c2, int i, int j) {
                var divRe = c2.Real - c1.Real;
                var divIm = c2.Imaginary - c1.Imaginary;
                errorRe += divRe * divRe;
                errorIm += divIm * divRe;
            };

            C0.FoldWith(deblurFilter.C0, action);
            C1.FoldWith(deblurFilter.C1, action);
            C2.FoldWith(deblurFilter.C2, action);

            //for (var j = 0; j < jlength; j++)
            //  for (var i = 0; i < ilength; i++)
            //  {
            //    var divR = r2[i][j].Real - r1[i][j].Real;
            //    var divG = g2[i][j].Real - g1[i][j].Real;
            //    var divB = b2[i][j].Real - b1[i][j].Real;
            //    var divRi = r2[i][j].Imaginary - r1[i][j].Imaginary;
            //    var divGi = g2[i][j].Imaginary - g1[i][j].Imaginary;
            //    var divBi = b2[i][j].Imaginary - b1[i][j].Imaginary;

            //    errorRe += divR*divR;
            //    errorRe += divG*divG;
            //    errorRe += divB*divB;
            //    errorIm += divRi*divRi;
            //    errorIm += divGi*divGi;
            //    errorIm += divBi*divBi;
            //  }
            return(new Complex(errorRe, errorIm));
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Creates a deblur filter from three channel in frequency domain.
        /// </summary>
        /// <param name="c0Freqz">channel 0 in frequency domain</param>
        /// <param name="c1Freqz">channel 1 in frequency domain</param>
        /// <param name="c2Freqz">channel 2 in frequency domain</param>
        /// <param name="normalize">
        ///   if true, each pixel c_n with 0 &lt; n &lt; N-1 will be divided by 1/sum|c_n|, c_0 will be set
        ///   to 1+j0
        /// </param>
        /// <returns></returns>
        public static DeblurFilter CreateFromFrequencies(Complex2D c0Freqz,
                                                         Complex2D c1Freqz,
                                                         Complex2D c2Freqz,
                                                         bool normalize = false)
        {
            var w = MathCV.Max(c0Freqz.Width, c1Freqz.Width, c2Freqz.Width);
            var h = MathCV.Max(c0Freqz.Height, c1Freqz.Height, c2Freqz.Height);

            var result = new DeblurFilter {
                C0                                              = c0Freqz.Width != w || c0Freqz.Height != h?DoScale(c0Freqz, w, h) : new Complex2D(c0Freqz),
                                           C1                   = c1Freqz.Width != w || c1Freqz.Height != h?DoScale(c1Freqz, w, h) : new Complex2D(c1Freqz),
                                                             C2 = c2Freqz.Width != w || c2Freqz.Height != h?DoScale(c2Freqz, w, h) : new Complex2D(c2Freqz)
            };

            if (!normalize)
            {
                return(result);
            }

            var n     = w * h;
            var d0    = result.C0.Data;
            var d1    = result.C1.Data;
            var d2    = result.C2.Data;
            var norm0 = (n - 1) / (d0.SumAbs() - d0[0].Magnitude);
            var norm1 = (n - 1) / (d1.SumAbs() - d1[0].Magnitude);
            var norm2 = (n - 1) / (d2.SumAbs() - d2[0].Magnitude);

            d0[0] = d1[0] = d2[0] = Complex.One;
            for (var i = 1; i < n; i++)
            {
                d0[i] *= norm0;
                d1[i] *= norm1;
                d2[i] *= norm2;
            }

            return(result);
        }