Search in sources :

Example 6 with GradientValue

use of boofcv.struct.sparse.GradientValue in project BoofCV by lessthanoptimal.

the class BenchmarkSurfDescribeOps method timeGradient_SampleCheck.

/**
 * Sample the gradient, but just for boundary conditions
 */
public int timeGradient_SampleCheck(int reps) {
    for (int i = 0; i < reps; i++) {
        double tl_x = this.tl_x + 0.5;
        double tl_y = this.tl_y + 0.5;
        int j = 0;
        for (int y = 0; y < regionSize; y++) {
            for (int x = 0; x < regionSize; x++, j++) {
                int xx = (int) (tl_x + x * period);
                int yy = (int) (tl_y + y * period);
                if (g.isInBounds(xx, yy)) {
                    GradientValue deriv = g.compute(xx, yy);
                    derivX[j] = deriv.getX();
                    derivY[j] = deriv.getY();
                }
            }
        }
    }
    return 0;
}
Also used : GradientValue(boofcv.struct.sparse.GradientValue)

Example 7 with GradientValue

use of boofcv.struct.sparse.GradientValue in project BoofCV by lessthanoptimal.

the class DescribePointSurf method features.

/**
 * <p>
 * Computes features in the SURF descriptor.
 * </p>
 *
 * <p>
 * Deviation from paper:<br>
 * <ul>
 * <li>Weighting function is applied to each sub region as a whole and not to each wavelet inside the sub
 * region.  This allows the weight to be precomputed once.  Unlikely to degrade quality significantly.</li>
 * </ul>
 * </p>
 *
 * @param c_x Center of the feature x-coordinate.
 * @param c_y Center of the feature y-coordinate.
 * @param c cosine of the orientation
 * @param s sine of the orientation
 * @param scale The scale of the wavelets.
 * @param features Where the features are written to.  Must be 4*(widthLargeGrid*widthSubRegion)^2 large.
 */
public void features(double c_x, double c_y, double c, double s, double scale, SparseImageGradient gradient, double[] features) {
    int regionSize = widthLargeGrid * widthSubRegion;
    if (weight.width != regionSize) {
        throw new IllegalArgumentException("Weighting kernel has an unexpected size");
    }
    int regionR = regionSize / 2;
    int regionEnd = regionSize - regionR;
    int regionIndex = 0;
    // when computing the pixel coordinates it is more precise to round to the nearest integer
    // since pixels are always positive round() is equivalent to adding 0.5 and then converting
    // to an int, which floors the variable.
    c_x += 0.5;
    c_y += 0.5;
    // step through the sub-regions
    for (int rY = -regionR; rY < regionEnd; rY += widthSubRegion) {
        for (int rX = -regionR; rX < regionEnd; rX += widthSubRegion) {
            double sum_dx = 0, sum_dy = 0, sum_adx = 0, sum_ady = 0;
            // compute and sum up the response  inside the sub-region
            for (int i = 0; i < widthSubRegion; i++) {
                double regionY = (rY + i) * scale;
                for (int j = 0; j < widthSubRegion; j++) {
                    double w = weight.get(regionR + rX + j, regionR + rY + i);
                    double regionX = (rX + j) * scale;
                    // rotate the pixel along the feature's direction
                    int pixelX = (int) (c_x + c * regionX - s * regionY);
                    int pixelY = (int) (c_y + s * regionX + c * regionY);
                    // compute the wavelet and multiply by the weighting factor
                    GradientValue g = gradient.compute(pixelX, pixelY);
                    double dx = w * g.getX();
                    double dy = w * g.getY();
                    // align the gradient along image patch
                    // note the transform is transposed
                    double pdx = c * dx + s * dy;
                    double pdy = -s * dx + c * dy;
                    sum_dx += pdx;
                    sum_adx += Math.abs(pdx);
                    sum_dy += pdy;
                    sum_ady += Math.abs(pdy);
                }
            }
            features[regionIndex++] = sum_dx;
            features[regionIndex++] = sum_adx;
            features[regionIndex++] = sum_dy;
            features[regionIndex++] = sum_ady;
        }
    }
}
Also used : GradientValue(boofcv.struct.sparse.GradientValue)

Example 8 with GradientValue

