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:classes.TextExtractor.java

public void extractText(Rect roi, double roiAngle) throws Exception {

    Point roiTopLeft = roi.tl();/*from w  w  w .  j  av  a2  s .  c  o  m*/

    double radians = Math.toRadians(roiAngle);
    double sin = Math.abs(Math.sin(radians));
    double cos = Math.abs(Math.cos(radians));

    int newWidth = (int) (image.width() * cos + image.height() * sin);
    int newHeight = (int) (image.width() * sin + image.height() * cos);

    int[] newWidthHeight = { newWidth, newHeight };

    int pivotX = newWidthHeight[0] / 2;
    int pivotY = newWidthHeight[1] / 2;

    Point center = new Point(pivotX, pivotY);

    Size targetSize = new Size(newWidthHeight[0], newWidthHeight[1]);

    Mat intermediateImage = new Mat(targetSize, image.type());

    int offsetX = (newWidthHeight[0] - image.width()) / 2;
    int offsetY = (newWidthHeight[1] - image.height()) / 2;

    Point paddedTopLeft = new Point(roiTopLeft.x + offsetX, roiTopLeft.y + offsetY);

    Mat containerImage = intermediateImage.submat(offsetY, offsetY + image.height(), offsetX,
            offsetX + image.width());
    image.copyTo(containerImage);

    Mat rotationMatrix = Imgproc.getRotationMatrix2D(center, roiAngle, 1.0);

    Point transformedTopLeft = transformPoint(paddedTopLeft, rotationMatrix);

    Mat rotatedImage = new Mat();
    Imgproc.warpAffine(intermediateImage, rotatedImage, rotationMatrix, targetSize, Imgproc.INTER_LINEAR,
            Imgproc.BORDER_CONSTANT, new Scalar(0));

    ImageUtils.saveImage(rotatedImage, imageID + "_rotatedImage.png", request);

    double adjustedWidth = roi.size().width;
    double adjustedHeight = roi.size().height;

    if (transformedTopLeft.x + adjustedWidth > rotatedImage.width()) {
        adjustedWidth = rotatedImage.width() - transformedTopLeft.x;
    }

    if (transformedTopLeft.y + adjustedHeight > rotatedImage.height()) {
        adjustedHeight = rotatedImage.height() - transformedTopLeft.y;
    }

    Rect newROI = new Rect(transformedTopLeft, new Size(adjustedWidth, adjustedHeight));

    Mat extractedROI = new Mat(rotatedImage, newROI);

    String fileName = ImageUtils.saveImage(extractedROI, imageID + "_ROI.png", request);

    extractText(fileName);
}

From source file:classes.TextRecognitionPreparer.java

private static Mat reduceColor(Mat image, int div) {

    Mat result = new Mat(image.size(), image.type());

    int rows = image.rows(); // number of lines
    int cols = image.cols(); // number of elements per line

    for (int j = 0; j < rows; j++) {

        for (int i = 0; i < cols; i++) {

            double[] data = image.get(j, i);

            for (int k = 0; k < 3; k++) {
                data[k] = ((int) data[k] / div) * div + div / 2;

            }/*ww  w  .j a  v a  2s .  c om*/

            int put = result.put(j, i, data);

        }
    }

    return result;
}

From source file:com.carver.paul.truesight.ImageRecognition.RecognitionModel.java

License:Open Source License

