Example usage for org.opencv.core Mat submat

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

Introduction

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

Prototype

public Mat submat(int rowStart, int rowEnd, int colStart, int colEnd) 

Source Link

Usage

From source file:by.zuyeu.deyestracker.core.util.CVCoreUtils.java

public static Mat selectSubmatByRect(Rect rect, Mat image) {
    double colScale = 1.0 * image.cols() / image.width();
    int colStart = (int) (1.0 * rect.x * colScale);
    int colEnd = (int) (1.0 * (rect.x + rect.width) * colScale);
    double rowScale = 1.0 * image.rows() / image.height();
    int rowStart = (int) (1.0 * rect.y * rowScale);
    int rowEnd = (int) (1.0 * (rect.y + rect.height) * rowScale);

    return image.submat(rowStart, rowEnd, colStart, colEnd);
}

From source file:by.zuyeu.deyestracker.core.video.sampler.FaceInfoSampler.java

private Mat selectEyesRegionFromFace(final Mat faceImage) {
    return faceImage.submat(0, faceImage.rows() / 2, 0, faceImage.cols());
}

From source file:classes.ObjectGenerator.java

public static Mat extract(Mat image, double x, double y, int w, int h) {
    int row = (int) y;
    int col = (int) x;
    Mat submat = image.submat(row - h / 2, row + h / 2, col - w / 2, col + w / 2);
    return submat;
}

From source file:classes.TextExtractor.java

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

    Point roiTopLeft = roi.tl();/*ww w . ja  v  a  2s  .c  om*/

    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:com.blogspot.thedsweb.engine.Brightness.java

License:Open Source License

private boolean backlitDetection(Mat yCrCb, Scalar mainMean, int mainMeanLumaValue) {
    // Save the mean brightness and chroma value of the whole frame. Plus
    // the width and height of the frame.
    final int mainMeanChromaValue = (int) mainMean.val[2];
    final int PARTS = 8;
    final int w = yCrCb.width();
    final int h = yCrCb.height();

    final int wStep = w >> 2;
    final int hStep = h >> 1;

    // Separate the image into 8 equal parts.
    final Mat[] tiles = new Mat[PARTS];
    tiles[0] = yCrCb.submat(0, hStep, 0, wStep);
    tiles[1] = yCrCb.submat(0, hStep, wStep, wStep * 2);
    tiles[2] = yCrCb.submat(hStep, h, 0, wStep);
    tiles[3] = yCrCb.submat(hStep, h, wStep, wStep * 2);
    tiles[4] = yCrCb.submat(0, hStep, wStep * 2, wStep * 3);
    tiles[5] = yCrCb.submat(0, hStep, wStep * 2, w);
    tiles[6] = yCrCb.submat(hStep, h, wStep * 2, wStep * 3);
    tiles[7] = yCrCb.submat(hStep, h, wStep * 2, w);

    // Calculate the mean value of all parts.
    final Scalar[] tileMean = new Scalar[PARTS];
    for (int i = 0; i < tileMean.length; i++) {
        tileMean[i] = Core.mean(tiles[i]);
    }/*from  ww w. ja  v  a  2s .  c om*/

    // Save the mean brightness and chroma of all parts.
    final int[] tileMeanLuma = new int[PARTS];
    final int[] tileMeanChroma = new int[PARTS];

    // Save min and max value in container
    MinMaxContainer con = null;

    for (int j = 0; j < tileMean.length; j++) {
        tileMeanLuma[j] = (int) tileMean[j].val[0];
        if (j == 0) {
            con = new MinMaxContainer(tileMeanLuma[j]);
        } else {
            con.add(tileMeanLuma[j]);
        }
        tileMeanChroma[j] = (int) tileMean[j].val[2];
    }

    // Get the highest and lowest brightness value of all frame tiles.
    final int min = con.getMin();
    final int max = con.getMax() >> 1;

    // Check if their is a consistent chroma distribution and a brightness
    // spike to detect backlit conditions.
    if (checkChroma(tileMeanChroma, mainMeanChromaValue) && min < max) {
        return true;
    }

    return false;
}

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

License:Open Source License

