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:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Set a Region of Interest within the image. Subsequent image processing
 * functions will apply to this ROI rather than the full image.
 * Full image will display be included in output.
 * /*ww w .j  a  va 2  s  .c om*/
 * @return
 *       False if requested ROI exceed the bounds of the working image.
 *       True if ROI was successfully set.
 */
public boolean setROI(int x, int y, int w, int h) {
    if (x < 0 || x + w > width || y < 0 || y + h > height) {
        return false;
    } else {
        roiWidth = w;
        roiHeight = h;

        if (useColor) {
            nonROImat = matBGRA;
            matROI = new Mat(matBGRA, new Rect(x, y, w, h));
        } else {
            nonROImat = matGray;
            matROI = new Mat(matGray, new Rect(x, y, w, h));
        }
        useROI = true;

        return true;
    }
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public boolean setROI(int x, int y, int w, int h) {
    if (x < 0 || x + w > width || y < 0 || y + h > height) {
        return false;
    } else {/*from w w w .  j  a va  2s  .c om*/
        roiWidth = w;
        roiHeight = h;

        if (useColor) {
            nonROImat = matBGRA;
            matROI = new Mat(matBGRA, new Rect(x, y, w, h));
        } else {
            nonROImat = matGray;
            matROI = new Mat(matGray, new Rect(x, y, w, h));
        }
        useROI = true;

        return true;
    }
}

From source file:imageprocess.ObjectFinder.java

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread("D:\\backup\\opencv\\baboon1.jpg");
    // Define ROI
    Rect rect = new Rect(110, 260, 35, 40);
    Mat imageROI = new Mat(image, rect);
    Core.rectangle(image, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255));

    Imshow origIm = new Imshow("Origin");
    origIm.showImage(image);/* ww w  .j a  v a 2 s  .c o  m*/

    ObjectFinder finder = new ObjectFinder(false, 0.2f);

    // Get the Hue histogram
    int minSat = 65;
    Mat hist = finder.getHueHistogram(imageROI, minSat);
    Mat norm = new Mat();
    Core.normalize(hist, norm, 1, 0, NORM_L2);

    finder.setROIHistogram(norm);

    // Convert to HSV space
    Mat hsv = new Mat();
    Imgproc.cvtColor(image, hsv, CV_BGR2HSV);
    // Split the image
    List<Mat> v = new ArrayList<>();
    Core.split(hsv, v);

    // Eliminate pixels with low saturation
    Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY);
    Imshow satIm = new Imshow("Saturation");
    satIm.showImage(v.get(1));
    // Get back-projection of hue histogram
    Mat result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f));

    Imshow resultHueIm = new Imshow("Result Hue");
    resultHueIm.showImage(result);

    Core.bitwise_and(result, v.get(1), result);
    Imshow resultHueAndIm = new Imshow("Result Hue and raw");
    resultHueAndIm.showImage(result);

    // Second image
    Mat image2 = Highgui.imread("D:\\backup\\opencv\\baboon3.jpg");

    // Display image
    Imshow img2Im = new Imshow("Imgage2");
    img2Im.showImage(image2);

    // Convert to HSV space
    Imgproc.cvtColor(image2, hsv, CV_BGR2HSV);

    // Split the image
    Core.split(hsv, v);

    // Eliminate pixels with low saturation
    Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY);
    Imshow satIm2 = new Imshow("Saturation2");
    satIm2.showImage(v.get(1));

    // Get back-projection of hue histogram
    finder.setThreshold(-1.0f);
    result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f));

    Imshow resultHueIm2 = new Imshow("Result Hue2");
    resultHueIm2.showImage(result);

    Core.bitwise_and(result, v.get(1), result);
    Imshow resultHueAndIm2 = new Imshow("Result Hue and raw2");
    resultHueAndIm2.showImage(result);

    Rect rect2 = new Rect(110, 260, 35, 40);
    Core.rectangle(image2, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255));

    TermCriteria criteria = new TermCriteria(TermCriteria.MAX_ITER | TermCriteria.EPS, 100, 0.01);
    int steps = Video.meanShift(result, rect2, criteria);

    Core.rectangle(image2, new Point(rect2.x, rect2.y),
            new Point(rect2.x + rect2.width, rect2.y + rect2.height), new Scalar(0, 255, 0));

    Imshow meanshiftIm = new Imshow("Meanshift result");
    meanshiftIm.showImage(image2);

}

From source file:imagesave.n.load.ImageSaveNLoad.java