private static Mat calculateHeroImageFromPhoto(HeroLine line, Mat backgroundImage) {
    if (line.isRealLine == false) {
        return makeFakeHeroFromPhoto(backgroundImage);
    }//w  w w.jav a 2 s .  com

    final double rationHeightToWidthbeforeCuts = 1.8;

    final double ratioToCutFromSide = 0.05;
    final double ratioToCutFromTop = 0.05;
    // ratioToCutFromBottom may need to be larger because a red box with MMR at the bottom may obscure the image
    final double ratioToCutFromBottom = 0.05;

    double heightWithoutCuts = line.rect.width() / rationHeightToWidthbeforeCuts;
    int left = line.rect.left + (int) (line.rect.width() * ratioToCutFromSide);
    int width = line.rect.width() - (int) (line.rect.width() * 2 * ratioToCutFromSide);
    int top = line.rect.top + line.rect.height() + (int) (heightWithoutCuts * ratioToCutFromTop);
    int finalHeight = (int) heightWithoutCuts - (int) (heightWithoutCuts * ratioToCutFromBottom);

    Log.d("AA", "width " + width + ", height" + finalHeight);

    if (left + width > backgroundImage.width())
        width = backgroundImage.width() - left;
    if (top + finalHeight > backgroundImage.height())
        finalHeight = backgroundImage.height() - top;

    if (left < 0)
        left = 0;
    if (top < 0)
        top = 0;

    if (left > backgroundImage.width() || top > backgroundImage.height()) {
        return makeFakeHeroFromPhoto(backgroundImage);
    }

    Rect rect = new Rect(left, top, width, finalHeight);
    return new Mat(backgroundImage, rect);
}

From source file:com.carver.paul.truesight.ImageRecognition.RecognitionModel.java

License:Open Source License

private static Mat makeFakeHeroFromPhoto(Mat backgroundImage) {
    int width = 26;
    int height = 15;//(int) (width / rationHeightToWidthbeforeCuts);
    Rect rect = new Rect(0, 0, width, height);

    return new Mat(backgroundImage, rect);
}

From source file:com.example.yannic.remotefacedetection.agent.FaceDetectionAgent.java

License:Open Source License

@Override
public Tuple2Future<List<Integer>, byte[]> getFrame(int id, byte[] input) {
    int count = 1;

    long startTime = System.currentTimeMillis();

    Tuple2Future<List<Integer>, byte[]> fut = new Tuple2Future<List<Integer>, byte[]>();

    List<Integer> rectData = new ArrayList<Integer>();
    rectData.add(id);//from   w  w w . j  a  va2 s  .  c o  m
    byte[] thumbnail = new byte[0];
    BufferedImage bi;
    int label = 0;
    try {

        bi = ImageIO.read(new ByteArrayInputStream(input));

        Mat mat = bufferedImageToMat(bi);

        MatOfRect faces = new MatOfRect();
        if (mCascadeClassifier != null) {
            mCascadeClassifier.detectMultiScale(mat, faces, 1.1, 2, 2, new Size(100, 100), new Size());

        }
        System.out.println("face detection was called id: " + id);

        Rect[] facesArray = faces.toArray();

        for (Rect face : facesArray) {

            // testRect = testRect + "height: " + face.height;
            // testRect = testRect + "width: " + face.width;
            // testRect = testRect + "x: " + face.x;
            // testRect = testRect + "y: " + face.y;
            //
            //
            rectData.add(face.x);
            rectData.add(face.y);
            rectData.add(face.width);
            rectData.add(face.height);

        }

        fut.setFirstResult(rectData);
        long fendTime = System.currentTimeMillis();
        System.out.println("Dauer first Result: " + (fendTime - startTime) + " milliseconds");
        // for(Rect face : facesArray){
        /*
         * prep for recognition
         */
        if (facesArray.length > 0) {

            for (int i = 0; i < facesArray.length; i++) {

                Rect face = facesArray[i];
                Mat cropImg = new Mat(mat, face);
                Mat resizedImg = new Mat();
                Size size = new Size(92, 112);
                Imgproc.resize(cropImg, resizedImg, size);
                bi = matToBufferedImage(resizedImg, bi);
                ImageIO.write(bi, "jpg", new File("C:\\inputPictures\\(" + count + ")-test.jpg"));

                label = mFaceRecognizer.recognizeFace("C:\\inputPictures\\(" + count + ")-test.jpg");
                count++;

                // }
                if (label > 0) {

                    byte[] tempface = Files
                            .readAllBytes(new File("C:\\trainingPictures\\(" + label + ")-test.jpg").toPath());
                    byte[] combined = new byte[tempface.length + thumbnail.length];

                    for (int x = 0; x < combined.length; x++) {
                        combined[x] = x < thumbnail.length ? thumbnail[x] : tempface[x - thumbnail.length];
                    }

                    thumbnail = combined;
                    // System.out.println("bytes: " + thumbnail.length);
                }
            }
        }
        // zeichnet rects ein
        /*
         * for (int i = 0; i < facesArray.length; i++)
         * Imgproc.rectangle(mat, facesArray[i].tl(), facesArray[i].br(),
         * new Scalar(100), 3);
         * 
         */

        long endTime = System.currentTimeMillis();
        System.out.println("Dauer second Result: " + (endTime - startTime) + " milliseconds");

        fut.setSecondResult(thumbnail);

        return fut;
    } catch (IOException e) {
        System.out.println("IOException beim umwandeln des byteArray");
    }

    long endTime = System.currentTimeMillis();
    System.out.println("Dauer second Result: " + (endTime - startTime) + " milliseconds");
    return fut;
}

