Example usage for org.opencv.core Mat Mat

List of usage examples for org.opencv.core Mat Mat

Introduction

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

Prototype

public Mat(Mat m, Rect roi) 

Source Link

Usage

From source file:com.mycompany.mavenproject1.GenericResource.java

/**
 * Retrieves representation of an instance of com.mycompany.mavenproject1.GenericResource
 * @return an instance of java.lang.String
 *//*  www . j  a va2 s.c om*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getXml() {
    //TODO return proper representation object
    JsonBuilderFactory factory = Json.createBuilderFactory(null);

    String filename = "D:\\1Downloads\\Firefox downloads\\stock.jpg";
    Mat img;
    img = Imgcodecs.imread(filename);
    int width = img.width();
    int height = img.height();
    System.out.println("w= " + width + "  h= " + height);

    Analyzer analyzer = new Analyzer(filename);
    analyzer.setFrame(0, 0, width, height);

    Individual ind = new Individual(analyzer);
    ind.generateIndividual(width, height);
    System.out.println("\n\nTHE RESULT: " + analyzer.calcCombinedAestheticScore());

    Population myPop = new Population(200, true, width, height, analyzer);

    long startTime = System.currentTimeMillis();

    int generationCount = 0;
    while (generationCount < 500) {
        generationCount++;
        System.out.println(
                "Generation: " + generationCount + " Fittest: " + myPop.getFittest().getAestheticScore());
        //myPop = CropAlgorithm.evolvePopulation(myPop);
        CropAlgorithm cropAlg = new CropAlgorithm(analyzer);
        myPop = cropAlg.evolvePopulation(myPop, width, height);
        System.out.println("-------------------------------------------------ACTUAL BEST:  "
                + myPop.getFittest().getAestheticScore() + "    " + myPop.getFittest().toString());
    }
    System.out.println("Solution found!");
    System.out.println("Generation: " + generationCount);
    System.out.println("Genes:");
    Individual fittest = myPop.getFittest();
    System.out.println(fittest);
    double score = fittest.getAestheticScore();
    System.out.println(fittest.getAestheticScore());

    int x = (int) fittest.getX();
    int y = (int) fittest.getY();
    int w = (int) fittest.getWidth();
    int h = (int) fittest.getHeight();

    Rect roi = new Rect(x, y, w, h);
    Mat cropped = new Mat(img, roi);

    Imgcodecs.imwrite("D:\\1Downloads\\Firefox downloads\\cropped\\torocko.jpg", cropped);

    long estimatedTime = System.currentTimeMillis() - startTime;
    System.out.println("IDOOO==== " + estimatedTime);

    JsonObject value = factory.createObjectBuilder().add("value", score).add("x", x).add("y", y).add("width", w)
            .add("height", h).build();

    System.out.println("-------------------------------");

    return value;
}

From source file:com.orange.documentare.core.image.Binarization.java

License:Open Source License

public static Mat getFrom(Mat mat) {
    Mat greyscaleMat = isGreyscale(mat) ? mat : getGreyscaleImage(mat);
    Mat binaryMat = new Mat(greyscaleMat.size(), CvType.CV_8UC1);
    Imgproc.adaptiveThreshold(greyscaleMat, binaryMat, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
            Imgproc.THRESH_BINARY, ADAPTIVE_BLOCK_SIZE, ADAPTIVE_MEAN_ADJUSTMENT);
    return binaryMat;
}

From source file:com.orange.documentare.core.image.segmentation.ImageSegmentationBuilder.java

License:Open Source License

private byte[] getBytesFor(SegmentationRect rect) {
    Mat crop = new Mat(binaryImageMat, new Rect(rect.x(), rect.y(), rect.width(), rect.height()));
    return OpenCvImage.matToRaw(crop);
}

From source file:com.superbool.easylpr.model.Transformation.java

public Mat crop(Size outputImageSize, Mat transformationMatrix) {

    Mat deskewed = new Mat(outputImageSize, this.bigImage.type());

    // Apply perspective transformation to the image
    Imgproc.warpPerspective(this.bigImage, deskewed, transformationMatrix, deskewed.size(),
            Imgproc.INTER_CUBIC);/*from   w ww  . ja  v a2 s .  co m*/

    return deskewed;
}

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