public static void find_bumpers(String imageName, String targetColor) {

    Mat original = imread(imageName);//from   w  ww . ja  va  2 s .  co  m
    Size s = original.size();
    int ySize = (int) s.height;
    int xSize = (int) s.width;

    System.out.println("Height: " + s.height);
    System.out.println("Width: " + s.width);

    double yCrop = (ySize - 0.3 * ySize);
    double xCrop = 0;
    double heightCrop = (0.3 * ySize);
    double widthCrop = xSize;

    System.out.println("Starting Y Position: " + yCrop);
    System.out.println("Starting X Position: " + xCrop);
    System.out.println("Y size after crop: " + heightCrop);
    System.out.println("X size after crop: " + widthCrop);

    int r = 0;
    int g = 0;
    int b = 0;

    Rect rectCrop = new Rect((int) xCrop, (int) yCrop, (int) widthCrop, (int) heightCrop);
    //Rect rectCrop = new Rect(2, 23, 380, 42);
    Mat imCrop = new Mat(original, rectCrop);

    //imwrite("Lilac.jpg", imCrop);

    if (targetColor.equals("r")) {
        r = 219;
        g = 94;
        b = 92;
    }

    if (targetColor.equals("b")) {
        r = 40;
        g = 61;
        b = 140;
    }

    Mat modImage = new Mat();
    Imgproc.cvtColor(imCrop, modImage, Imgproc.COLOR_RGB2HSV);
    //Imgproc.cvtColor(imCrop, modImage, Imgproc.COLOR_RGB2BGR);
    //Imgproc.cvtColor(imCrop, modImage, Imgproc.COLOR_BGR2HSV);
    //threshold(modImage, modImage, 20, 255, 1);
    //threshold(modImage, modImage, 170, 255, 0);
    Imgproc.cvtColor(modImage, modImage, Imgproc.COLOR_RGB2GRAY);

    threshold(modImage, modImage, 170, 255, 0);

    //imwrite("Lilac.jpg", modImage);
    //Vector<Mat> bgr_planes = null;
    //split(modImage, bgr_planes);
    imwrite("Lilac.jpg", modImage);

}

From source file:interactivespaces.activity.image.vision.opencv.outline.ImageOpenCvVisionOutlineActivity.java

License:Apache License

/**
 * Handle a new video frame./*from   w  ww  .jav a 2  s  .co  m*/
 *
 * @param frame
 *        the frame to handle
 *
 * @return the completed frame
 */
public Mat handleNewVideoFrame(Mat frame) {
    Mat processed = new Mat(frame.size(), CvType.CV_8UC3);
    edgeify(frame, processed);

    panel.drawImage(processed);

    return processed;
}

From source file:io.github.jakejmattson.facialrecognition.FacialRecognition.java

License:Open Source License

private static Mat detectFaces(Mat image, CascadeClassifier faceDetector, ImageFrame frame) {
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);
    Rect[] faces = faceDetections.toArray();
    boolean shouldSave = frame.shouldSave();
    String name = frame.getFileName();
    Scalar color = frame.getTextColor();

    for (Rect face : faces) {
        Mat croppedImage = new Mat(image, face);

        if (shouldSave)
            saveImage(croppedImage, name);

        Imgproc.putText(image, "ID: " + identifyFace(croppedImage), face.tl(), Font.BOLD, 1.5, color);
        Imgproc.rectangle(image, face.tl(), face.br(), color);
    }/*from  w  ww .  j  a va  2s  .co m*/

    int faceCount = faces.length;
    String message = faceCount + (faceCount == 1 ? "face" : "faces") + " detected!";
    Imgproc.putText(image, message, new Point(3, 25), Font.BOLD, 2, color);

    return image;
}

From source file:javaapplication1.Ocv.java

public void cropEachFace(String filter, String input) {
    // load the filter and create a classifier with it
    File f = new File(filter);
    final CascadeClassifier faceDetector = new CascadeClassifier(this.filter);

    // load the image and read it into a matrix
    File f2 = new File(input);
    final Mat image = Highgui.imread(this.input);

    // run a face detector on the image
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    // inform about faces detected
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

    // Make a separate image for each face region
    int i = 0;//from w w  w .  j a v  a2  s  .com
    for (Rect rect : faceDetections.toArray()) {
        Mat cropped = new Mat(image, rect); // wow, cropping is easy!
        Highgui.imwrite("" + i + ".png", cropped);
        i++;
    }
}

From source file:javaapplication1.Ocv.java

void resizeEachFace(String filter, String input) {
    // load the filter and create a classifier with it
    File f = new File(filter);
    final CascadeClassifier faceDetector = new CascadeClassifier(this.filter);

    // load the image and read it into a matrix
    File f2 = new File(this.input);
    final Mat image = Highgui.imread(f2.getAbsolutePath());

    // run a face detector on the image
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    // inform about faces detected, and then outline each of them
    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
    int i = 0;//from www  .  j  a  v  a2 s  . c  o  m
    for (Rect rect : faceDetections.toArray()) {
        // crop out the face
        Mat cropped = new Mat(image, rect);
        // resize the face.
        Mat resized = new Mat();
        // change the Size to shrink or expand the image
        Size s = new Size(1000, 1000);
        Imgproc.resize(cropped, resized, s);
        Highgui.imwrite("" + i + ".png", resized);
        i++;
    }
}