From source file:com.ibm.streamsx.edgevideo.device.FaceDetector.java

License:Open Source License

public List<Mat> extractFaces(Mat rgbFrame, MatOfRect faceRects) {
    List<Mat> faces = new ArrayList<>();
    for (Rect faceRect : faceRects.toArray()) {
        Mat rgbFaceFrame = new Mat(rgbFrame, faceRect);
        rgbFaceFrame = rgbFaceFrame.clone(); // don't inherit subsequent changes to rgbFrame
        faces.add(rgbFaceFrame);//from  w ww.ja v  a 2  s  .c o  m
    }
    return faces;
}

From source file:com.jeremydyer.nifi.CropImageProcessor.java

License:Apache License

final public void cropImage(final ProcessSession session, ProcessContext context, FlowFile original,
        final Mat image) {

    final int x = context.getProperty(X_POINT).evaluateAttributeExpressions(original).asInteger();
    final int y = context.getProperty(Y_POINT).evaluateAttributeExpressions(original).asInteger();
    final int width = context.getProperty(CROP_WIDTH).evaluateAttributeExpressions(original).asInteger();
    final int height = context.getProperty(CROP_HEIGHT).evaluateAttributeExpressions(original).asInteger();

    FlowFile croppedImage = session.write(session.create(original), new OutputStreamCallback() {
        @Override/*from w ww.  j  a v a2  s  .  c  om*/
        public void process(OutputStream outputStream) throws IOException {
            Rect rectCrop = new Rect(x, y, width, height);
            Mat croppedImage = new Mat(image, rectCrop);

            MatOfByte updatedImage = new MatOfByte();
            Imgcodecs.imencode(".jpg", croppedImage, updatedImage);
            outputStream.write(updatedImage.toArray());
        }
    });

    Map<String, String> atts = new HashMap<>();
    atts.put("image.width", new Integer(width).toString());
    atts.put("image.height", new Integer(height).toString());

    croppedImage = session.putAllAttributes(croppedImage, atts);

    session.transfer(croppedImage, REL_CROPPED_IMAGE);
}

From source file:com.jeremydyer.nifi.ObjectDetectionProcessor.java

License:Apache License

