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:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

License:Apache License

static ArrayList<double[][][]> getGradientHistograms(Path filename, int w_d, int h_d, int o_d)
        throws PoTException {
    ArrayList<double[][][]> histograms = new ArrayList<double[][][]>();

    VideoCapture capture = new VideoCapture(filename.toString());

    if (!capture.isOpened()) {
        LOG.warning("video file not opened.");

        double[][][] hist = new double[w_d][h_d][o_d];
        histograms.add(hist);/*w w w  . j  a  v a2 s  .c o  m*/
    } else {
        // variables for processing images
        Mat original_frame = new Mat();
        Mat resized = new Mat();
        Mat resized_gray = new Mat();

        // initializing a list of histogram of gradients (i.e. a list of s*s*9
        // arrays)
        for (int i = 0;; i++) {
            // capturing the video images
            capture.read(original_frame);
            if (original_frame.empty()) {
                if (original_frame.empty()) {
                    if (i == 0) {
                        throw new PoTException("Could not read the video file");
                    } else
                        break;
                }
            }

            double[][][] hist = new double[w_d][h_d][o_d];

            Imgproc.resize(original_frame, resized, new Size(frame_width, frame_height));
            Imgproc.cvtColor(resized, resized_gray, Imgproc.COLOR_BGR2GRAY);

            ArrayList<double[][]> gradients = computeGradients(resized_gray, o_d);
            updateGradientHistogram(hist, gradients);

            histograms.add(hist);
        }

        capture.release();
    }

    return histograms;
}

From source file:hotgoaldetection.HotGoalDetection.java

/**
 * @param args the command line arguments
 *//*from w  w  w .j a  v  a  2s  .  co m*/
public static void main(String[] args) throws IOException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    img = Highgui.imread("..\\Robot-26ft.png", CV_LOAD_IMAGE_COLOR);
    //CameraWindow cWindow = new CameraWindow();
    //cWindow.setVisible(true);
    Webcam.ImagePanel panel = Webcam.createPanel(img, "img");
    Webcam.ImagePanel panel2 = Webcam.createPanel(img, "hsv");
    //Webcam.ImagePanel panel3 = Webcam.createPanel(filter, "filter");

    binary = new Mat();
    hsv = new Mat();
    filter = new Mat();
    dst = new Mat();

    GaussianBlur(img, img, new Size(3, 3), 2, 2);

    while (true) {
        Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV);

        //Core.inRange(img, new Scalar(120, 170, 10), new Scalar(160, 240, 40), filter);
        //Core.inRange(hsv, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), hsv);
        Core.inRange(hsv, new Scalar(40, 52, 64), new Scalar(97, 255, 255), binary);
        erode(binary, binary, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)));

        double[] condensedMat = new double[binary.width()];
        for (int y = 0; y < binary.size().width; y++) {
            double totalX = 0;
            for (int x = 0; x < binary.size().height; x++) {
                if (binary.get(x, y)[0] == 255.0)
                    totalX++;
                //System.out.println(java.util.Arrays.toString(binary.get(x,y)));
            }
            condensedMat[y] = totalX;
        }
        double[] rightOfMat = Arrays.copyOfRange(condensedMat, 0, (condensedMat.length / 2) - 1);
        double[] leftOfMat = Arrays.copyOfRange(condensedMat, condensedMat.length / 2, condensedMat.length);
        double totalRight = 0, totalLeft = 0;

        for (double s : rightOfMat)
            totalRight += s;
        for (double s : leftOfMat)
            totalLeft += s;

        //System.out.println(totalRight + ", " + totalLeft);

        if (totalRight > totalLeft)
            direction = true;
        else
            direction = false;

        totalLeft = 0;
        totalRight = 0;

        panel.updateImage(toBufferedImage(img));
        panel2.updateImage(toBufferedImage(binary));
        // panel3.updateImage(toBufferedImage(filter));
    }
}

From source file:hotgoaldetection.HotGoalDetection.java

public static void takeScreenshot(int mat) {
    Date tempDate = new Date();
    Mat matFrame = new Mat();

    switch (mat) {
    case 1:// w ww.  j  a v a2s  .  co  m
        matFrame = img;
        break;
    case 2:
        matFrame = dst;
        break;
    case 3:
        matFrame = hsv;
        break;
    default:
        matFrame = filter;
        break;
    }

    //camera.read(matFrame);
    Highgui.imwrite("screenshots\\ Screenshot " + counter + " -"
            + String.format("%1$s %2$tb %2$td at %2$tH %2$tM %2$tS", "", tempDate) + ".jpeg", matFrame);
    counter++;
}

