Example usage for org.opencv.core Core randu

List of usage examples for org.opencv.core Core randu

Introduction

In this page you can find the example usage for org.opencv.core Core randu.

Prototype

public static void randu(Mat dst, double low, double high) 

Source Link

Usage

From source file:com.trandi.opentld.tld.PatchGenerator.java

License:Apache License

/**
 * //  w w w.ja  v a2 s . c  o m
 * @param image
 * @param T
 * @param patch OUTPUT
 * @param patchSize
 */
void generate(final Mat image, final Mat T, Mat patch, Size patchSize, final RNG rng) {
    patch.create(patchSize, image.type());
    if (backgroundMin != backgroundMax) {
        Core.randu(patch, backgroundMin, backgroundMax);
        // TODO if that null scalar OK or should it be new Scalar(0) ?
        Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Core.BORDER_TRANSPARENT, null);
    } else {
        Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Core.BORDER_CONSTANT,
                new Scalar(backgroundMin));
    }

    int ksize = randomBlur ? rng.nextInt() % 9 - 5 : 0;
    if (ksize > 0) {
        ksize = ksize * 2 + 1;
        Imgproc.GaussianBlur(patch, patch, new Size(ksize, ksize), 0, 0);
    }

    if (noiseRange > 0) {
        final Mat noise = new Mat(patchSize, image.type());
        int delta = (image.depth() == CvType.CV_8U ? 128 : (image.depth() == CvType.CV_16U ? 32768 : 0));
        Core.randn(noise, delta, noiseRange);

        // TODO this was different !!
        Core.addWeighted(patch, 1, noise, 1, -delta, patch);

        //           if( backgroundMin != backgroundMax )
        //               addWeighted(patch, 1, noise, 1, -delta, patch);
        //           else
        //           {
        //               for( int i = 0; i < patchSize.height; i++ )
        //               {
        //                   uchar* prow = patch.ptr<uchar>(i);
        //                   const uchar* nrow =  noise.ptr<uchar>(i);
        //                   for( int j = 0; j < patchSize.width; j++ )
        //                       if( prow[j] != backgroundMin )
        //                           prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta);
        //               }
        //           }
    }
}

From source file:com.trandi.opentld.tld.PatchGenerator.java

License:Apache License

/**
 * /*w  w  w .  j a  v a2  s .c  o  m*/
 * @param srcCenter
 * @param dstCenter
 * @param transform OUTPUT
 * @param inverse
 */
private void generateRandomTransform(Point srcCenter, Point dstCenter, Mat transform, boolean inverse) {
    MatOfDouble tempRand = new MatOfDouble(0d, 0d);
    Core.randu(tempRand, lambdaMin, lambdaMax);
    final double[] rands = tempRand.toArray();
    final double lambda1 = rands[0];
    final double lambda2 = rands[1];
    Core.randu(tempRand, thetaMin, thetaMax);
    final double theta = tempRand.toArray()[0];
    Core.randu(tempRand, phiMin, phiMax);
    final double phi = tempRand.toArray()[0];

    // Calculate random parameterized affine transformation A,
    // A = T(patch center) * R(theta) * R(phi)' * S(lambda1, lambda2) * R(phi) * T(-pt)
    final double st = Math.sin(theta);
    final double ct = Math.cos(theta);
    final double sp = Math.sin(phi);
    final double cp = Math.cos(phi);
    final double c2p = cp * cp;
    final double s2p = sp * sp;

    final double A = lambda1 * c2p + lambda2 * s2p;
    final double B = (lambda2 - lambda1) * sp * cp;
    final double C = lambda1 * s2p + lambda2 * c2p;

    final double Ax_plus_By = A * srcCenter.x + B * srcCenter.y;
    final double Bx_plus_Cy = B * srcCenter.x + C * srcCenter.y;

    transform.create(2, 3, CvType.CV_64F);
    transform.put(0, 0, A * ct - B * st);
    transform.put(0, 1, B * ct - C * st);
    transform.put(0, 2, -ct * Ax_plus_By + st * Bx_plus_Cy + dstCenter.x);
    transform.put(1, 0, A * st + B * ct);
    transform.put(1, 1, B * st + C * ct);
    transform.put(1, 2, -st * Ax_plus_By - ct * Bx_plus_Cy + dstCenter.y);

    if (inverse) {
        Imgproc.invertAffineTransform(transform, transform);
    }
}

From source file:LetsStart.GUI.java

private void addNoise() {
    image = originalImage.clone();/* ww w. j a  va  2s  .co  m*/
    Mat grayRnd = new Mat(image.rows(), image.cols(), image.type());
    double noise = 128;
    grayRnd.setTo(new Scalar(noise / 2, noise / 2, noise / 2));
    Core.subtract(image, grayRnd, image);
    Core.randu(grayRnd, 0, noise);
    Core.add(image, grayRnd, image);
    processOperation();
    updateView();
}

From source file:syncleus.dann.data.video.PatchGenerator.java

License:Apache License

/**
 * /*  w w w. jav a 2  s  . co m*/
 * @param image
 * @param T
 * @param patch OUTPUT
 * @param patchSize
 */
void generate(final Mat image, final Mat T, Mat patch, Size patchSize, final RNG rng) {
    patch.create(patchSize, image.type());
    if (backgroundMin != backgroundMax) {
        Core.randu(patch, backgroundMin, backgroundMax);
        // TODO if that null scalar OK or should it be new Scalar(0) ?
        Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_TRANSPARENT, null);
    } else {
        Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT,
                new Scalar(backgroundMin));
    }

    int ksize = randomBlur ? rng.nextInt() % 9 - 5 : 0;
    if (ksize > 0) {
        ksize = ksize * 2 + 1;
        Imgproc.GaussianBlur(patch, patch, new Size(ksize, ksize), 0, 0);
    }

    if (noiseRange > 0) {
        final Mat noise = new Mat(patchSize, image.type());
        int delta = (image.depth() == CvType.CV_8U ? 128 : (image.depth() == CvType.CV_16U ? 32768 : 0));
        Core.randn(noise, delta, noiseRange);

        // TODO this was different !!
        Core.addWeighted(patch, 1, noise, 1, -delta, patch);

        //           if( backgroundMin != backgroundMax )
        //               addWeighted(patch, 1, noise, 1, -delta, patch);
        //           else
        //           {
        //               for( int i = 0; i < patchSize.height; i++ )
        //               {
        //                   uchar* prow = patch.ptr<uchar>(i);
        //                   const uchar* nrow =  noise.ptr<uchar>(i);
        //                   for( int j = 0; j < patchSize.width; j++ )
        //                       if( prow[j] != backgroundMin )
        //                           prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta);
        //               }
        //           }
    }
}