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() 

Source Link

Usage

From source file:ch.zhaw.facerecognitionlibrary.Recognition.KNearestNeighbor.java

License:Open Source License

public KNearestNeighbor(Context context, int method) {
    this.context = context;
    fh = new FileHelper();
    k = 20;/*w  ww .ja  va2 s  . co m*/
    trainingList = new Mat();
    testList = new Mat();
    this.labelList = new ArrayList<>();
    this.labelListTest = new ArrayList<>();
    this.labelMap = new OneToOneMap<String, Integer>();
    this.labelMapTest = new OneToOneMap<String, Integer>();
    trainingFile = "knn_traininglist.xml";
    testFile = "knn_testlist.xml";
    this.method = method;
    if (method == RECOGNITION) {
        loadFromFile();
    }

}

From source file:ch.zhaw.facerecognitionlibrary.Recognition.KNearestNeighbor.java

License:Open Source License

@Override
public String recognize(Mat img, String expectedLabel) {
    Mat result = new Mat();
    float nearest;

    img = getFeatureVector(img);//from w  w w  .  j  a v a  2s. com
    addImage(img, expectedLabel, true);
    nearest = knn.findNearest(img, k, result);

    return labelMap.getKey((int) nearest);
}

From source file:Clases.Segmentador.java

public double[] TransfHough(Mat edges, int inv, int distMinCir, int umbMin, int umbMax, int radMin,
        int radMax) {

    Mat circles = new Mat();
    double[] data = null;
    //Vector<Mat> circlesList = new Vector<Mat>();
    Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, inv, distMinCir, umbMin, umbMax, radMin,
            radMax);// w w  w  .  j  a v a 2 s  .  c  o  m
    //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 200, 1, 10, 20, 40  );
    //Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 50) ;

    System.out.println("#rows " + circles.rows() + " #cols " + circles.cols());
    double x = 0.0;
    double y = 0.0;
    int r = 0;

    for (int i = 0; i < circles.rows(); i++) {
        data = circles.get(i, 0);
        for (int j = 0; j < data.length; j++) {
            x = data[0];
            y = data[1];
            r = (int) data[2];
        }
        Point center = new Point(x, y);

        // circle center
        //            Core.circle(color, center, 1, new Scalar(0, 0, 0), -1);
        // circle outline
        //          Core.circle(color, center, r, new Scalar(0, 255, 0), 1);
        //Ventana ventana8 = new Ventana(convertir((color)),0,2);
        //ventana8.setTitle("Houg");
        /*
         Rect bbox = new Rect((int)Math.abs(x-r), (int)Math.abs(y-r), (int)2*r, (int)2*r);
         Mat croped_image = new Mat(color, bbox);
         Imgproc.resize(croped_image, croped_image, new Size(200,200));
         circlesList.add(croped_image);
         Ventana ventana9 = new Ventana(convertir(croped_image),1,2);
         */
    }

    return data;
}

From source file:Class.VideoCap.java

public static void Capture() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    VideoCapture camera = new VideoCapture(0);

    if (!camera.isOpened()) {
        System.out.println("Error");
    } else {//from   w  w w .  ja  v  a 2  s  .com
        Mat frame = new Mat();
        while (true) {
            if (camera.read(frame)) {
                File f = new File("src/PicturesWarning/intruso.jpg");
                Highgui.imwrite(f.getPath(), frame);

                break;
            }
        }
    }
    camera.release();
}

From source file:classes.BlobsFinder.java