use of boofcv.struct.sparse.GradientValue in project BoofCV by lessthanoptimal.

the class ImplSurfDescribeOps method naiveGradient.

/**
 * Simple algorithm for computing the gradient of a region.  Can handle image borders
 */
public static <T extends ImageGray<T>> void naiveGradient(T ii, double tl_x, double tl_y, double samplePeriod, int regionSize, double kernelSize, boolean useHaar, double[] derivX, double[] derivY) {
    SparseScaleGradient<T, ?> gg = SurfDescribeOps.createGradient(useHaar, (Class<T>) ii.getClass());
    gg.setWidth(kernelSize);
    gg.setImage(ii);
    SparseGradientSafe g = new SparseGradientSafe(gg);
    // add 0.5 to c_x and c_y to have it round when converted to an integer pixel
    // this is faster than the straight forward method
    tl_x += 0.5;
    tl_y += 0.5;
    int i = 0;
    for (int y = 0; y < regionSize; y++) {
        for (int x = 0; x < regionSize; x++, i++) {
            int xx = (int) (tl_x + x * samplePeriod);
            int yy = (int) (tl_y + y * samplePeriod);
            GradientValue deriv = g.compute(xx, yy);
            derivX[i] = deriv.getX();
            derivY[i] = deriv.getY();
        // System.out.printf("%2d %2d %2d %2d dx = %6.2f  dy = %6.2f\n",x,y,xx,yy,derivX[i],derivY[i]);
        }
    }
}
Also used : GradientValue(boofcv.struct.sparse.GradientValue) SparseGradientSafe(boofcv.struct.sparse.SparseGradientSafe)

Example 9 with GradientValue

use of boofcv.struct.sparse.GradientValue in project BoofCV by lessthanoptimal.

the class ImplOrientationAverageGradientIntegral method computeUnweighted.

/**
 * Compute the gradient while checking for border conditions
 */
protected double computeUnweighted(double tl_x, double tl_y, double samplePeriod, SparseImageGradient<T, G> g) {
    // add 0.5 to c_x and c_y to have it round
    tl_x += 0.5;
    tl_y += 0.5;
    double Dx = 0, Dy = 0;
    for (int y = 0; y < sampleWidth; y++) {
        int pixelsY = (int) (tl_y + y * samplePeriod);
        for (int x = 0; x < sampleWidth; x++) {
            int pixelsX = (int) (tl_x + x * samplePeriod);
            GradientValue v = g.compute(pixelsX, pixelsY);
            Dx += v.getX();
            Dy += v.getY();
        }
    }
    return Math.atan2(Dy, Dx);
}
Also used : GradientValue(boofcv.struct.sparse.GradientValue)

Example 10 with GradientValue

use of boofcv.struct.sparse.GradientValue in project BoofCV by lessthanoptimal.

the class ImplOrientationAverageGradientIntegral method computeWeighted.

/**
 * Compute the gradient while checking for border conditions
 */
protected double computeWeighted(double tl_x, double tl_y, double samplePeriod, SparseImageGradient<T, G> g) {
    // add 0.5 to c_x and c_y to have it round
    tl_x += 0.5;
    tl_y += 0.5;
    double Dx = 0, Dy = 0;
    int i = 0;
    for (int y = 0; y < sampleWidth; y++) {
        int pixelsY = (int) (tl_y + y * samplePeriod);
        for (int x = 0; x < sampleWidth; x++, i++) {
            int pixelsX = (int) (tl_x + x * samplePeriod);
            double w = weights.data[i];
            GradientValue v = g.compute(pixelsX, pixelsY);
            Dx += w * v.getX();
            Dy += w * v.getY();
        }
    }
    return Math.atan2(Dy, Dx);
}
Also used : GradientValue(boofcv.struct.sparse.GradientValue)

Aggregations

GradientValue (boofcv.struct.sparse.GradientValue)10 SparseImageGradient (boofcv.struct.sparse.SparseImageGradient)2 Test (org.junit.Test)2 ImageGradient (boofcv.abst.filter.derivative.ImageGradient)1 FactoryImageBorder (boofcv.core.image.border.FactoryImageBorder)1 ImageBorder (boofcv.core.image.border.ImageBorder)1 SparseGradientSafe (boofcv.struct.sparse.SparseGradientSafe)1