예제 #1
0
 // -------------------------------------------
 /// projects a sample on n first eigen vectors
 public void project(NeighborhoodSynth smp, int n, ref NeighborhoodSynth ret)
 {
     Globals.Assert(n <= m_EigenVects.Count);
     for (int d = 0; d < m_iNumDim; d++)
     {
         ret.setValue(d, 0.0f);
     }
     for (int i = 0; i < n; i++)
     {
         Globals.Assert(m_EigenVects[i].Count == m_iNumDim);
         float proj = 0.0f;
         for (int j = 0; j < m_iNumDim; j++)
         {
             proj += m_EigenVects[i][j] * smp.getValue(j);
         }
         ret.setValue(i, proj);
     }
 }
예제 #2
0
        void computeProjectedSynthNeighborhoods(List <PCA /*_Synth*/> pcaSynth)
        {
            bool clean_neigh = false;

            // if synth neighborhoods not available, compute them
            if (m_SynthNeighborhoods == null)
            {
                computeSynthNeighborhoods();
                clean_neigh = true;
            }

            {
                ScopeTimer tm = new ScopeTimer("[computeProjectedSynthNeighborhoods]");

                m_ProjectedSynthNeighborhoods = new NeighborhoodSynth[mNumLevels][];
                // foreach level
                for (int l = 0; l < mNumLevels; l++)
                {
                    m_ProjectedSynthNeighborhoods[l] = new NeighborhoodSynth[mOwner.recoloredStack(l).getWidth() * mOwner.recoloredStack(l).getHeight()];
                    for (int j = 0; j < mOwner.recoloredStack(l).getHeight(); j++)
                    {
                        for (int i = 0; i < mOwner.recoloredStack(l).getWidth(); i++)
                        {
                            m_ProjectedSynthNeighborhoods[l][i + j * mOwner.recoloredStack(l).getWidth()] = new NeighborhoodSynth();
                            NeighborhoodSynth neigh = m_SynthNeighborhoods[l][i + j * mOwner.recoloredStack(l).getWidth()];
                            NeighborhoodSynth proj  = m_ProjectedSynthNeighborhoods[l][i + j * mOwner.recoloredStack(l).getWidth()];
                            pcaSynth[l].project(neigh, Globals.NUM_RUNTIME_PCA_COMPONENTS, ref proj);
                            proj.setIJ(i, j);
                        }
                    }
                }
                tm.destroy();
                tm = null;
            }


            // clean RT neighborhoods
            if (clean_neigh)
            {
                m_SynthNeighborhoods = null;
            }
        }
