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:uk.ac.horizon.artcodes.detect.handler.MarkerActionDetectionHandler.java

License:Open Source License

@Override
public void onMarkersDetected(Collection<Marker> markers, ArrayList<MatOfPoint> contours, Mat hierarchy,
        Size sourceImageSize) {//from   w  w w .j  a v a  2 s .  c o  m
    countMarkers(markers);
    long now = System.currentTimeMillis();
    int best = 0;
    Action selected = null;
    for (Action action : this.experience.getActions()) {
        if (action.getMatch() == Action.Match.any) {
            for (String code : action.getCodes()) {
                int count = markerCounts.count(code);
                if (count > best) {
                    selected = action;
                    best = count;
                }
            }
        } else if (action.getMatch() == Action.Match.all) {
            int min = MAX;
            int total = 0;
            for (String code : action.getCodes()) {
                int count = markerCounts.count(code);
                min = Math.min(min, count);
                total += (count * 2);
            }

            if (min > REQUIRED && total > best) {
                best = total;
                selected = action;
            }
        }
    }

    if (best < REQUIRED) {
        if (currentAction != null) {
            if (now - lastSeen > REMAIN) {
                currentAction = null;
                this.markerActionHandler.onMarkerActionDetected(null, null, null);
            }
        }
    } else if (selected != currentAction) {
        currentAction = selected;
        lastSeen = now;
        ArrayList<MarkerImage> markerImages = null;
        if (this.markerDrawer != null) {
            Marker markerObject = null;
            for (Marker possibleMarkerObject : markers) {
                if (possibleMarkerObject.toString().equals(currentAction.getCodes().get(0))) {
                    markerObject = possibleMarkerObject;
                }
            }
            if (markerObject != null) {
                final Rect boundingRect = Imgproc.boundingRect(contours.get(markerObject.markerIndex));
                Mat thumbnailMat = this.markerDrawer.drawMarker(markerObject, contours, hierarchy, boundingRect,
                        null);
                Bitmap thumbnail = Bitmap.createBitmap(thumbnailMat.width(), thumbnailMat.height(),
                        Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(thumbnailMat, thumbnail);
                MarkerImage markerImage = new MarkerImage(markerObject.toString(), thumbnail,
                        (float) (boundingRect.tl().x / sourceImageSize.width),
                        (float) (boundingRect.tl().y / sourceImageSize.height),
                        (float) (boundingRect.width / sourceImageSize.width),
                        (float) (boundingRect.height / sourceImageSize.height));
                markerImages = new ArrayList<>(1);
                markerImages.add(markerImage);

                Log.i("SOURCEIMG", "w" + sourceImageSize.width + " h" + sourceImageSize.height);
            }
        }
        this.markerActionHandler.onMarkerActionDetected(currentAction, currentAction, markerImages);
    } else {
        for (Marker possibleMarkerObject : markers) {
            String marker = possibleMarkerObject.toString();
            for (String code : currentAction.getCodes()) {
                if (code.equals(marker)) {
                    lastSeen = now;
                    return;
                }
            }
        }
    }
}

From source file:uk.ac.horizon.artcodes.detect.handler.MultipleMarkerActionDetectionHandler.java

License:Open Source License

private MarkerImage createImageForMarker(Marker marker, ArrayList<MatOfPoint> contours, Mat hierarchy,
        Size sourceImageSize) {//from  w ww.  j a v  a 2 s .c  om
    if (marker != null) {
        final Rect boundingRect = Imgproc.boundingRect(contours.get(marker.markerIndex));
        final Mat thumbnailMat = this.markerDrawer.drawMarker(marker, contours, hierarchy, boundingRect, null);
        final Bitmap thumbnail = Bitmap.createBitmap(thumbnailMat.width(), thumbnailMat.height(),
                Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(thumbnailMat, thumbnail);
        return new MarkerImage(marker.toString(), thumbnail,
                (float) (boundingRect.tl().x / sourceImageSize.width),
                (float) (boundingRect.tl().y / sourceImageSize.height),
                (float) (boundingRect.width / sourceImageSize.width),
                (float) (boundingRect.height / sourceImageSize.height));
    }
    return null;
}

From source file:video.PictureAnalyser.java

public void Calibrate(BufferedImage img) {
    Mat frameMat = new Mat();
    frameMat = PictureView.bufferedImageToMat(img);
    pic = blur(frameMat, 3);/*from w w w .  j  ava 2s  .  com*/
    Mat imgHSV = new Mat();
    Imgproc.cvtColor(pic, imgHSV, Imgproc.COLOR_RGB2HSV);
    int height = 10;
    int width = 10;
    int hue = 0;
    int saturation = 0;
    int value = 0;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            double[] testColor = imgHSV.get((imgHSV.height() / 2) - (width / 2) + i,
                    (imgHSV.width() / 2) - (height / 2) + j);
            if (testColor[0] > 140)
                testColor[0] = testColor[0] - 180;
            hue = hue + (int) testColor[0];
            saturation = saturation + (int) testColor[1];
            value = value + (int) testColor[2];

        }
    }
    /*
     * Green values hue=55; saturation =240; value= 70;
     */
    hue = hue / (height * width);
    saturation = saturation / (height * width);
    value = value / (height * width);
    this.color.get(0).val[0] = hue - 5;
    this.color.get(0).val[1] = saturation - 40;
    this.color.get(0).val[2] = value - 40;
    this.color.get(1).val[0] = hue + 5;
    this.color.get(1).val[1] = saturation + 40;
    this.color.get(1).val[2] = value + 40;

}