License:Apache License

/**
 * @return real similarities errors/*from  www  .  j  a v a2s  .c om*/
 */
private float[] normCrossCorrelation(final Mat lastImg, final Mat currentImg, final Point[] lastPoints,
        final Point[] currentPoints, final byte[] status) {
    final float[] similarity = new float[lastPoints.length];

    final Mat lastPatch = new Mat(CROSS_CORR_PATCH_SIZE, CvType.CV_8U);
    final Mat currentPatch = new Mat(CROSS_CORR_PATCH_SIZE, CvType.CV_8U);
    final Mat res = new Mat(new Size(1, 1), CvType.CV_32F);

    for (int i = 0; i < lastPoints.length; i++) {
        if (status[i] == 1) {
            Imgproc.getRectSubPix(lastImg, CROSS_CORR_PATCH_SIZE, lastPoints[i], lastPatch);
            Imgproc.getRectSubPix(currentImg, CROSS_CORR_PATCH_SIZE, currentPoints[i], currentPatch);
            Imgproc.matchTemplate(lastPatch, currentPatch, res, Imgproc.TM_CCOEFF_NORMED);

            similarity[i] = Util.getFloat(0, 0, res);
        } else {
            similarity[i] = 0f;
        }
    }

    return similarity;
}

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

License:Apache License

/**
 * /*from  w ww  .j  av  a  2s .c om*/
 * @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.wallerlab.compcellscope.MultiModeViewActivity.java

License:BSD License

public Mat generateMMFrame(Mat gridOut, Mat MatTL, Mat MatTR, Mat MatBL, Mat MatBR) {
    //gridOut = new Mat(100, 100, gridOut.type(), new Scalar(0,0,0));
    Mat Mat1 = new Mat(MatTL.size(), MatTL.type());
    Mat Mat2 = new Mat(MatTR.size(), MatTR.type());
    Mat Mat3 = new Mat(MatBL.size(), MatBL.type());
    Mat Mat4 = new Mat(MatBR.size(), MatBR.type());

    // Ensure all of the mats are of the correct size since pyramid operation resizes
    Imgproc.resize(MatTL, MatTL, sz);// w ww .j a  v  a  2  s . co  m
    Imgproc.resize(MatTR, MatTR, sz);
    Imgproc.resize(MatBL, MatBL, sz);
    Imgproc.resize(MatBR, MatBR, sz);

    // Downsample by 2 for 2x2 grid
    Imgproc.pyrDown(MatBL, Mat1);
    Imgproc.pyrDown(MatBR, Mat2);
    Imgproc.pyrDown(MatTL, Mat3);
    Imgproc.pyrDown(MatTR, Mat4);

    /*
    Log.d(TAG,String.format("TLRect format is %.1f-%.1f",TLRect.size().width,TLRect.size().height));
    Log.d(TAG,String.format("TRRect format is %.1f-%.1f",TRRect.size().width,TRRect.size().height));
            
    Log.d(TAG,String.format("BLRect format is %.1f-%.1f",BLRect.size().width,BLRect.size().height));
    Log.d(TAG,String.format("BRRect format is %.1f-%.1f",BRRect.size().width,BRRect.size().height));
            
    Log.d(TAG,String.format("MatTL format is %.1f-%.1f",MatTL.size().width,MatTL.size().height));
    Log.d(TAG,String.format("MatTR format is %.1f-%.1f",MatTR.size().width,MatTR.size().height));
            
    Log.d(TAG,String.format("MatBL format is %.1f-%.1f",MatBL.size().width,MatBL.size().height));
    Log.d(TAG,String.format("MatBR format is %.1f-%.1f",MatBR.size().width,MatBR.size().height));
     */

    Core.putText(Mat1, "DPC-LR", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0));
    Core.putText(Mat2, "DPC-TB", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0));
    Core.putText(Mat3, "BrightField", new Point(33, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0));
    Core.putText(Mat4, "DarkField", new Point(37, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0));

    Mat1.copyTo(gridOut.submat(BLRect));
    Mat2.copyTo(gridOut.submat(BRRect));
    Mat3.copyTo(gridOut.submat(TLRect));
    Mat4.copyTo(gridOut.submat(TRRect));

    Mat1.release();
    Mat2.release();
    Mat3.release();
    Mat4.release();

    return gridOut;
}