final public Mat detectObjects(final ProcessSession session, FlowFile original, final JSONObject dd,
        final Mat image) {

    CascadeClassifier objectDetector = new CascadeClassifier(dd.getString("opencv_xml_cascade_path"));
    MatOfRect objectDetections = new MatOfRect();
    objectDetector.detectMultiScale(image, objectDetections);
    //getLogger().error("Detected " + objectDetections.toArray().length + " " + dd.getString("name") + " objects in the input flowfile");

    final AtomicReference<Mat> croppedImageReference = new AtomicReference<>();

    int counter = 0;
    for (int i = 0; i < objectDetections.toArray().length; i++) {
        final Rect rect = objectDetections.toArray()[i];
        FlowFile detection = session.write(session.create(original), new OutputStreamCallback() {
            @Override//from w w w.j  a va2  s .c o m
            public void process(OutputStream outputStream) throws IOException {

                Mat croppedImage = null;

                //Should the image be cropped? If so there is no need to draw bounds because that would be the same as the cropping
                if (dd.getBoolean("crop")) {
                    Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
                    croppedImage = new Mat(image, rectCrop);
                    MatOfByte updatedImage = new MatOfByte();
                    Imgcodecs.imencode(".jpg", croppedImage, updatedImage);
                    croppedImageReference.set(croppedImage);
                    outputStream.write(updatedImage.toArray());
                } else {
                    //Should the image have a border drawn around it?
                    if (dd.getBoolean("drawBounds")) {
                        Mat imageWithBorder = image.clone();
                        Imgproc.rectangle(imageWithBorder, new Point(rect.x, rect.y),
                                new Point(rect.x + rect.width, rect.y + rect.height),
                                new Scalar(255, 255, 255));
                        MatOfByte updatedImage = new MatOfByte();
                        Imgcodecs.imencode(".jpg", imageWithBorder, updatedImage);
                        outputStream.write(updatedImage.toArray());
                    } else {
                        MatOfByte updatedImage = new MatOfByte();
                        Imgcodecs.imencode(".jpg", image, updatedImage);
                        outputStream.write(updatedImage.toArray());
                    }
                }

            }
        });

        Map<String, String> atts = new HashMap<>();
        atts.put("object.detection.name", dd.getString("name"));
        atts.put("object.detection.id", new Long(System.currentTimeMillis() + counter).toString());

        counter++;

        detection = session.putAllAttributes(detection, atts);
        session.transfer(detection, REL_OBJECT_DETECTED);
    }

    Mat childResponse = null;

    if (croppedImageReference.get() != null) {
        childResponse = croppedImageReference.get();
    } else {
        childResponse = image;
    }

    if (dd.has("children")) {
        JSONArray children = dd.getJSONArray("children");
        if (children != null) {

            for (int i = 0; i < children.length(); i++) {
                JSONObject ddd = children.getJSONObject(i);
                childResponse = detectObjects(session, original, ddd, childResponse);
            }
        }
    }

    return childResponse;
}

From source file:com.kinect.FaceCapture.java

public void saveFace() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("\nRunning FaceDetector");

    CascadeClassifier faceDetector = new CascadeClassifier(
            FaceCapture.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
    Mat image = Highgui.imread("screancapture.jpg");
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    Rect rectCrop = null;/*  www .j  a  va2 s  . co m*/
    for (Rect rect : faceDetections.toArray()) {
        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                new Scalar(0, 255, 0));
        rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);
    }
    Mat image_roi = new Mat(image, rectCrop);
    Highgui.imwrite("screancapture.jpg", image_roi);

    System.out.println("save face...");
}

From source file:com.mitzuli.core.ocr.OcrPreprocessor.java

License:Open Source License

/**
 * Binarizes and cleans the input image for OCR, saving debugging images in the given directory.
 *
 * @param input the input image, which is recycled by this method, so the caller should make a defensive copy of it if necessary.
 * @param debugDir the directory to write the debugging images to, or null to disable debugging.
 * @return the preprocessed image./*from   w w  w  . j a  v a2 s.co m*/
 */
