Example usage for org.opencv.core Mat zeros

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

Introduction

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

Prototype

public static Mat zeros(int rows, int cols, int type) 

Source Link

Usage

From source file:de.vion.eyetracking.cameracalib.calibration.opencv.CameraCalibrator.java

public CameraCalibrator(int width, int height) {
    this.mImageSize = new Size(width, height);
    this.mFlags = Calib3d.CALIB_FIX_PRINCIPAL_POINT + Calib3d.CALIB_ZERO_TANGENT_DIST
            + Calib3d.CALIB_FIX_ASPECT_RATIO + Calib3d.CALIB_FIX_K4 + Calib3d.CALIB_FIX_K5;
    Mat.eye(3, 3, CvType.CV_64FC1).copyTo(this.mCameraMatrix);
    this.mCameraMatrix.put(0, 0, 1.0);
    Mat.zeros(5, 1, CvType.CV_64FC1).copyTo(this.mDistortionCoefficients);
    Log.i(TAG, "Instantiated new " + this.getClass());
}

From source file:de.vion.eyetracking.cameracalib.calibration.opencv.CameraCalibrator.java

public void calibrate() {
    ArrayList<Mat> rvecs = new ArrayList<Mat>();
    ArrayList<Mat> tvecs = new ArrayList<Mat>();
    Mat reprojectionErrors = new Mat();
    ArrayList<Mat> objectPoints = new ArrayList<Mat>();
    objectPoints.add(Mat.zeros(this.mCornersSize, 1, CvType.CV_32FC3));
    calcBoardCornerPositions(objectPoints.get(0));
    for (int i = 1; i < this.mCornersBuffer.size(); i++) {
        objectPoints.add(objectPoints.get(0));
    }/*w w  w . jav  a  2 s .  c o  m*/

    Calib3d.calibrateCamera(objectPoints, this.mCornersBuffer, this.mImageSize, this.mCameraMatrix,
            this.mDistortionCoefficients, rvecs, tvecs, this.mFlags);

    this.mIsCalibrated = Core.checkRange(this.mCameraMatrix) && Core.checkRange(this.mDistortionCoefficients);

    this.mRms = computeReprojectionErrors(objectPoints, rvecs, tvecs, reprojectionErrors);
}

From source file:digimesh.xbee.gui.MeshGui.java

public MeshGui() {
    initComponents();//www  .j  a v a2  s .  c  o  m
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    sensorMap = Mat.zeros(320, 490, CvType.CV_8UC3);
    BufferedImage image = Mat2BufferedImage(sensorMap);
    ImageIcon icon = new ImageIcon(image);
    jLMapImage.setIcon(icon);
    //init memeber variable
    workerThreadStarted = false;
    broadcastData = false;
    canAddData = true;
    hubDataToSend = new ArrayList<>();
    smartSensors = new ArrayList<>();
    Position hubPos = new Position(HUB_LAT, HUB_LONG);
    smartSensors.add(new SmartSensor(HUB_64ADRESS, hubPos, 1000));
    cbSensorList.addItem("HUB");
    messagesBuffer = new ArrayList<>();
    connectedToHubModule = false;
    serialGuiOpen = false;
    sensorSettingsOpen = false;
    Enumeration ports = CommPortIdentifier.getPortIdentifiers();
    ArrayList<String> portNames = new ArrayList<String>();
    while (ports.hasMoreElements()) {
        portNames.add(((CommPortIdentifier) ports.nextElement()).getName());
    }
    cbPortName.setModel(new DefaultComboBoxModel(portNames.toArray()));
    caretPosition = 0;
    prevCaretPos = 0;
}

From source file:digimesh.xbee.gui.SensorMap.java

