Example usage for org.opencv.core Mat width

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

Introduction

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

Prototype

public int width() 

Source Link

Usage

From source file:emotion.EyeRegion.java

public static void areEyebrowsWrinkles() {
    //setting parameters
    int height = (int) (abs(rightInnerEyebrowsCorner.y - rightInnerEyeCorner.y) * 1.2);
    int width = (int) (rightInnerEyeCorner.x - leftInnerEyeCorner.x);
    int y = (int) (rightInnerEyebrowsCorner.y - height / 2);
    int x = (int) leftInnerEyebrowsCorner.x;

    Rect wrinklesRect = new Rect(x, y, width, height);
    Mat wrinklesArea = new Mat(_face, wrinklesRect).clone();

    wrinklesThreshold = (int) (wrinklesArea.width() * wrinklesArea.height() * 0.085);
    //Wrinkles between eyebrows are vertical
    int[] gradientMask = new int[9];
    gradientMask[0] = -1;// w  w  w .  j a va 2  s. c  om
    gradientMask[1] = 0;
    gradientMask[2] = 1;
    gradientMask[3] = -5;
    gradientMask[4] = 0;
    gradientMask[5] = 5;
    gradientMask[6] = -1;
    gradientMask[7] = 0;
    gradientMask[8] = 1;

    wrinklesArea.convertTo(wrinklesArea, CvType.CV_32F);
    Imgproc.cvtColor(wrinklesArea, wrinklesArea, Imgproc.COLOR_BGR2GRAY);
    Core.pow(wrinklesArea, 1.09, wrinklesArea);
    imwrite("wrinklesArea.jpg", wrinklesArea);

    wrinklesArea = StaticFunctions.convolution(gradientMask, wrinklesArea);
    threshold(wrinklesArea, wrinklesArea, 110, 255, Imgproc.THRESH_BINARY);
    imwrite("wrinklesAreaGradiented.jpg", wrinklesArea);

    long wrinklesPoints = 0;
    for (int i = 0; i < wrinklesArea.width(); i++) {
        for (int j = 0; j < wrinklesArea.height(); j++) {
            if (wrinklesArea.get(j, i)[0] == 255) {
                wrinklesPoints++;
            }
        }
    }
    EyeRegion.wrinklesFactor = wrinklesPoints;
    //        System.out.println("Wrinkles factor: "+wrinklesPoints);
    if (wrinklesPoints >= wrinklesThreshold) {
        //            System.out.println("Expression wrinkles detected! Threshold exceeded");
        Imgproc.rectangle(EyeRegion._face, wrinklesRect.br(), wrinklesRect.tl(), new Scalar(0, 50, 205));
    }
}

From source file:emotion.StaticFunctions.java

public static Mat gabor(Mat image) {
    Mat img = image.clone();

    double ksize = 15;
    double sigme = 4;
    double gamma = 1;
    double psi = 50;
    int lambd[] = new int[] { 5, 6, 7, 10/*,15,13,2*/ };
    double theta[] = new double[] { 180, 200 };
    ArrayList<Mat> kernels = new ArrayList<>();
    for (int i = 0; i < theta.length; i++) {
        for (int j = 0; j < lambd.length; j++) {
            kernels.add(Imgproc.getGaborKernel(new Size(ksize, ksize), sigme, theta[i], lambd[j], gamma, psi,
                    CvType.CV_32F));/*from ww  w.  j  av  a  2  s . c o  m*/
        }
    }

    Mat result = new Mat(img.height(), img.width(), img.type(), new Scalar(0, 0, 0));
    for (Mat kernel : kernels) {
        Mat temp = new Mat(img.height(), img.width(), img.type(), new Scalar(0, 0, 0));
        Imgproc.filter2D(img, temp, -1, kernel);
        Core.add(result, temp, result);
    }

    //imwrite("gaborResult.jpg",result);
    return result;
}

From source file:es.ugr.osgiliart.features.opencv.Histogram.java

License:Open Source License

@Override
public double[] extract(Mat image) {
    Mat hsvImage = new Mat(image.width(), image.height(), image.type());
    Mat histHue = new Mat();
    Mat histSaturation = new Mat();

    Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV);
    List<Mat> channels = new ArrayList<Mat>();
    Core.split(hsvImage, channels);/*from www. j av  a2s  .  c om*/

    //Histogram for hue
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(0) }), new MatOfInt(0), new Mat(), histHue,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    //Histogram for saturation
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(1) }), new MatOfInt(0), new Mat(), histSaturation,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    double sum = Core.sumElems(histHue).val[0];
    double[] values = new double[histHue.height() + histSaturation.height()];
    int k = 0;
    for (int i = 0; i < histHue.height(); ++i) {
        values[k++] = histHue.get(i, 0)[0] / sum;
    }
    sum = Core.sumElems(histSaturation).val[0];
    for (int i = 0; i < histSaturation.height(); ++i) {
        values[k++] = histSaturation.get(i, 0)[0] / sum;
    }
    return values;
}

From source file:fi.conf.tabare.ARDataProvider.java