From source file:video.PictureView.java

public static BufferedImage setCross(BufferedImage img) {
    Mat imgMat;
    imgMat = bufferedImageToMat(img);//from w w w .j a  va  2  s.  c o m
    Imgproc.circle(imgMat, new Point(imgMat.width() / 2, imgMat.height() / 2), 4,
            new Scalar(255, 49, 255, 255));
    img = mat2Img(imgMat);
    return img;
}

From source file:webcamcapture.WebCamCapture.java

/**
 * @param args the command line arguments
 */// w  w  w  . j  a v a2s  .co  m
public static void main(String[] args) throws IOException {
    // TODO code application logic here

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    httpRequestSend request = new httpRequestSend();
    VideoCapture camera = new VideoCapture(0);
    int x = 0;
    if (!camera.isOpened()) {
        System.out.println("Error");
    } else {
        Mat frame = new Mat();
        while (true) {
            if (camera.read(frame)) {
                System.out.println("Frame Obtained");
                System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height());
                Highgui.imwrite("camera" + x + ".jpg", frame);
                //System.out.println("OK");
                // break;

                request.request("https://fireacc.herokuapp.com/dash", "camera" + x + ".jpg");
                x++;
            }
            try {
                //}
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                System.out.println("stuck");
            }
        }
    }
    camera.release();

}

From source file:webcamfacedetect.FaceDetect.java

/**
 * Converts/writes a Mat into a BufferedImage.
 *
 * @param matrix Mat of type CV_8UC3 or CV_8UC1
 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
 *///from   w w  w . j  a  v a  2s.  c om
public boolean MatToBufferedImage(Mat matBGR) {
    long startTime = System.nanoTime();
    int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels();
    byte[] sourcePixels = new byte[width * height * channels];
    matBGR.get(0, 0, sourcePixels);
    // create new image and get reference to backing data  
    image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
    long endTime = System.nanoTime();
    //System.out.println(String.format("Elapsed time: %.2f ms", (float) (endTime - startTime) / 1000000));
    return true;
}

From source file:webcamfacedetect.WebcamFaceDetect.java

/**
 * @param args the command line arguments
 */// ww w .j av  a2s  . co  m
public static void main(String args[]) {
    // Load the native library.
    Libloader.load();
    String window_name = "Capture - Face detection";
    JFrame frame = new JFrame(window_name);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1024, 800);
    FaceDetect faceDetect = new FaceDetect();
    Processor my_processor = new Processor();
    frame.setContentPane(faceDetect);
    frame.setVisible(true);
    // Read the video stream  
    Mat webcam_image = new Mat();
    VideoCapture capture = null;
    for (int numCam = 0; numCam < 5; numCam++) {
        try {
            capture = new VideoCapture(numCam);
            break;
        } catch (Exception e) {
            System.out.println("Webcam number " + numCam + " unavailable ...");
        }
    }
    if (capture.isOpened()) {
        while (true) {
            capture.read(webcam_image);
            if (!webcam_image.empty()) {
                frame.setSize(webcam_image.width() + 40, webcam_image.height() + 60);
                // Apply the classifier to the captured image  
                webcam_image = my_processor.detect(webcam_image);
                // Display recognized image  
                faceDetect.MatToBufferedImage(webcam_image);
                faceDetect.repaint();
            } else {
                System.out.println("No captured frame. Exit.");
                break;
            }
        }
    }
}