public static List<Mat> findHeroTopLinesInImage(Mat photo, List<List<Integer>> colourRanges, int lowerHsvS,
        int lowerHsvV, int upperHsvS, int upperHsvV) {
    List<Mat> linesList = new ArrayList<>();
    int pos = 0;// w  w w  .  j a  va2s. c  om
    int photoWidth = photo.width();

    for (List<Integer> colourRange : colourRanges) {
        int minX;
        int maxX;

        if (colourRanges.size() == 1) {
            minX = 0;
            maxX = photoWidth;
        } else {
            minX = pos * photoWidth / 6;
            maxX = (2 + pos) * photoWidth / 6;
        }

        Scalar lowerHsv = new Scalar(colourRange.get(0), lowerHsvS, lowerHsvV);
        Scalar upperHsv = new Scalar(colourRange.get(1), upperHsvS, upperHsvV);

        Mat subMat = photo.submat(0, photo.height() / 2, minX, maxX);
        Mat mask = new Mat();
        ImageTools.MaskAColourFromImage(subMat, lowerHsv, upperHsv, mask);

        Mat lines = new Mat();

        // note, this is the part that takes most time.
        ImageTools.getLineFromTopRectMask(mask, lines, photoWidth / 7); //USED TO BE 8!!!!

        adjustXPosOfLines(lines, minX);
        linesList.add(lines);

        //   Main.DrawMatInImageBox(mask, maskImage); // just for debug
        pos++;
    }

    return linesList;
}

From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java

License:Open Source License

public Optional<Bounds> calibrateFrame(MatOfPoint2f boardCorners, Mat mat) {

    // For debugging
    Mat traceMat = null;/*from  w w  w. jav a  2s . c om*/
    if (logger.isTraceEnabled()) {
        traceMat = mat.clone();
    }

    initializeSize(mat.cols(), mat.rows());

    // Step 2: Estimate the pattern corners
    MatOfPoint2f estimatedPatternRect = estimatePatternRect(traceMat, boardCorners);

    // Step 3: Use Hough Lines to find the actual corners
    final Optional<MatOfPoint2f> idealCorners = findIdealCorners(mat, estimatedPatternRect);

    if (!idealCorners.isPresent())
        return Optional.empty();

    if (logger.isTraceEnabled()) {
        String filename = String.format("calibrate-dist.png");
        final File file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, traceMat);
    }

    // Step 4: Initialize the warp matrix and bounding box
    initializeWarpPerspective(mat, idealCorners.get());

    if (boundingBox.getMinX() < 0 || boundingBox.getMinY() < 0
            || boundingBox.getWidth() > cameraManager.getFeedWidth()
            || boundingBox.getHeight() > cameraManager.getFeedHeight()) {
        return Optional.empty();
    }

    if (logger.isDebugEnabled())
        logger.debug("bounds {} {} {} {}", boundingBox.getMinX(), boundingBox.getMinY(), boundingBox.getWidth(),
                boundingBox.getHeight());

    final Mat undistorted = warpPerspective(mat);

    if (logger.isTraceEnabled()) {

        String filename = String.format("calibrate-undist.png");
        File file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, undistorted);

        Mat undistortedCropped = undistorted.submat((int) boundingBox.getMinY(), (int) boundingBox.getMaxY(),
                (int) boundingBox.getMinX(), (int) boundingBox.getMaxX());

        filename = String.format("calibrate-undist-cropped.png");
        file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, undistortedCropped);
    }

    Mat warpedBoardCorners = warpCorners(boardCorners);

    isCalibrated = true;

    if (calculateFrameDelay) {
        findColors(undistorted, warpedBoardCorners);

        final double squareHeight = boundingBox.getHeight() / (double) (PATTERN_HEIGHT + 1);
        final double squareWidth = boundingBox.getWidth() / (double) (PATTERN_WIDTH + 1);

        int secondSquareCenterX = (int) (boundingBox.getMinX() + (squareWidth * 1.5));
        int secondSquareCenterY = (int) (boundingBox.getMinY() + (squareHeight * .5));

        if (logger.isDebugEnabled())
            logger.debug("pF getFrameDelayPixel x {} y {} p {}", secondSquareCenterX, secondSquareCenterY,
                    undistorted.get(secondSquareCenterY, secondSquareCenterX));

    }

    return Optional.of(boundingBox);
}

From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java

License:Open Source License

/**
 * Perspective pattern discovery/*from   w ww.j a v  a  2s  .  co  m*/
 * 
 * Works similar to arena calibration but does not try to identify the
 * outline of the projection area We are only concerned with size, not
 * alignment or angle
 * 
 * This function blanks out the pattern that it discovers in the Mat it is
 * provided. This is so that the pattern is not discovered by future pattern
 * discovery, e.g. auto-calibration
 * 
 * workingMat should be null for all external callers unless there is some
 * need to work off a different Mat than is having patterns blanked out by
 * this function
 */