private void detect() {

    //Mat composite_image;
    Mat input_image = new Mat();
    Mat undistorted_image = new Mat();
    Mat circles = new Mat();
    MatOfKeyPoint mokp = new MatOfKeyPoint();
    Mat cameraMatrix = null;/* ww w .  jav a2 s . c o m*/

    //List<Mat> channels = new LinkedList<>();

    //Loop
    while (running) {
        try {
            if (inputVideo.read(input_image)) {
                Mat preview_image = null;

                if (selectedView == View.calib)
                    preview_image = input_image.clone();

                //Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_RGB2HSV);
                //Core.split(input_image, channels);

                Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_BGR2GRAY);

                //Imgproc.equalizeHist(input_image, input_image);

                input_image.convertTo(input_image, -1, params.contrast, params.brightness); //image*contrast[1.0-3.0] + brightness[0-255]

                doBlur(input_image, input_image, params.blur, params.blurAmount);

                if (selectedView == View.raw)
                    preview_image = input_image.clone();

                if (params.enableDistortion) {

                    if (cameraMatrix == null)
                        cameraMatrix = Imgproc.getDefaultNewCameraMatrix(Mat.eye(3, 3, CvType.CV_64F),
                                new Size(input_image.width(), input_image.height()), true);

                    Imgproc.warpAffine(input_image, input_image, shiftMat, frameSize);

                    if (undistorted_image == null)
                        undistorted_image = new Mat((int) frameSize.width * 2, (int) frameSize.height * 2,
                                CvType.CV_64F);

                    Imgproc.undistort(input_image, undistorted_image, cameraMatrix, distCoeffs);

                    input_image = undistorted_image.clone();

                    if (selectedView == View.dist)
                        preview_image = input_image.clone();

                }

                //               if(background == null) background = input_image.clone();         
                //               if(recaptureBg){
                //                  backgSubstractor.apply(background, background);
                //                  System.out.println(background.channels() + " " + background.size() );
                //                  System.out.println(input_image.channels() + " " + input_image.size() );
                //                  recaptureBg = false;
                //               }
                //               if(dynamicBGRemoval){
                //                  //Imgproc.accumulateWeighted(input_image, background, dynamicBGAmount);
                //                  //Imgproc.accumulateWeighted(input_image, background, 1.0f);
                //                  //Core.subtract(input_image, background, input_image);
                //                  //Core.bitwise_xor(input_image, background, input_image);
                //
                //                  doBlur(input_image, background, Blur.normal_7x7, 0); //Blur a little, to get nicer result when substracting
                //                  backgSubstractor.apply(background, background, dynamicBGAmount);
                //               }
                //               if(background != null) Core.add(input_image, background, input_image);

                if (params.blobTracking) {
                    Mat blobs_image = input_image.clone();

                    Imgproc.threshold(blobs_image, blobs_image, params.blobThreshold, 254,
                            (params.blobThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY));

                    Size kernelSize = null;

                    switch (params.blobMorpthKernelSize) {
                    case size_3x3:
                        kernelSize = new Size(3, 3);
                        break;
                    case size_5x5:
                        kernelSize = new Size(5, 5);
                        break;
                    case size_7x7:
                        kernelSize = new Size(7, 7);
                        break;
                    case size_9x9:
                        kernelSize = new Size(9, 9);
                        break;
                    }

                    int kernelType = -1;

                    switch (params.blobMorphKernelShape) {
                    case ellipse:
                        kernelType = Imgproc.MORPH_ELLIPSE;
                        break;
                    case rect:
                        kernelType = Imgproc.MORPH_RECT;
                        break;
                    default:
                        break;
                    }

                    switch (params.blobMorphOps) {
                    case dilate:
                        Imgproc.dilate(blobs_image, blobs_image,
                                Imgproc.getStructuringElement(kernelType, kernelSize));
                        break;
                    case erode:
                        Imgproc.erode(blobs_image, blobs_image,
                                Imgproc.getStructuringElement(kernelType, kernelSize));
                        break;
                    default:
                        break;
                    }

                    if (blobFeatureDetector == null)
                        blobFeatureDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);

                    blobFeatureDetector.detect(blobs_image, mokp);
                    blobData.add(mokp);

                    if (selectedView == View.blob)
                        preview_image = blobs_image.clone();

                    blobs_image.release();
                }

                if (params.tripTracking) {

                    Mat trips_image = undistorted_image.clone();

                    if (params.tripEnableThresholding)
                        if (params.tripAdaptThreshold) {
                            Imgproc.adaptiveThreshold(trips_image, trips_image, 255,
                                    (params.tripThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY),
                                    Imgproc.ADAPTIVE_THRESH_MEAN_C, 5, params.tripThreshold * 0.256f);
                        } else {
                            Imgproc.threshold(trips_image, trips_image, params.tripThreshold, 255,
                                    (params.tripThInverted ? Imgproc.THRESH_BINARY_INV
                                            : Imgproc.THRESH_BINARY));
                        }

                    switch (params.tripMorphOps) {
                    case dilate:
                        Imgproc.dilate(trips_image, trips_image,
                                Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3)));
                        break;
                    case erode:
                        Imgproc.erode(trips_image, trips_image,
                                Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3)));
                        break;
                    default:
                        break;
                    }

                    //Imgproc.HoughCircles(tres, circ, Imgproc.CV_HOUGH_GRADIENT, 1, tres.height()/8, 80, 1+p.par4, p.par5, p.par6);
                    Imgproc.HoughCircles(trips_image, circles, Imgproc.CV_HOUGH_GRADIENT, params.tripDP,
                            params.tripCenterDist, params.tripCannyThresh, params.tripAccumThresh,
                            params.tripRadMin, params.tripRadMax);

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

                        double[] coords = circles.get(0, i);

                        if (coords == null || coords[0] <= 1 || coords[1] <= 1)
                            continue; //If the circle is off the limits, or too small, don't process it.

                        TripcodeCandidateSample tc = new TripcodeCandidateSample(undistorted_image, coords);

                        if (tc.isValid())
                            tripcodeData.add(tc);

                    }

                    if (selectedView == View.trip)
                        preview_image = trips_image.clone();
                    trips_image.release();

                }

                if (preview_image != null) {
                    camPreviewPanel.updatePreviewImage(preview_image);
                    preview_image.release();
                }

            } else {
                System.out.println("frame/cam failiure!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            running = false;
        }

        //FPS calculations
        if (camPreviewPanel != null) {
            long t = System.currentTimeMillis();
            detectTime = (t - lastFrameDetectTime);
            lastFrameDetectTime = t;
            camPreviewPanel.updateDetectTime(detectTime);
        }

    }

    //De-init
    circles.release();
    undistorted_image.release();
    input_image.release();
    inputVideo.release();
    shiftMat.release();
}

