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, Range rowRange, Range colRange) 

Source Link

Usage

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public void init(int w, int h) {
    width = w;/* www.  j av a2 s  . c o  m*/
    height = h;
    welcome();
    setupWorkingImages();

    matR = new Mat(height, width, CvType.CV_8UC1);
    matG = new Mat(height, width, CvType.CV_8UC1);
    matB = new Mat(height, width, CvType.CV_8UC1);
    matA = new Mat(height, width, CvType.CV_8UC1);
    matGray = new Mat(height, width, CvType.CV_8UC1);

    matBGRA = new Mat(height, width, CvType.CV_8UC4);
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public static Mat imitate(Mat m) {
    return new Mat(m.height(), m.width(), m.type());
}

From source file:imageprocess.HistogramProcessor.java

public static Mat stretch(Mat image, int minValue) {
    // Compute histogram first
    Mat hist = getGrayHistogram(image);//from   www.  j  a v a2 s  .  c o m

    // find left extremity of the histogram
    int imin = 0;
    for (; imin < 256; imin++) {
        System.out.println(String.format("[%d] = %f", imin, hist.get(imin, 0)[0]));
        if (hist.get(imin, 0)[0] > minValue) {
            break;
        }
    }
    // find right extremity of the histogram
    int imax = 255;
    for (; imax >= 0; imax--) {
        if (hist.get(imax, 0)[0] > minValue) {
            break;
        }
    }

    // Create lookup table
    Mat lookup = new Mat(256, 1, CV_8U);

    for (int i = 0; i < 256; i++) {
        if (i < imin) {
            lookup.put(i, 0, 0);
        } else if (i > imax) {
            lookup.put(i, 0, 255);
        } else {
            lookup.put(i, 0, 255.0 * (i - imin) / (imax - imin) + 0.5);
        }
    }
    // Apply lookup table
    Mat result;
    result = applyLookUp(image, lookup);

    return result;
}

From source file:imageprocess.HistogramProcessor.java

public static Mat applyLookUp(Mat image, Mat lookup) {
    // Set output image (always 1-channel)
    Mat result = new Mat(image.rows(), image.cols(), CV_8U);

    //        for (int i = 0; i < image.cols(); i++) {
    //            for (int j = 0; j < image.rows(); j++) {
    //                double[] data = image.get(j, i);
    //                double newIntensity = lookup.get((int)data[0], 0)[0];
    //                result.put(j, i, newIntensity);
    //            }
    //        }/*  www  . j a  v  a 2 s  .  c  o  m*/
    Core.LUT(image, lookup, result);
    return result;
}

From source file:in.fabinpaul.sixthsense.ColorBlobDetectionFragment.java

License:Apache License

@Override
public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    for (int i = 0; i < 4; i++) {
        mDetector[i] = new ColorBlobDetector();
        mBlobColorRgba[i] = new Scalar(255);
        mBlobColorHsv[i] = new Scalar(255);
    }/*  w  ww.j  ava2s . c  o m*/
    CONTOUR_COLOR = new Scalar(255, 0, 0, 255);
    getActivity().invalidateOptionsMenu();

}

From source file:io.appium.java_client.ScreenshotState.java

License:Apache License

private static Mat prepareImageForComparison(BufferedImage srcImage) {
    final BufferedImage normalizedBitmap = new BufferedImage(srcImage.getWidth(), srcImage.getHeight(),
            BufferedImage.TYPE_3BYTE_BGR);
    final Graphics2D g = normalizedBitmap.createGraphics();
    try {/* ww w.j  a  v  a2 s.  c  o m*/
        g.setComposite(AlphaComposite.Src);
        g.drawImage(srcImage, 0, 0, null);
    } finally {
        g.dispose();
    }
    final byte[] pixels = ((DataBufferByte) normalizedBitmap.getRaster().getDataBuffer()).getData();
    final Mat result = new Mat(normalizedBitmap.getHeight(), normalizedBitmap.getWidth(), CvType.CV_8UC3);
    result.put(0, 0, pixels);
    return result;
}

From source file:io.appium.java_client.ScreenshotState.java

License:Apache License

/**
 * Compares two valid java bitmaps and calculates similarity score between them.
 *
 * @param refImage   reference image//from  ww w  .  j  a  v a  2s.  c o  m
 * @param tplImage   template
 * @param resizeMode one of possible enum values. Set it either to <em>TEMPLATE_TO_REFERENCE_RESOLUTION</em> or
 *                   <em>REFERENCE_TO_TEMPLATE_RESOLUTION</em> if given bitmaps have different dimensions
 * @return similarity score value in range (-1.0, 1.0). 1.0 is returned if the images are equal
 * @throws ScreenshotComparisonError if provided images are not valid or have
 *                                   different resolution, but resizeMode has been set to <em>NO_RESIZE</em>
 */