public Optional<Dimension2D> findPaperPattern(MatOfPoint2f boardCorners, Mat mat, Mat workingMat) {

    if (workingMat == null)
        workingMat = mat.clone();

    initializeSize(workingMat.cols(), workingMat.rows());

    // Step 2: Estimate the pattern corners
    final BoundingBox box = getPaperPatternDimensions(workingMat, boardCorners);

    // OpenCV gives us the checkerboard corners, not the outside dimension
    // So this estimates where the outside corner would be, plus a fudge
    // factor for the edge of the paper
    // Printer margins are usually a quarter inch on each edge
    double width = ((double) box.getWidth() * ((double) (PATTERN_WIDTH + 1) / (double) (PATTERN_WIDTH - 1))
            * 1.048);
    double height = ((double) box.getHeight() * ((double) (PATTERN_HEIGHT + 1) / (double) (PATTERN_HEIGHT - 1))
            * 1.063);

    final double PAPER_PATTERN_SIZE_THRESHOLD = .25;
    if (width > PAPER_PATTERN_SIZE_THRESHOLD * workingMat.cols()
            || height > PAPER_PATTERN_SIZE_THRESHOLD * workingMat.rows()) {
        logger.trace("Pattern too big to be paper, must be projection, setting blank {} x {}", box.getWidth(),
                box.getHeight());

        workingMat.submat((int) box.getMinY(), (int) box.getMaxY(), (int) box.getMinX(), (int) box.getMaxX())
                .setTo(new Scalar(0, 0, 0));

        if (logger.isTraceEnabled()) {
            String filename = String.format("blanked-box.png");
            File file = new File(filename);
            filename = file.toString();
            Highgui.imwrite(filename, workingMat);

        }

        final Optional<MatOfPoint2f> boardCornersNew = findChessboard(workingMat);

        if (!boardCornersNew.isPresent())
            return Optional.empty();

        logger.trace("Found new pattern, attempting findPaperPattern {}", boardCornersNew.get());

        return findPaperPattern(boardCornersNew.get(), mat, workingMat);

    }

    if (logger.isTraceEnabled()) {
        logger.trace("pattern width {} height {}", box.getWidth(), box.getHeight());

        logger.trace("paper width {} height {}", width, height);

        int widthOffset = ((int) width - (int) box.getWidth()) / 2;
        int heightOffset = ((int) height - (int) box.getHeight()) / 2;

        logger.trace("offset width {} height {}", widthOffset, heightOffset);

        Mat fullpattern = workingMat.clone();

        // TODO: This doesn't work if the pattern is upside down, but this is for debugging anyway right now
        // Should fix in case it causes an out of bounds or something
        Point topLeft = new Point(boardCorners.get(0, 0)[0], boardCorners.get(0, 0)[1]);
        Point topRight = new Point(boardCorners.get(PATTERN_WIDTH - 1, 0)[0],
                boardCorners.get(PATTERN_WIDTH - 1, 0)[1]);
        Point bottomRight = new Point(boardCorners.get(PATTERN_WIDTH * PATTERN_HEIGHT - 1, 0)[0],
                boardCorners.get(PATTERN_WIDTH * PATTERN_HEIGHT - 1, 0)[1]);
        Point bottomLeft = new Point(boardCorners.get(PATTERN_WIDTH * (PATTERN_HEIGHT - 1), 0)[0],
                boardCorners.get(PATTERN_WIDTH * (PATTERN_HEIGHT - 1), 0)[1]);

        Core.circle(fullpattern, topLeft, 1, new Scalar(255, 0, 0), -1);
        Core.circle(fullpattern, topRight, 1, new Scalar(255, 0, 0), -1);
        Core.circle(fullpattern, bottomRight, 1, new Scalar(255, 0, 0), -1);
        Core.circle(fullpattern, bottomLeft, 1, new Scalar(255, 0, 0), -1);

        String filename = String.format("marked-box.png");
        File file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, fullpattern);

        fullpattern = fullpattern.submat((int) box.getMinY() - heightOffset,
                (int) box.getMinY() - heightOffset + (int) height, (int) box.getMinX() - widthOffset,
                (int) box.getMinX() - widthOffset + (int) width);

        filename = String.format("full-box.png");
        file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, fullpattern);

        Mat cropped = workingMat.submat((int) box.getMinY(), (int) box.getMaxY(), (int) box.getMinX(),
                (int) box.getMaxX());

        filename = String.format("pattern-box.png");
        file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, cropped);
    }

    mat.submat((int) box.getMinY(), (int) box.getMaxY(), (int) box.getMinX(), (int) box.getMaxX())
            .setTo(new Scalar(0, 0, 0));

    return Optional.of(new Dimension2D(width, height));
}

From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java

License:Open Source License

