Exemple #1
0
    //-----------------------------------------------------
    // distance between two patches in two images
    public static int distance(MaskedImage source,int xs,int ys, MaskedImage target,int xt,int yt, int S)
    {
        long distance=0, wsum=0, ssdmax = 9*255*255;

        // for each pixel in the source patch
        for(int dy=-S;dy<=S;dy++) {
            for(int dx=-S;dx<=S;dx++) {
                wsum+=ssdmax;

                int xks=xs+dx, yks=ys+dy;
                if (xks<1 || xks>=source.width-1) {distance+=ssdmax; continue;}
                if (yks<1 || yks>=source.height-1) {distance+=ssdmax; continue;}

                // cannot use masked pixels as a valid source of information
                if (source.isMasked(xks, yks)) {distance+=ssdmax; continue;}

                // corresponding pixel in the target patch
                int xkt=xt+dx, ykt=yt+dy;
                //Debug.Log("target.width:"+(target.getWidth()));
                //Debug.Log("xkt:"+xkt+" ykt:"+ykt);
                if (xkt < 1 || xkt >= target.width-1)
                                {
                                distance += ssdmax; continue;
                                }
                if (ykt<1 || ykt>=target.height-1)
                                {distance += ssdmax; continue;}

                // cannot use masked pixels as a valid source of information
                if (target.isMasked(xkt, ykt)) {distance+=ssdmax; continue;}

                // SSD distance between pixels (each value is in [0,255^2])
                long ssd=0;
                for(int band=0; band<3; band++) {
                    // pixel values
                    int s_value = source.getSample(xks, yks, band);
                    int t_value = source.getSample(xkt, ykt, band);

                    // pixel horizontal gradients (Gx)
                    float s_gx = 128+(source.getSample(xks+1, yks, band) - source.getSample(xks-1, yks, band))/2;
                    float t_gx = 128+(target.getSample(xkt+1, ykt, band) - target.getSample(xkt-1, ykt, band))/2;

                    // pixel vertical gradients (Gy)
                    float s_gy = 128+(source.getSample(xks, yks+1, band) - source.getSample(xks, yks-1, band))/2;
                    float t_gy = 128+(target.getSample(xkt, ykt+1, band) - target.getSample(xkt, ykt-1, band))/2;

                    ssd += (long) Mathf.Pow(s_value-t_value , 2); // distance between values in [0,255^2]
                    ssd += (long) Mathf.Pow(s_gx-t_gx , 2); // distance between Gx in [0,255^2]
                    ssd += (long) Mathf.Pow(s_gy-t_gy , 2); // distance between Gy in [0,255^2]
                }

                // add pixel distance to global patch distance
                distance += ssd;
            }
        }

        return (int)(DSCALE*distance/wsum);
    }
Exemple #2
0
    //-----------------------------------------------------
    private void weightedCopy(MaskedImage src, int xs, int ys, double[,,] vote, int xd,int yd, double w)
    {
        //Debug.Log("src:"+src.width+"/"+src.height+" xs/ys:"+xs+"/"+ys+" xd/yd:"+xd+"/"+yd+ "  w: "+w);

        if (src.isMasked(xs, ys)) return;
        //Debug.Log("vote: "+vote.GetLength(0)+"/"+vote.GetLength(1));
        if(xd<vote.GetLength(0) && yd<vote.GetLength(1)){
        vote[xd,yd,0] += w*src.getSample(xs, ys, 0);
        vote[xd,yd,1] += w*src.getSample(xs, ys, 1);
        vote[xd,yd,2] += w*src.getSample(xs, ys, 2);
        vote[xd,yd,3] += w;
        }
        else Debug.Log("en dehors");
    }