public Mat getMap() {
    coorTab = new Position[2];
    findMinMaxCordinates(); // {<minx,miny> , <maxx, maxy>} // maybe make coordinates a separate object later

    double maxX = longLatToDist(coorTab[0].positionX, coorTab[0].positionX, coorTab[0].positionY,
            coorTab[1].positionY);//from w ww .j a v  a 2s .  c o  m
    double maxY = longLatToDist(coorTab[0].positionX, coorTab[1].positionX, coorTab[0].positionY,
            coorTab[0].positionY);
    boolean flip = false;
    if (maxX < maxY) {
        double temp = maxX;
        maxX = maxY;
        maxY = temp;
        flip = true;
    }

    //get the maxX maxY in meters
    anglePositionToMeters(flip, maxX, maxY);

    //sensor map c.d - desingn map - image size in pixels based on maxX, maxY, number and position of Sensors and their range
    // picturebox size is 490 x 320 (width(horizontal), length(vertical)) - this should be minimal size of image
    map = Mat.zeros(320, 490, CvType.CV_8UC3); //  rows-length cols -width format

    double scaleX = getScale(maxX, map.cols());
    double scaleY = getScale(maxY, map.rows());
    for (SmartSensor sensor : this.SensorList) {
        if (sensor.setPositionGPS) {
            sensor.setSensorPositionXY(sensor.getPositionXY().positionX * scaleX,
                    sensor.getPositionXY().positionY * scaleY);
            drawSensor(sensor);
        }
    }

    return map;
}

From source file:karthik.Barcode.Barcode.java

License:Open Source License

protected Mat scale_candidateBarcode(Mat candidate) {
    // resizes candidate image to have at least MIN_COLS columns and MIN_ROWS rows
    // called when RESIZE_BEFORE_DECODE is set - seems to help ZXing decode barcode

    int MIN_COLS = 200;
    int MIN_ROWS = 200;

    int num_rows = candidate.rows();
    int num_cols = candidate.cols();

    if ((num_cols > MIN_COLS) && (num_rows > MIN_ROWS))
        return candidate;

    if (num_cols < MIN_COLS) {
        num_rows = (int) (num_rows * MIN_COLS / (1.0 * num_cols));
        num_cols = MIN_COLS;//from   w ww . j a  v a2 s  . co  m
    }

    if (num_rows < MIN_ROWS) {
        num_cols = (int) (num_cols * MIN_ROWS / (1.0 * num_rows));
        num_rows = MIN_ROWS;
    }

    Mat result = Mat.zeros(num_rows, num_cols, candidate.type());

    Imgproc.resize(candidate, result, result.size(), 0, 0, Imgproc.INTER_CUBIC);
    return result;
}

From source file:karthik.Barcode.ImageInfo.java

License:Open Source License

protected void initializeMats(int rows, int cols, SearchParameters searchParams) {
    probabilities = Mat.zeros((int) (rows * searchParams.scale_factor + 1),
            (int) (cols * searchParams.scale_factor + 1), CvType.CV_8U);
    src_grayscale = new Mat(rows, cols, CvType.CV_32F);
    probMatRows = probabilities.rows();//from  w  w w .j a  v  a  2  s  . c  om
    probMatCols = probabilities.cols();

    histNumCols = cols + 1;
    histNumRows = rows + 1;
    edgeDensity = new int[(cols + 1) * (rows + 1) * temp_integral.channels()];

    // create Mat objects to contain integral histograms
    for (int r = 0; r < bins; r++) {
        histograms.add(Mat.zeros((int) (rows / (1.0 * searchParams.tileSize) + 1),
                (int) (cols / (1.0 * searchParams.tileSize) + 1), CvType.CV_32F));
        // below includes channels for completeness but temp_integral should only have one channel
        // one extra row and column as required for integral images
        histIntegralArrays[r] = new int[(cols + 1) * (rows + 1) * temp_integral.channels()];
    }
}

From source file:mvision.Bhattacharyya.java