From source file:formularios.FrmCamera.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    System.out.println("Hello, OpenCV");
    // Load the native library.
    System.out.println(System.getProperty("java.library.path"));
    System.loadLibrary("opencv-300");

    VideoCapture camera = new VideoCapture(0);
    camera.open(0); //Useless
    if (!camera.isOpened()) {
        System.out.println("Camera Error");
    } else {/*  w  w  w  . j  a v  a2s .  c  om*/
        System.out.println("Camera OK?");
    }

    Mat frame = new Mat();

    //camera.grab();
    //System.out.println("Frame Grabbed");
    //camera.retrieve(frame);
    //System.out.println("Frame Decoded");

    camera.read(frame);
    System.out.println("Frame Obtained");

    /* No difference
    camera.release();
    */

    System.out.println("Captured Frame Width " + frame.width());

    Imgcodecs.imwrite("camera.jpg", frame);
    System.out.println("OK");

}

From source file:formularios.FrmCamera.java

public BufferedImage MatToBufferedImage(Mat frame) {
    //Mat() to BufferedImage
    int type = 0;
    if (frame.channels() == 1) {
        type = BufferedImage.TYPE_BYTE_GRAY;
    } else if (frame.channels() == 3) {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }//w  w  w  . ja v a 2s  . c  om
    BufferedImage image = new BufferedImage(frame.width(), frame.height(), type);
    WritableRaster raster = image.getRaster();
    DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
    byte[] data = dataBuffer.getData();
    frame.get(0, 0, data);

    return image;
}

From source file:fr.olympicinsa.riocognized.facedetector.tools.Treatment.java

public static Mat resize(Mat image, int maxW) {
    Mat resizeImage = new Mat();
    log.info("Image size: " + image.width() + "," + image.height());
    double f = (double) image.width() / (double) maxW;
    log.info("Resize factor : " + f);
    int w = (int) (image.width() / f);
    int h = (int) (image.height() / f);
    Size sz = new Size(w, h);
    log.info("Resizing image to : " + w + "," + h);
    Imgproc.resize(image, resizeImage, sz);
    return resizeImage;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Helper to create a new OpenCV Mat whose channels and
 * bit-depth mask an existing Mat./*w ww  .  j a  v  a  2s .  c om*/
 * 
 * @param m
 *       The Mat to match
 * @return
 *       A new Mat
 */
public static Mat imitate(Mat m) {
    return new Mat(m.height(), m.width(), m.type());
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public ArrayList<Line> findLines(int threshold, double minLineLength, double maxLineGap) {
    ArrayList<Line> result = new ArrayList<Line>();

    Mat lineMat = new Mat();
    Imgproc.HoughLinesP(getCurrentMat(), lineMat, 1, PConstants.PI / 180.0, threshold, minLineLength,
            maxLineGap);//  www  . j  a  va 2s  .  co m
    for (int i = 0; i < lineMat.width(); i++) {
        double[] coords = lineMat.get(0, i);
        result.add(new Line(coords[0], coords[1], coords[2], coords[3]));
    }

    return result;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * /*from  w w  w .  j ava2s .c  om*/
 * @param src
 *       A Mat of type 8UC4 with channels arranged as BGRA.
 * @return
 *       A Mat of type 8UC1 in grayscale.
 */
public static Mat gray(Mat src) {
    Mat result = new Mat(src.height(), src.width(), CvType.CV_8UC1);
    Imgproc.cvtColor(src, result, Imgproc.COLOR_BGRA2GRAY);

    return result;
}