public void findBlobContours() {

    Mat grayImage = new Mat();
    Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);
    ImageUtils.saveImage(grayImage, outImageName + "_grayImage.png", request);

    Mat gaussianImage = new Mat();
    Imgproc.GaussianBlur(grayImage, gaussianImage, new Size(0, 0), 3);
    Core.addWeighted(grayImage, 1.5, gaussianImage, -1, 0, gaussianImage);
    ImageUtils.saveImage(gaussianImage, outImageName + "_gaussianGrayImage.png", request);

    Mat binaryImage = new Mat();
    Imgproc.adaptiveThreshold(gaussianImage, binaryImage, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
            Imgproc.THRESH_BINARY_INV, 15, 4);
    ImageUtils.saveImage(binaryImage, outImageName + "_binaryImage.png", request);

    Mat erodedImage = new Mat();

    binaryImage.copyTo(erodedImage);/*from   w  w  w.j a va2 s. c o m*/

    Mat structuringElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
    Point anchor = new Point(-1, -1);

    Imgproc.morphologyEx(erodedImage, erodedImage, Imgproc.MORPH_CLOSE, structuringElement, anchor, 1);
    ImageUtils.saveImage(erodedImage, outImageName + "_erodedImage.png", request);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.findContours(erodedImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    Mat originalContoursImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0));
    Scalar contourColor = new Scalar(255);
    int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours
    Imgproc.drawContours(originalContoursImage, contours, -1, contourColor, thickness); // Drawing all the contours found
    ImageUtils.saveImage(originalContoursImage, outImageName + "_originalContoursImage.png", request);

    Mat erodedContoursImage = new Mat();
    Imgproc.erode(originalContoursImage, erodedContoursImage, structuringElement, anchor, 1);
    ImageUtils.saveImage(erodedContoursImage, outImageName + "_erodedContoursImage.png", request);

    ArrayList<MatOfPoint> finalContours = new ArrayList<MatOfPoint>();
    Mat finalContourImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0));
    Imgproc.findContours(erodedContoursImage, finalContours, new Mat(), Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < finalContours.size(); i++) {
        MatOfPoint currentContour = finalContours.get(i);
        double area = Imgproc.contourArea(currentContour);
        if (area > MIN_AREA) {

            validContours.add(currentContour);

            String fabricPath = generateFabricPathString(currentContour);
            contourPaths.add(fabricPath);

            Rect boundingRect = Imgproc.boundingRect(currentContour);
            topLeftCorners.add(boundingRect.tl());

            contoursAreas.add(area);
        }
    }

    // Drawing ALL the valid contours
    Imgproc.drawContours(finalContourImage, validContours, -1, contourColor, thickness);
    ImageUtils.saveImage(finalContourImage, outImageName + "_finalContourImage.png", request);

}

From source file:classes.FloodFillFacade.java

public Mat fill(Mat image, Mat mask, int x, int y, Scalar newVal) {

    Point seedPoint = new Point(x, y);

    Rect rect = new Rect();

    //        Scalar newVal = isColored() ? new Scalar(b, g, r) : new Scalar(r * 0.299 + g * 0.587 + b * 0.114);
    Scalar lowerDifference = new Scalar(lowerDiff, lowerDiff, lowerDiff);
    Scalar upperDifference = new Scalar(upperDiff, upperDiff, upperDiff);
    if (range == NULL_RANGE) {
        lowerDifference = new Scalar(0, 0, 0);
        upperDifference = new Scalar(0, 0, 0);
    }//from   www  . j  a v  a  2s  . co m
    int flags = connectivity + (newMaskVal << 8)
            + ((range == FIXED_RANGE ? Imgproc.FLOODFILL_FIXED_RANGE : 0) | 0);//Imgproc.FLOODFILL_MASK_ONLY);
    int area = 0;
    if (masked) {
        area = Imgproc.floodFill(image, mask, seedPoint, newVal, rect, lowerDifference, upperDifference, flags);
    } else {
        area = Imgproc.floodFill(image, new Mat(), seedPoint, newVal, rect, lowerDifference, upperDifference,
                flags);
    }

    //        Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\image_after_flood_" + cont + ".png", image);
    //        Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\mask_" + cont + ".png", mask);

    //        System.out.println("area: " + area);

    cont++;

    return image;

}

From source file:classes.ObjectFinder.java

public ObjectFinder(Mat objectImage, ArrayList<Integer> thresholdsVector) {
    this.objectImage = objectImage;

    Highgui.imwrite("objectImage.png", objectImage);

    this.thresholdsVector = thresholdsVector;
    this.objectHistogram = new Mat();
    this.meanColor = Core.mean(objectImage);
    computeObjectHistogram();//from w  ww . j  a  v a2s.c  om
}

From source file:classes.ObjectFinder.java