public Mat histogram(String img) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat image = Highgui.imread(img);//  w  w w .ja  v a  2s .co  m

    //Mat image = Highgui.imread("C:\\image1.jpg");

    //Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);

    Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV);
    java.util.List<Mat> matList = new LinkedList<Mat>();
    matList.add(image);
    Mat histogram = new Mat();
    MatOfFloat ranges = new MatOfFloat(0, 256);
    MatOfInt histSize = new MatOfInt(255);
    Imgproc.calcHist(matList, new MatOfInt(0), new Mat(), histogram, histSize, ranges);

    // Create space for histogram image
    Mat histImage = Mat.zeros(100, (int) histSize.get(0, 0)[0], CvType.CV_8UC1);

    histogram.convertTo(histogram, CvType.CV_32F);

    // Normalize histogram      
    Core.normalize(histogram, histogram, 1, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    // Draw lines for histogram points
    for (int i = 0; i < (int) histSize.get(0, 0)[0]; i++) {
        Core.line(histImage, new org.opencv.core.Point(i, histImage.rows()),
                new org.opencv.core.Point(i, histImage.rows() - Math.round(histogram.get(i, 0)[0])),
                new Scalar(255, 255, 255), 1, 8, 0);
    }
    return histogram;

}

From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.OpenCVUtil.java

License:Open Source License

public static Mat perspectiveTransform(double[] topLeft, double[] topRight, double[] bottomLeft,
        double[] bottomRight, Mat bgr) {

    // determine the size of the destination Mat: use the positions of the finder patterns to determine the width and height.
    // look out: the horizontal direction now refers again to the actual calibration card
    int verSize = (int) Math.round(
            Math.sqrt(Math.pow((topLeft[0] - topRight[0]), 2) + Math.pow((topLeft[1] - topRight[1]), 2)));
    int horSize = (int) Math.round(
            Math.sqrt(Math.pow((topLeft[0] - bottomLeft[0]), 2) + Math.pow((topLeft[1] - bottomLeft[1]), 2)));

    // we rotate the resulting image, so we go from a portrait view to the regular calibration card in landscape
    // so the mapping is:
    // top left source => top right destination
    // top right source => bottom right destination
    // bottom right source => bottom left destination
    // bottom left source => top left destination

    double[] trDest = new double[] { horSize - 1, 0 };
    double[] brDest = new double[] { horSize - 1, verSize - 1 };
    double[] blDest = new double[] { 0, verSize - 1 };
    double[] tlDest = new double[] { 0, 0 };

    Mat transformMatrix = transformMatrix(topLeft, topRight, bottomRight, bottomLeft, trDest, brDest, blDest,
            tlDest);//from  www  . j a  va 2s. com

    //make a destination mat for a warp
    Mat warpMat = Mat.zeros(verSize, horSize, bgr.type());

    //do the warp
    Imgproc.warpPerspective(bgr, warpMat, transformMatrix, warpMat.size());
    return warpMat;
}

From source file:org.ar.rubik.MonoChromatic.java

License:Open Source License

/**
 * Use mask operation and then min max.//from w ww .  java 2  s.  c  o m
 * This solution consumes about 20 minutes per frame!
 * 
 * @param original_image
 * @return
 */
@SuppressWarnings("unused")
private static Mat monochromaticMedianImageFilterUtilizingOpenCv2(Mat original_image) {

    final Size imageSize = original_image.size();
    final int numColumns = (int) original_image.size().width;
    final int numRows = (int) original_image.size().height;
    final int bufferSize = numColumns * numRows;
    final int span = (int) 7;
    final int accuracy = (int) 5;

    Mat hsv_image = new Mat(imageSize, CvType.CV_8UC3);
    Imgproc.cvtColor(original_image, hsv_image, Imgproc.COLOR_RGB2HLS);
    List<Mat> channels = new LinkedList<Mat>();
    Core.split(hsv_image, channels);
    Mat hueMat = channels.get(0);
    Mat lumMat = channels.get(1);
    Mat satMat = channels.get(2);

    // Output byte array for speed efficiency
    Mat monochromatic_image = new Mat(imageSize, CvType.CV_8UC1);
    byte[] monochromaticByteArray = new byte[bufferSize];

    Mat mask = Mat.zeros(numRows, numColumns, CvType.CV_8UC1);

    Log.i(Constants.TAG, "Begin MonoChromatic CV");
    for (int row = 0; row < numRows; row++) {

        byte result_pixel = 0;

        for (int col = 0; col < numColumns; col++) {

            if (col < span || (col >= numColumns - span))
                result_pixel = 0; // Just put in black

            else if (row < span || (row >= numRows - span))
                result_pixel = 0; // Just put in black

            else {

                //               Log.i(Constants.TAG, "Creating Mask at " + row +"," + col);
                Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span),
                        new Scalar(1, 1, 1));

                //               Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(hueMat, mask);
                Mat subset = new Mat();
                hueMat.copyTo(subset, mask);
                Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(subset);

                if (((minMaxResult.maxVal - minMaxResult.maxVal) < accuracy)) //&& (lum_max - lum_min < accuracy) && (sat_max - sat_min < accuracy) )
                    result_pixel = (byte) 128;
                else
                    result_pixel = (byte) 0;
                //               Log.i(Constants.TAG, "Completed Mask at " + row +"," + col);

                Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span),
                        new Scalar(0, 0, 0));
            }

            if ((col >= span / 2) && (row >= span / 2))
                monochromaticByteArray[(row - span / 2) * numColumns + (col - span / 2)] = result_pixel;
        }

        Log.i(Constants.TAG, "Completed Row: " + row);
    }

    monochromatic_image.put(0, 0, monochromaticByteArray);
    Log.i(Constants.TAG, "Completed MonoChromatic CV");
    //      System.exit(0);
    return monochromatic_image;
}