private void findColors(Mat frame, Mat warpedBoardCorners) {
    final Point rCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 3);
    final Point gCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 5);
    final Point bCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 7);

    if (logger.isDebugEnabled()) {
        logger.debug("findColors {} {} {}", rCenter, gCenter, bCenter);
        logger.debug("findColors r {} {} {} {}", (int) rCenter.y - 10, (int) rCenter.y + 10,
                (int) rCenter.x - 10, (int) rCenter.x + 10);
    }//  w  w w. j  a  v a 2  s .  com

    final Scalar rMeanColor = Core.mean(frame.submat((int) rCenter.y - 10, (int) rCenter.y + 10,
            (int) rCenter.x - 10, (int) rCenter.x + 10));
    final Scalar gMeanColor = Core.mean(frame.submat((int) gCenter.y - 10, (int) gCenter.y + 10,
            (int) gCenter.x - 10, (int) gCenter.x + 10));
    final Scalar bMeanColor = Core.mean(frame.submat((int) bCenter.y - 10, (int) bCenter.y + 10,
            (int) bCenter.x - 10, (int) bCenter.x + 10));

    if (logger.isTraceEnabled()) {
        String filename = String.format("rColor.png");
        File file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, frame.submat((int) rCenter.y - 10, (int) rCenter.y + 10, (int) rCenter.x - 10,
                (int) rCenter.x + 10));

        filename = String.format("gColor.png");
        file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, frame.submat((int) gCenter.y - 10, (int) gCenter.y + 10, (int) gCenter.x - 10,
                (int) gCenter.x + 10));

        filename = String.format("bColor.png");
        file = new File(filename);
        filename = file.toString();
        Highgui.imwrite(filename, frame.submat((int) bCenter.y - 10, (int) bCenter.y + 10, (int) bCenter.x - 10,
                (int) bCenter.x + 10));
    }

    if (logger.isDebugEnabled())
        logger.debug("meanColor {} {} {}", rMeanColor, gMeanColor, bMeanColor);
}

From source file:com.shootoff.camera.CameraManager.java

License:Open Source License

protected BufferedImage processFrame(BufferedImage currentFrame) {
    frameCount++;//from w  w w. ja v  a2s  .  co  m

    if (isAutoCalibrating.get() && ((getFrameCount() % Math.min(getFPS(), 3)) == 0)) {

        acm.processFrame(currentFrame);
        return currentFrame;
    }

    Mat matFrameBGR = Camera.bufferedImageToMat(currentFrame);
    Mat submatFrameBGR = null;

    if (cameraAutoCalibrated && projectionBounds.isPresent()) {
        if (acm != null) {
            // MUST BE IN BGR pixel format.
            matFrameBGR = acm.undistortFrame(matFrameBGR);
        }

        submatFrameBGR = matFrameBGR.submat((int) projectionBounds.get().getMinY(),
                (int) projectionBounds.get().getMaxY(), (int) projectionBounds.get().getMinX(),
                (int) projectionBounds.get().getMaxX());

        if (recordingCalibratedArea) {
            BufferedImage image = ConverterFactory.convertToType(Camera.matToBufferedImage(submatFrameBGR),
                    BufferedImage.TYPE_3BYTE_BGR);
            IConverter converter = ConverterFactory.createConverter(image, IPixelFormat.Type.YUV420P);

            IVideoPicture frame = converter.toPicture(image,
                    (System.currentTimeMillis() - recordingCalibratedAreaStartTime) * 1000);
            frame.setKeyFrame(isFirstCalibratedAreaFrame);
            frame.setQuality(0);
            isFirstCalibratedAreaFrame = false;

            videoWriterCalibratedArea.encodeVideo(0, frame);
        }

        if (debuggerListener.isPresent()) {
            debuggerListener.get().updateDebugView(Camera.matToBufferedImage(submatFrameBGR));
        }
    }

    if ((isLimitingDetectionToProjection() || isCroppingFeedToProjection())
            && getProjectionBounds().isPresent()) {
        if (submatFrameBGR == null)
            submatFrameBGR = matFrameBGR.submat((int) projectionBounds.get().getMinY(),
                    (int) projectionBounds.get().getMaxY(), (int) projectionBounds.get().getMinX(),
                    (int) projectionBounds.get().getMaxX());

        shotDetector.processFrame(submatFrameBGR, isDetecting.get());
    } else {
        shotDetector.processFrame(matFrameBGR, isDetecting.get());
    }

    // matFrameBGR is showing the colored pixels for brightness and motion,
    // hence why we need to return the converted version
    return Camera.matToBufferedImage(matFrameBGR);
}