From source file:hotgoaldetection.Webcam.java

public static void main(String[] args) throws Exception {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    //  VideoCapture camera = new VideoCapture(0);
    Mat frame = new Mat();
    ImagePanel panel = createPanel(camera, "camera");
    while (true) {
        frame = camera;/*from w w w  .  jav  a 2  s .  c  o  m*/
        panel.updateImage(toBufferedImage(frame));
    }
}

From source file:hotgoaldetection.Webcam.java

public static void takeScreenshot() {
    Mat matFrame = new Mat();
    matFrame = camera;//w ww.  j ava 2s .  c  om
    Highgui.imwrite("screenshots\\screenshot " + counter + ".jpeg", matFrame);
    counter++;
}

From source file:houghtransform.Capture.java

@Override
public void run() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat scr = new Mat();
    Mat edges = new Mat();

    while (!shouldStop) {
        try {//from  w  w  w .  j a  va 2 s .  c o m
            webCam.read(scr);

            // Canny Edge Detector
            Imgproc.Canny(scr, edges, 50, 200, 3, false);

            // Hough Transform

            switch (choiceT) {
            case 1:
                transf = new OpenCV();
                break;
            case 2:
                transf = new MyTransform();
                break;
            default:
                throw new IOException("Transform wasn't choiced!!!");
            }
            transf.houghTransform(edges);
            scr = transf.drawLines(scr);

            syncJFrame.showVideo(convertMatToBufferedImage(edges), convertMatToBufferedImage(scr));
        } catch (IOException ex) {
            Logger.getLogger(Capture.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:houghtransform.Picture.java

public Picture(String fileName, int choiceT) throws IOException {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat scr = Imgcodecs.imread(fileName);
    if (scr.empty())
        throw new IOException("File not exist!!!");

    Mat edges = new Mat();
    // Canny Edge Detector
    Imgproc.Canny(scr, edges, 50, 200, 3, false);

    // Hough Transform
    Transform transf;//from   w  w  w .  j a v  a2  s. c o  m
    switch (choiceT) {
    case 1:
        transf = new OpenCV();
        break;
    case 2:
        transf = new MyTransform();
        break;
    default:
        throw new IOException("Transform wasn't choiced!!!");
    }

    transf.houghTransform(edges);
    scr = transf.drawLines(scr);

    PictureJFrame x = new PictureJFrame(convertMatToBufferedImage(edges), convertMatToBufferedImage(scr));
    x.setVisible(true);

}

From source file:houghtransform.transform_process.OpenCV.java

public OpenCV() {
    lines = new Mat();
}

From source file:hu.unideb.fksz.VideoProcessor.java

License:Open Source License

/**
 * Does the main loop, if we reach the penultimate frame,
 * it means we have reached the end of the end of the video.
 *//* www .  ja  va 2 s. c o  m*/
public void processVideo() {
    do {
        Mat tmp = new Mat();
        video.read(tmp);
        if (!tmp.empty()) {
            frame = tmp.clone();
            tmp.release();
            if (frameCounter < (getFrameCount() / 2) - 1) {
                frameCounter++;
                if (getMinutes() > 0) {
                    carsPerMinute = getDetectedCarsCount() / getMinutes();
                }

                processFrame(getFrame());
            } else {
                frameCounter = 0;
                finished = true;

                logger.trace("Restarting..");
                setFramePos(1);
            }
        } else {
            logger.warn("Empty image!");
            frameCounter = 0;
            finished = true;

            logger.trace("Restarting..");
            setFramePos(1);
        }
    } while (frameCounter > (getFrameCount() / 2) - 2);
}

From source file:hu.unideb.fksz.VideoProcessor.java

License:Open Source License

/**
 * Gets an {@code Image}, converted from the specified index
 * of the {@code video}.//from   ww w.  j ava  2 s  .com
 *
 * @param pos   the position from which the frame is retrieved.
 * @return   an {@code Image}, converted from the specified index
 * of the {@code video}.
 */
public Image getImageAtPos(int pos) {
    if (video.isOpened()) {
        if (pos < getFrameCount() && pos > 0) {
            setFramePos(pos);
            Mat tmp = new Mat();
            video.retrieve(tmp);
            setFramePos(0);
            try {
                Imgproc.resize(tmp, tmp, frameSize);
            } catch (Exception e) {
                TrafficCounterLogger.errorMessage(e.getMessage());
            }
            return convertCvMatToImage(tmp);
        } else {
            return getImageAtPos(1);
        }
    } else {
        logger.error("VideoCapture not opened!");
        return null;
    }
}