From source file:org.ar.rubik.MonoChromatic.java

License:Open Source License

/**
 * Use OpenCV minMax.//  ww  w.  j ava2  s .  c o m
 * 
 * However, this is enormously slow, taking 10 minutes per frame!  Why?
 * I think because it is effective O(O^4) in computation.
 * 
 * @param original_image
 * @return
 */
@SuppressWarnings("unused")
private static Mat monochromaticMedianImageFilterUtilizingOpenCv(Mat original_image) {

    final Size imageSize = original_image.size();
    final int numColumns = (int) original_image.size().width;
    final int numRows = (int) original_image.size().height;
    final int bufferSize = numColumns * numRows;
    final int span = (int) 7;
    final int accuracy = (int) 5;

    Mat hsv_image = new Mat(imageSize, CvType.CV_8UC3);
    Imgproc.cvtColor(original_image, hsv_image, Imgproc.COLOR_RGB2HLS);
    List<Mat> channels = new LinkedList<Mat>();
    Core.split(hsv_image, channels);
    Mat hueMat = channels.get(0);
    Mat lumMat = channels.get(1);
    Mat satMat = channels.get(2);

    // Output byte array for speed efficiency
    Mat monochromatic_image = new Mat(imageSize, CvType.CV_8UC1);
    byte[] monochromaticByteArray = new byte[bufferSize];

    Mat mask = Mat.zeros(numRows, numColumns, CvType.CV_8UC1);

    Log.i(Constants.TAG, "Begin MonoChromatic CV");
    for (int row = 0; row < numRows; row++) {

        byte result_pixel = 0;

        for (int col = 0; col < numColumns; col++) {

            if (col < span || (col >= numColumns - span))
                result_pixel = 0; // Just put in black

            else if (row < span || (row >= numRows - span))
                result_pixel = 0; // Just put in black

            else {

                //               Log.i(Constants.TAG, "Creating Mask at " + row +"," + col);
                Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span),
                        new Scalar(1, 1, 1));

                Core.MinMaxLocResult minMaxResult = Core.minMaxLoc(hueMat, mask);

                if (((minMaxResult.maxVal - minMaxResult.maxVal) < accuracy)) //&& (lum_max - lum_min < accuracy) && (sat_max - sat_min < accuracy) )
                    result_pixel = (byte) 128;
                else
                    result_pixel = (byte) 0;
                //               Log.i(Constants.TAG, "Completed Mask at " + row +"," + col);

                Core.rectangle(mask, new Point(row, col), new Point(row + span, col + span),
                        new Scalar(0, 0, 0));
            }

            if ((col >= span / 2) && (row >= span / 2))
                monochromaticByteArray[(row - span / 2) * numColumns + (col - span / 2)] = result_pixel;
        }

        Log.i(Constants.TAG, "Completed Row: " + row);

    }

    monochromatic_image.put(0, 0, monochromaticByteArray);
    Log.i(Constants.TAG, "Completed MonoChromatic CV");
    //      System.exit(0);
    return monochromatic_image;
}