public ObjectFinder(Mat objectImage, ArrayList<Integer> thresholdsVector, String objectName) {
    this.objectImage = objectImage;
    this.thresholdsVector = thresholdsVector;
    this.objectName = objectName;
    this.objectHistogram = new Mat();
    computeObjectHistogram();//from w  ww.  java  2s.  co m
}

From source file:classes.ObjectFinder.java

private void computeSearchWindow() {

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    // a vector of contours
    // retrieve the external contours
    // all pixels of each contours    
    Imgproc.findContours(this.morphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_NONE);/*from   ww  w.  j  ava  2 s  .  c  o m*/

    // Draw black contours on a white image
    this.contoursImage = new Mat(morphologicalImage.size(), CvType.CV_8U, new Scalar(255));

    if (contours.size() > 1) {

        int minContourWith = 20;
        int minContourHeight = 20;
        int maxContourWith = 6400 / 2;
        int maxContourHeight = 4800 / 2;

        contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight);
    }

    if (contours.size() > 1) {
        Collections.sort(contours, new ContourComparator()); // Sorttig the contours to take ONLY the bigger one
    }

    computedSearchWindow = new Rect();
    massCenter = new Point(-1, -1);

    if (contours.size() > 0) {

        this.firstContour = contours.get(0);

        Mat contournedImage = this.firstContour;

        // draw all contours in black with a thickness of 2
        Scalar color = new Scalar(0);
        int thickness = 2;
        Imgproc.drawContours(contoursImage, contours, 0, color, thickness); //

        // testing the bounding box
        computedSearchWindow = Imgproc.boundingRect(this.firstContour);

        topLeftCorner = computedSearchWindow.tl();

        // compute all moments
        Moments mom = Imgproc.moments(contournedImage);

        massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00());

        // draw black dot
        Core.circle(contoursImage, massCenter, 4, color, 8);
    }
}

From source file:classes.TextRecognitionPreparer.java

public static ArrayList<String> generateRecognizableImagesNames(Mat img, Scalar userPickedColor, String imageID,
        HttpServletRequest request) {/*from   w w  w .j a va2s. co m*/

    ArrayList<String> imageNames = new ArrayList<String>();

    Mat filledImage = img.clone();
    Scalar newVal = new Scalar(userPickedColor.val[2], userPickedColor.val[1], userPickedColor.val[0]);

    Imgproc.floodFill(filledImage, new Mat(), new Point(0, 0), newVal);
    String file1 = imageID + "_filledImage.png";
    //        Highgui.imwrite(file1, filledImage);
    imageNames.add(ImageUtils.saveImage(filledImage, file1, request));

    Mat filledGrayImage = new Mat();
    Imgproc.cvtColor(filledImage, filledGrayImage, Imgproc.COLOR_BGR2GRAY);
    String file2 = imageID + "_filledGrayImage.png";
    //        Highgui.imwrite(file2, filledGrayImage);
    imageNames.add(ImageUtils.saveImage(filledGrayImage, file2, request));

    Mat gaussianGrayImage = new Mat();
    Imgproc.GaussianBlur(filledGrayImage, gaussianGrayImage, new Size(0, 0), 3);
    Core.addWeighted(filledGrayImage, 3.5, gaussianGrayImage, -1, 0, gaussianGrayImage);
    String file3 = imageID + "_sharpenedImage.png";
    //        Highgui.imwrite(file3, gaussianGrayImage);
    imageNames.add(ImageUtils.saveImage(gaussianGrayImage, file3, request));

    //        Mat filledBinarizedImage2 = new Mat();
    //        Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage2, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 10);
    //        String file5 = imageID + "_filledBinarizedImage2.png";  
    ////        Highgui.imwrite(file11, filledBinarizedImage2);
    //        imageNames.add(ImageUtils.saveImage(filledBinarizedImage2, file5));
    //        
    //        Mat filledBinarizedImage1 = new Mat();
    //        Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage1, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 4);
    //        String file4 = imageID + "_filledBinarizedImage1.png";  
    ////        Highgui.imwrite(file4, filledBinarizedImage1);
    //        imageNames.add(ImageUtils.saveImage(filledBinarizedImage1, file4));

    return imageNames;
}