예제 #3
0
        unsafe void synthesisNeighborhoodsToD3DTexture(Exemplar a, int l)
        {
            // synthesis neighborhoods
            // . build float multidim texture from projected neighborhoods
            MultiDimFloatTexture tex = new MultiDimFloatTexture(a.recoloredStack(l).width(), a.recoloredStack(l).height(), Globals.NUM_RUNTIME_PCA_COMPONENTS);

            for (int j = 0; j < tex.height(); j++)
            {
                for (int i = 0; i < tex.width(); i++)
                {
                    NeighborhoodSynth nproj = a.getProjectedSynthNeighborhood(l, i, j);
                    for (int c = 0; c < tex.numComp(); c++)
                    {
                        tex.set(nproj.getValue(c), i, j, c);
                    }
                }
            }

            // . quantize
            Quantizer q = new Quantizer(tex, Globals.QUANTIZE_NUM_BITS, Globals.QUANTIZE_PERCENT_INSIDE);

            m_d3dNeighborhoods_0_3 = new Texture(BRenderDevice.getDevice(), tex.width(), tex.height(), 1, 0, Format.A8R8G8B8, Pool.Managed);



            // . fill
            GraphicsStream texstream = m_d3dNeighborhoods_0_3.LockRectangle(0, LockFlags.None);
            byte *         data      = (byte *)texstream.InternalDataPointer;
            int            rectPitch = tex.width() * 4;

            for (int j = 0; j < tex.height(); j++)
            {
                for (int i = 0; i < tex.width(); i++)
                {
                    int v0 = q.quantized().get(i, j, 0);
                    int v1 = q.quantized().get(i, j, 1);
                    int v2 = q.quantized().get(i, j, 2);
                    int v3 = q.quantized().get(i, j, 3);
                    Globals.Assert(v0 >= 0 && v0 <= 255);
                    Globals.Assert(v1 >= 0 && v1 <= 255);
                    Globals.Assert(v2 >= 0 && v2 <= 255);
                    Globals.Assert(v3 >= 0 && v3 <= 255);
                    data[i * 4 + j * rectPitch + 2] = (byte)(v0);
                    data[i * 4 + j * rectPitch + 1] = (byte)(v1);
                    data[i * 4 + j * rectPitch + 0] = (byte)(v2);
                    data[i * 4 + j * rectPitch + 3] = (byte)(v3);
                }
            }
            m_d3dNeighborhoods_0_3.UnlockRectangle(0);


            // de-quantization parameters

            m_UnqNeighborhoods_Scale = new List <float>(8);
            m_UnqNeighborhoods_Mean  = new List <float>(8);

            for (int c = 0; c < q.quantized().numComp(); c++)
            {
                m_UnqNeighborhoods_Scale.Add(q.radius(c));
                m_UnqNeighborhoods_Mean.Add(q.center(c));
            }
            qn_mean_0_3  = new Vector4(m_UnqNeighborhoods_Mean[0], m_UnqNeighborhoods_Mean[1], m_UnqNeighborhoods_Mean[2], m_UnqNeighborhoods_Mean[3]);
            qn_scale_0_3 = new Vector4(m_UnqNeighborhoods_Scale[0], m_UnqNeighborhoods_Scale[1], m_UnqNeighborhoods_Scale[2], m_UnqNeighborhoods_Scale[3]);

            // expression in the shader is
            // (v*2.0-1.0)*UnqNeighborhoods_Scale_0_3 + UnqNeighborhoods_Mean_0_3
            // => this is baked into the ants to reduce work load
            qn_mean_0_3  = qn_mean_0_3 - qn_scale_0_3;
            qn_scale_0_3 = qn_scale_0_3 * 2.0f;
        }
예제 #4
0
        void computeSynthNeighborhoods()
        {
            ScopeTimer tm = new ScopeTimer("[computeSynthNeighborhoods]");

            m_SynthNeighborhoods = new NeighborhoodSynth[mNumLevels][];

            // foreach level
            for (int level = 0; level < mNumLevels; level++)
            {
                MultiDimFloatTexture recolored_level = null;

                if (Globals.isDefined("4D"))
                {
                    // . keep only 4 dimension from recolored exemplar
                    MultiDimFloatTexture level_4D = new MultiDimFloatTexture(mOwner.recoloredStack(level).width(), mOwner.recoloredStack(level).height(), mOwner.recoloredStack(level).numComp());
                    int w = level_4D.getWidth();
                    int h = level_4D.getHeight();
                    Globals.Assert(w == mOwner.stack(level).getWidth() && h == mOwner.stack(level).height());
                    Globals.Assert(level_4D.numComp() == Globals.NUM_RECOLORED_PCA_COMPONENTS);
                    for (int i = 0; i < w; i++)
                    {
                        for (int j = 0; j < h; j++)
                        {
                            // . copy first four channels
                            for (int c = 0; c < 4; c++)
                            {
                                level_4D.set(mOwner.recoloredStack(level).get(i, j, c), i, j, c);
                            }
                            // . zero out all channels above 4
                            for (int c = 4; c < level_4D.numComp(); c++)
                            {
                                level_4D.set(0, i, j, c);
                            }
                        }
                    }
                    recolored_level = level_4D;
                }
                else
                {
                    // . keep all dimensions
                    recolored_level = mOwner.recoloredStack(level);
                }

                m_SynthNeighborhoods[level] = new NeighborhoodSynth[recolored_level.width() * recolored_level.height()];

                stack_accessor_v2 access = new stack_accessor_v2(level);


                for (int j = 0; j < recolored_level.height(); j++)
                {
                    for (int i = 0; i < recolored_level.width(); i++)
                    {
                        int index = i + j * recolored_level.width();
                        m_SynthNeighborhoods[level][index] = new NeighborhoodSynth();
                        m_SynthNeighborhoods[level][index].construct(
                            recolored_level,
                            access,
                            (!mOwner.isToroidal()) && level < (mNumLevels - Globals.NUM_LEVELS_WITHOUT_BORDER),
                            //(!m_bToroidal) && l < FIRST_LEVEL_WITH_BORDER,
                            level, i, j);
                    }
                }
            }

            tm.destroy();
            tm = null;
        }