static Image preprocess(final Image input, final File debugDir) {
    // TODO Temporary workaround to allow to manually enable debugging (the global final variable should be used)
    boolean DEBUG = debugDir != null;

    // Initialization
    final Mat mat = input.toGrayscaleMat();
    final Mat debugMat = DEBUG ? input.toRgbMat() : null;
    input.recycle();
    final Mat aux = new Mat(mat.size(), CvType.CV_8UC1);
    final Mat binary = new Mat(mat.size(), CvType.CV_8UC1);
    if (DEBUG)
        Image.fromMat(mat).write(new File(debugDir, "1_input.jpg"));

    // Binarize the input image in mat through adaptive Gaussian thresholding
    Imgproc.adaptiveThreshold(mat, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 51,
            13);
    // Imgproc.adaptiveThreshold(mat, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 31, 7);

    // Edge detection
    Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_OPEN, KERNEL_3X3); // Open
    Imgproc.morphologyEx(mat, aux, Imgproc.MORPH_CLOSE, KERNEL_3X3); // Close
    Core.addWeighted(mat, 0.5, aux, 0.5, 0, mat); // Average
    Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_GRADIENT, KERNEL_3X3); // Gradient
    Imgproc.threshold(mat, mat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); // Edge map
    if (DEBUG)
        Image.fromMat(mat).write(new File(debugDir, "2_edges.jpg"));

    // Extract word level connected-components from the dilated edge map
    Imgproc.dilate(mat, mat, KERNEL_3X3);
    if (DEBUG)
        Image.fromMat(mat).write(new File(debugDir, "3_dilated_edges.jpg"));
    final List<MatOfPoint> wordCCs = new ArrayList<MatOfPoint>();
    Imgproc.findContours(mat, wordCCs, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Filter word level connected-components individually and calculate their average attributes
    final List<MatOfPoint> individuallyFilteredWordCCs = new ArrayList<MatOfPoint>();
    final List<MatOfPoint> removedWordCCs = new ArrayList<MatOfPoint>();
    double avgWidth = 0, avgHeight = 0, avgArea = 0;
    for (MatOfPoint cc : wordCCs) {
        final Rect boundingBox = Imgproc.boundingRect(cc);
        if (boundingBox.height >= 6 // bounding box height >= 6
                && boundingBox.area() >= 50 // bounding box area >= 50
                && (double) boundingBox.width / (double) boundingBox.height >= 0.25 // bounding box aspect ratio >= 1:4
                && boundingBox.width <= 0.75 * mat.width() // bounding box width <= 0.75 image width
                && boundingBox.height <= 0.75 * mat.height()) // bounding box height <= 0.75 image height
        {
            individuallyFilteredWordCCs.add(cc);
            avgWidth += boundingBox.width;
            avgHeight += boundingBox.height;
            avgArea += boundingBox.area();
        } else {
            if (DEBUG)
                removedWordCCs.add(cc);
        }
    }
    wordCCs.clear();
    avgWidth /= individuallyFilteredWordCCs.size();
    avgHeight /= individuallyFilteredWordCCs.size();
    avgArea /= individuallyFilteredWordCCs.size();
    if (DEBUG) {
        Imgproc.drawContours(debugMat, removedWordCCs, -1, BLUE, -1);
        removedWordCCs.clear();
    }

    // Filter word level connected-components in relation to their average attributes
    final List<MatOfPoint> filteredWordCCs = new ArrayList<MatOfPoint>();
    for (MatOfPoint cc : individuallyFilteredWordCCs) {
        final Rect boundingBox = Imgproc.boundingRect(cc);
        if (boundingBox.width >= 0.125 * avgWidth // bounding box width >= 0.125 average width
                && boundingBox.width <= 8 * avgWidth // bounding box width <= 8 average width
                && boundingBox.height >= 0.25 * avgHeight // bounding box height >= 0.25 average height
                && boundingBox.height <= 4 * avgHeight) // bounding box height <= 4 average height
        {
            filteredWordCCs.add(cc);
        } else {
            if (DEBUG)
                removedWordCCs.add(cc);
        }
    }
    individuallyFilteredWordCCs.clear();
    if (DEBUG) {
        Imgproc.drawContours(debugMat, filteredWordCCs, -1, GREEN, -1);
        Imgproc.drawContours(debugMat, removedWordCCs, -1, PURPLE, -1);
        removedWordCCs.clear();
    }

    // Extract paragraph level connected-components
    mat.setTo(BLACK);
    Imgproc.drawContours(mat, filteredWordCCs, -1, WHITE, -1);
    final List<MatOfPoint> paragraphCCs = new ArrayList<MatOfPoint>();
    Imgproc.morphologyEx(mat, aux, Imgproc.MORPH_CLOSE, KERNEL_30X30);
    Imgproc.findContours(aux, paragraphCCs, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Filter paragraph level connected-components according to the word level connected-components inside
    final List<MatOfPoint> textCCs = new ArrayList<MatOfPoint>();
    for (MatOfPoint paragraphCC : paragraphCCs) {
        final List<MatOfPoint> wordCCsInParagraphCC = new ArrayList<MatOfPoint>();
        aux.setTo(BLACK);
        Imgproc.drawContours(aux, Collections.singletonList(paragraphCC), -1, WHITE, -1);
        Core.bitwise_and(mat, aux, aux);
        Imgproc.findContours(aux, wordCCsInParagraphCC, new Mat(), Imgproc.RETR_EXTERNAL,
                Imgproc.CHAIN_APPROX_SIMPLE);
        final Rect boundingBox = Imgproc.boundingRect(paragraphCC);
        final double center = mat.size().width / 2;
        final double distToCenter = center > boundingBox.x + boundingBox.width
                ? center - boundingBox.x - boundingBox.width
                : center < boundingBox.x ? boundingBox.x - center : 0.0;
        if (DEBUG) {
            System.err.println("****************************************");
            System.err.println("\tArea:                " + boundingBox.area());
            System.err.println("\tDistance to center:  " + distToCenter);
            System.err.println("\tCCs inside:          " + wordCCsInParagraphCC.size());
        }
        if ((wordCCsInParagraphCC.size() >= 10 || wordCCsInParagraphCC.size() >= 0.3 * filteredWordCCs.size())
                && mat.size().width / distToCenter >= 4) {
            textCCs.addAll(wordCCsInParagraphCC);
            if (DEBUG) {
                System.err.println("\tText:                YES");
                Imgproc.drawContours(debugMat, Collections.singletonList(paragraphCC), -1, DARK_GREEN, 5);
            }
        } else {
            if (DEBUG) {
                System.err.println("\tText:                NO");
                Imgproc.drawContours(debugMat, Collections.singletonList(paragraphCC), -1, DARK_RED, 5);
            }
        }
    }
    filteredWordCCs.clear();
    paragraphCCs.clear();
    mat.setTo(WHITE);
    Imgproc.drawContours(mat, textCCs, -1, BLACK, -1);
    textCCs.clear();
    if (DEBUG)
        Image.fromMat(debugMat).write(new File(debugDir, "4_filtering.jpg"));

    // Obtain the final text mask from the filtered connected-components
    Imgproc.erode(mat, mat, KERNEL_15X15);
    Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_OPEN, KERNEL_30X30);
    if (DEBUG)
        Image.fromMat(mat).write(new File(debugDir, "5_text_mask.jpg"));

    // Apply the text mask to the binarized image
    if (DEBUG)
        Image.fromMat(binary).write(new File(debugDir, "6_binary.jpg"));
    binary.setTo(WHITE, mat);
    if (DEBUG)
        Image.fromMat(binary).write(new File(debugDir, "7_binary_text.jpg"));

    // Dewarp the text using Leptonica
    Pix pixs = Image.fromMat(binary).toGrayscalePix();
    Pix pixsDewarp = Dewarp.dewarp(pixs, 0, Dewarp.DEFAULT_SAMPLING, 5, true);
    final Image result = Image.fromGrayscalePix(pixsDewarp);
    if (DEBUG)
        result.write(new File(debugDir, "8_dewarp.jpg"));

    // Clean up
    pixs.recycle();
    mat.release();
    aux.release();
    binary.release();
    if (debugMat != null)
        debugMat.release();

    return result;
}