public static double getOverlapScore(BufferedImage refImage, BufferedImage tplImage, ResizeMode resizeMode) {
    Mat ref = prepareImageForComparison(refImage);
    if (ref.empty()) {
        throw new ScreenshotComparisonError("Reference image cannot be converted for further comparison");
    }
    Mat tpl = prepareImageForComparison(tplImage);
    if (tpl.empty()) {
        throw new ScreenshotComparisonError("Template image cannot be converted for further comparison");
    }
    switch (resizeMode) {
    case TEMPLATE_TO_REFERENCE_RESOLUTION:
        tpl = resizeFirstMatrixToSecondMatrixResolution(tpl, ref);
        break;
    case REFERENCE_TO_TEMPLATE_RESOLUTION:
        ref = resizeFirstMatrixToSecondMatrixResolution(ref, tpl);
        break;
    default:
        // do nothing
    }

    if (ref.width() != tpl.width() || ref.height() != tpl.height()) {
        throw new ScreenshotComparisonError(
                "Resolutions of template and reference images are expected to be equal. "
                        + "Try different resizeMode value.");
    }

    Mat res = new Mat(ref.rows() - tpl.rows() + 1, ref.cols() - tpl.cols() + 1, CvType.CV_32FC1);
    Imgproc.matchTemplate(ref, tpl, res, Imgproc.TM_CCOEFF_NORMED);
    return Core.minMaxLoc(res).maxVal;
}

From source file:it.baywaylabs.jumpersumo.FrameDisplayCV.java

License:Open Source License

@Override
protected Bitmap doInBackground(Void... params) {

    if (bitmapOriginal != null) {
        this.imgMAT = new Mat(bitmapOriginal.getWidth(), bitmapOriginal.getHeight(), CvType.CV_8UC4);
        try {/*from  w  w  w.  j  a  v a2 s .c  o  m*/
            zxing();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (FormatException e) {
            e.printStackTrace();
        }
        Bitmap bitmapTranform = Bitmap.createBitmap(this.imgMAT.width(), this.imgMAT.height(),
                Bitmap.Config.ARGB_8888);
        //bitmapOriginal = Bitmap.createBitmap(imgMAT.width(), imgMAT.height(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(this.imgMAT, bitmapTranform);

        // return bitmapTranform;
    }

    return bitmapOriginal;
}

From source file:javafx1.JavaFX1.java

public Image bildLaden() {
        Image zwischenBild = null;

        try {//from  ww w. j av a 2s  .  c o  m
            File input = new File("D:/_piCam/bild.jpg");
            //FileInputStream bi = ImageIO.read(input);
            BufferedImage bi = ImageIO.read(input);

            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);
            mat.put(0, 0, data);

            Mat bild = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC1);
            Imgproc.cvtColor(mat, bild, Imgproc.COLOR_BGR2GRAY);

            byte[] data1 = new byte[bild.rows() * bild.cols() * (int) (bild.elemSize())];
            bild.get(0, 0, data1);
            BufferedImage image1 = new BufferedImage(bild.cols(), bild.rows(), BufferedImage.TYPE_BYTE_GRAY);
            image1.getRaster().setDataElements(0, 0, bild.cols(), bild.rows(), data1);

            File ouptut = new File("D:/xml/grayscale2.jpg");
            //ImageIO.write(image1, "jpg", ouptut);
            BufferedImage gray = image1.getSubimage(0, 0, image1.getTileWidth(), image1.getHeight());
            zwischenBild = SwingFXUtils.toFXImage(gray, null);

        } catch (IOException ex) {
            System.out.println("Fehler beim Bild laden...");
        }
        return zwischenBild;
    }

From source file:javafx1.JavaFX1.java

private Image analyzePic(BufferedImage bi) {
        //System.out.println("height:" + bi.getHeight() + ", width:" + bi.getWidth());
        //bi = bi.getSubimage(100, 100, 50, 50);
        // Originalbild nach MAT
        byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
        Mat matOrigin = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);
        matOrigin.put(0, 0, data);// www.  ja  v  a 2  s  .c o m
        //+Core.addWeighted();
        //+Imgproc.threshold(matOrigin, matOrigin, thresh, maxval, type)
        //+Core.bitwise_xor(matOrigin, matOrigin, matOrigin);

        // fr graubild
        Mat matManipulate = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC1);
        // in Graubild wandeln von...nach
        // Imgproc.cvtColor(matOrigin, matManipulate, Imgproc.COLOR_BGR2GRAY);
        // Imgproc.blur(matManipulate, matManipulate, new Size(3, 3));
        matManipulate = doCanny(matOrigin);
        //aus Mat in BufferedImagae zurckwandeln
        //  byte[] data1 = new byte[matManipulate.rows() * matManipulate.cols() * (int) (matManipulate.elemSize())];
        //  matManipulate.get(0, 0, data1);
        //  BufferedImage biManipulate = new BufferedImage(matManipulate.cols(), matManipulate.rows(), BufferedImage.TYPE_BYTE_GRAY);
        //  biManipulate.getRaster().setDataElements(0, 0, matManipulate.cols(), matManipulate.rows(), data1);
        // BufferedImage gray = image1.getSubimage(0, 0, image1.getTileWidth(), image1.getHeight());
        //return biManipulate;
        matManipulate = doBackgroundRemoval(matManipulate);
        Rect r = new Rect(230, 10, 55, 40);
        Mat m = new Mat(matOrigin, r);
        //return mat2Image(matOrigin);
        return mat2Image(m);

    }