Example #1
0
        private void refineSample()
        {
            if (ftuples.Count < width * height + 1)
            {
                for (int i = ftuples.Count; i < width * height + 1; i++)
                {
                    ftuples.Add((new FtupleInfo()));
                }
            }

            while (ftuples.Count > width * height + 1)
            {
                ftuples.RemoveAt(ftuples.Count - 1);
            }

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    double CB     = data[i * step + j * channels];
                    double CG     = data[i * step + j * channels + 1];
                    double CR     = data[i * step + j * channels + 2];
                    int    indexf = i * width + j;
                    int    gray   = tri[i, j];
                    if (gray == 0)
                    {
                        ftuples[indexf].FB = CB;
                        ftuples[indexf].FG = CG;
                        ftuples[indexf].FR = CR;

                        ftuples[indexf].BB         = CB;
                        ftuples[indexf].BG         = CG;
                        ftuples[indexf].BR         = CR;
                        ftuples[indexf].alphar     = 0;
                        ftuples[indexf].confidence = 1;
                        alpha[i, j] = 0;
                    }
                    else if (gray == 255)
                    {
                        ftuples[indexf].FB = CB;
                        ftuples[indexf].FG = CG;
                        ftuples[indexf].FR = CR;

                        ftuples[indexf].BB         = CB;
                        ftuples[indexf].BG         = CG;
                        ftuples[indexf].BR         = CR;
                        ftuples[indexf].alphar     = 1;
                        ftuples[indexf].confidence = 1;
                        alpha[i, j] = 255;
                    }
                }
            }

            for (int i = 0; i < uT.Count; i++)
            {
                int xi = uT[i].X;
                int yj = uT[i].Y;
                int i1 = Math.Max(0, xi - 5);
                int i2 = Math.Min(xi + 5, height - 1);
                int j1 = Math.Max(0, yj - 5);
                int j2 = Math.Min(yj + 5, width - 1);

                double[]        minvalue = { 1e10, 1e10, 1e10 };
                Drawing.Point[] p        = new Drawing.Point[3];
                int             num      = 0;
                for (int k = i1; k <= i2; ++k)
                {
                    for (int l = j1; l <= j2; ++l)
                    {
                        int temp = tri[k, l];

                        if (temp == 0 || temp == 255)
                        {
                            continue;
                        }

                        TupleInfo t = tuples[unknownIndex[k, l]];
                        if (t.flag == -1)
                        {
                            continue;
                        }

                        double m = mP(xi, yj, t.FB, t.FG, t.FR, t.BB, t.BG, t.BR);
                        if (m > minvalue[2])
                        {
                            continue;
                        }

                        if (m < minvalue[0])
                        {
                            minvalue[2] = minvalue[1];
                            p[2]        = p[1];

                            minvalue[1] = minvalue[0];
                            p[1]        = p[0];

                            minvalue[0] = m;
                            p[0].X      = k;
                            p[0].Y      = l;

                            ++num;
                        }
                        else if (m < minvalue[1])
                        {
                            minvalue[2] = minvalue[1];
                            p[2]        = p[1];

                            minvalue[1] = m;
                            p[1].X      = k;
                            p[1].Y      = l;

                            ++num;
                        }
                        else if (m < minvalue[2])
                        {
                            minvalue[2] = m;
                            p[2].X      = k;
                            p[2].Y      = l;

                            ++num;
                        }
                    }
                }

                num = Math.Min(num, 3);

                double FB = 0;
                double FG = 0;
                double FR = 0;

                double BB = 0;
                double BG = 0;
                double BR = 0;

                double sf = 0;
                double sb = 0;

                for (int k = 0; k < num; ++k)
                {
                    int idx = unknownIndex[p[k].X, p[k].Y];
                    FB += tuples[idx].FB;
                    FG += tuples[idx].FG;
                    FR += tuples[idx].FR;

                    BB += tuples[idx].BB;
                    BG += tuples[idx].BG;
                    BR += tuples[idx].BR;

                    sf += tuples[idx].sigmaf;
                    sb += tuples[idx].sigmab;
                }

                FB /= (num + 1e-10);
                FG /= (num + 1e-10);
                FR /= (num + 1e-10);

                BB /= (num + 1e-10);
                BG /= (num + 1e-10);
                BR /= (num + 1e-10);

                sf /= (num + 1e-10);
                sb /= (num + 1e-10);

                double PB = data[xi * step + yj * channels];
                double PG = data[xi * step + yj * channels + 1];
                double PR = data[xi * step + yj * channels + 2];

                double df = this.CalcColorDistance(PB, PG, PR, FB, FG, FR);
                double db = this.CalcColorDistance(PB, PG, PR, BB, BG, BR);

                double tFB = FB;
                double tFG = FG;
                double tFR = FR;

                double tBB = BB;
                double tBG = BG;
                double tBR = BR;

                int index = xi * width + yj;
                if (df < sf)
                {
                    FB = PB;
                    FG = PG;
                    FR = PR;
                }
                if (db < sb)
                {
                    BB = PB;
                    BG = PG;
                    BR = PR;
                }
                if (FB == BB && FG == BG && FR == BR)
                {
                    ftuples[index].confidence = 0.00000001;
                }
                else
                {
                    ftuples[index].confidence = Math.Exp(-10 * mP(xi, yj, tFB, tFG, tFR, tBB, tBG, tBR));
                }


                ftuples[index].FB = FB;
                ftuples[index].FG = FG;
                ftuples[index].FR = FR;
                ftuples[index].BB = BB;
                ftuples[index].BG = BG;
                ftuples[index].BR = BR;

                ftuples[index].alphar = Math.Max(0.0, Math.Min(1.0, this.CalcAlpha(PB, PG, PR, FB, FG, FR, BB, BG, BR)));
            }
            tuples.Clear();
        }