From source file:com.wallerlab.processing.utilities.ImageUtils.java

License:BSD License

public static Mat circularShift(Mat mat, int x, int y) {
    int w = mat.cols();
    int h = mat.rows();
    Mat result = Mat.zeros(h, w, CvType.CV_32FC4);

    int shiftR = x % w;
    int shiftD = y % h;
    //java modulus gives negative results for negative numbers
    if (shiftR < 0)
        shiftR += w;/*from  w w w  .j a  va  2 s  .c  o m*/
    if (shiftD < 0)
        shiftD += h;

    /* extract 4 submatrices
              |---| shiftR
     ______________
    |         |   |
    |    1    | 2 |
    |_________|___|  ___ shiftD
    |         |   |   |
    |    3    | 4 |   |
    |         |   |   |
    |_________|___|  _|_
     */
    Mat shift1 = mat.submat(0, h - shiftD, 0, w - shiftR);
    Mat shift2 = mat.submat(0, h - shiftD, w - shiftR, w);
    Mat shift3 = mat.submat(h - shiftD, h, 0, w - shiftR);
    Mat shift4 = mat.submat(h - shiftD, h, w - shiftR, w);

    /* and rearrange
     ______________
    |   |         |
    | 4 |    3    |
    |   |         |
    |___|_________|
    |   |         |
    | 2 |    1    |
    |___|_________|
     */
    shift1.copyTo(new Mat(result, new Rect(shiftR, shiftD, w - shiftR, h - shiftD)));
    shift2.copyTo(new Mat(result, new Rect(0, shiftD, shiftR, h - shiftD)));
    shift3.copyTo(new Mat(result, new Rect(shiftR, 0, w - shiftR, shiftD)));
    shift4.copyTo(new Mat(result, new Rect(0, 0, shiftR, shiftD)));

    return result;
}

From source file:cpsd.ImageGUI.java

private void blurImage() {
    Mat source = ImageClass.getInstance().getImage();
    Mat destination = new Mat(source.size(), source.type());
    Imgproc.GaussianBlur(source, destination, new org.opencv.core.Size(0, 0), 2);
    // Imgproc.bilateralFilter(source, destination, , sensorSize, sensorSize, WIDTH);
    ImageClass.getInstance().setImage(destination);
}

From source file:ctPrincipal.Graficos.java

public String desvioPadrao() {
    String resultImgOutput = "";
    double[] tmp = new double[arquivos.length];

    for (String s : arquivos) {
        images.add(LoadImagB(s));//from  w  ww  .j a  v a2  s.com
    }

    double[][] stdDev = new double[images.get(0).rows()][images.get(0).cols()];

    for (int i = 0; i < images.get(0).rows(); i++) {
        for (int j = 0; j < images.get(0).cols(); j++) {
            for (int k = 0; k < arquivos.length; k++) {
                tmp[k] = images.get(k).get(i, j)[0];
            }
            double tmpDev = Math.sqrt(somatorio(tmp) / arquivos.length);
            stdDev[i][j] = tmpDev;
        }
    }

    ruido = new Mat(new Size((int) images.get(0).cols(), (int) images.get(0).rows()), CvType.CV_8UC1);

    double[][] d = normalizacao(stdDev);

    Imgcodecs.imwrite("OutputImg/ruido.jpg", ruido);
    return resultImgOutput = "OutputImg/ruido.jpg";
}