Example #2
0
        private void gathering()
        {
            List <Drawing.Point>         f = new List <Drawing.Point>();
            List <Drawing.Point>         b = new List <Drawing.Point>();
            List <List <Drawing.Point> > F = new List <List <Drawing.Point> >();
            List <List <Drawing.Point> > B = new List <List <Drawing.Point> >();

            Sample(F, B);

            int    index = 0;
            double a;
            int    size = uT.Count();

            for (int m = 0; m < size; ++m)
            {
                int i = uT[m].X;
                int j = uT[m].Y;

                double pfp  = pfP(new Drawing.Point(i, j), F[m], B[m]);
                double gmin = 1.0e10;

                Drawing.Point tf = new Drawing.Point();
                Drawing.Point tb = new Drawing.Point();

                bool flag  = false;
                bool first = true;

                for (int idx = 0; idx < F[m].Count; idx++)
                {
                    double dpf = dP(new Drawing.Point(i, j), F[m][idx]);
                    for (int idx2 = 0; idx2 < B[m].Count; idx2++)
                    {
                        double gp = gP(new Drawing.Point(i, j), F[m][idx], B[m][idx2], dpf, pfp);
                        if (gp < gmin)
                        {
                            gmin = gp;
                            tf   = new Drawing.Point(F[m][idx].X, F[m][idx].Y);
                            tb   = new Drawing.Point(B[m][idx2].X, B[m][idx2].Y);
                            flag = true;
                        }
                    }
                }

                TupleInfo tupleInfo = new TupleInfo();
                tupleInfo.flag = -1;
                if (flag)
                {
                    tupleInfo.FB   = data[tf.X * step + tf.Y * channels];
                    tupleInfo.FG   = data[tf.X * step + tf.Y * channels + 1];
                    tupleInfo.FR   = data[tf.X * step + tf.Y * channels + 2];
                    tupleInfo.flag = 1;

                    tupleInfo.BB = data[tb.X * step + tb.Y * channels];
                    tupleInfo.BG = data[tb.X * step + tb.Y * channels + 1];
                    tupleInfo.BR = data[tb.X * step + tb.Y * channels + 2];

                    tupleInfo.sigmaf = sigma2(tf);
                    tupleInfo.sigmab = sigma2(tb);
                }

                tuples.Add(tupleInfo);
                unknownIndex[i, j] = index;
                ++index;
            }
            f.Clear();
            b.Clear();
        }