Example usage for org.opencv.imgproc Imgproc putText

List of usage examples for org.opencv.imgproc Imgproc putText

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc putText.

Prototype

public static void putText(Mat img, String text, Point org, int fontFace, double fontScale, Scalar color,
            int thickness) 

Source Link

Usage

From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatOperation.java

License:Open Source License

public static void drawRectangleAndLabelOnPreview(Mat img, Rect face, String label, boolean front_camera) {
    Point tl = drawRectangleOnPreview(img, face, front_camera);
    Imgproc.putText(img, label, tl, Core.FONT_HERSHEY_PLAIN, FONT_SIZE, FACE_RECT_COLOR, THICKNESS);
}

From source file:com.trandi.opentld.TLDView.java

License:Apache License

@Override
public Mat onCameraFrame(Mat originalFrame) {
    try {/*from w  w  w  .  j  a v a2s . c o  m*/
        // Image is too big and this requires too much CPU for a phone, so scale everything down...
        Imgproc.resize(originalFrame, _workingFrame, WORKING_FRAME_SIZE);
        final Size workingRatio = new Size(originalFrame.width() / WORKING_FRAME_SIZE.width,
                originalFrame.height() / WORKING_FRAME_SIZE.height);
        // usefull to see what we're actually working with...
        _workingFrame.copyTo(originalFrame.submat(originalFrame.rows() - _workingFrame.rows(),
                originalFrame.rows(), 0, _workingFrame.cols()));

        if (_trackedBox != null) {
            if (_tld == null) { // run the 1st time only
                Imgproc.cvtColor(_workingFrame, _lastGray, Imgproc.COLOR_RGB2GRAY);
                _tld = new Tld(_tldProperties);
                final Rect scaledDownTrackedBox = scaleDown(_trackedBox, workingRatio);
                Log.i(Util.TAG, "Working Ration: " + workingRatio + " / Tracking Box: " + _trackedBox
                        + " / Scaled down to: " + scaledDownTrackedBox);
                try {
                    _tld.init(_lastGray, scaledDownTrackedBox);
                } catch (Exception eInit) {
                    // start from scratch, you have to select an init box again !
                    _trackedBox = null;
                    _tld = null;
                    throw eInit; // re-throw it as it will be dealt with later
                }
            } else {
                Imgproc.cvtColor(_workingFrame, _currentGray, Imgproc.COLOR_RGB2GRAY);

                _processFrameStruct = _tld.processFrame(_lastGray, _currentGray);
                drawPoints(originalFrame, _processFrameStruct.lastPoints, workingRatio, new Scalar(255, 0, 0));
                drawPoints(originalFrame, _processFrameStruct.currentPoints, workingRatio,
                        new Scalar(0, 255, 0));
                drawBox(originalFrame, scaleUp(_processFrameStruct.currentBBox, workingRatio),
                        new Scalar(0, 0, 255));

                _currentGray.copyTo(_lastGray);

                // overlay the current positive examples on the real image(needs converting at the same time !)
                //copyTo(_tld.getPPatterns(), originalFrame);
            }
        }
    } catch (Exception e) {
        _errMessage = e.getClass().getSimpleName() + " / " + e.getMessage();
        Log.e(Util.TAG, "TLDView PROBLEM", e);
    }

    if (_errMessage != null) {
        Imgproc.putText(originalFrame, _errMessage, new Point(0, 300), Core.FONT_HERSHEY_PLAIN, 1.3d,
                new Scalar(255, 0, 0), 2);
    }

    return originalFrame;
}

From source file:edu.ucue.tfc.Modelo.VideoProcessor.java

public void writeOnFrame(String text) {
    Imgproc.putText(getFrame(), text, textPosition, Core.FONT_HERSHEY_SIMPLEX, 0.7, fontColor, 2);
}

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

License:Open Source License

/**
 * Writes the specified {@code text} on the {@code frame}.
 * @param text   the text to be written on the {@code frame}.
 *///from  w ww . j a  v  a 2 s  .com
public void writeOnFrame(String text) {
    Imgproc.putText(getFrame(), text, textPosition, Core.FONT_HERSHEY_SIMPLEX, 0.7, fontColor, 2);
}

From source file:opencvdemos.BallGame.java

License:Apache License

private Image grabFrame() {
    // Init everything
    Image imageToShow = null;/*w w w  .  j av  a2  s .c o  m*/
    Mat frame = new Mat();

    // Check if the capture is open
    if (this.capture.isOpened()) {
        try {
            // Read the current frame
            this.capture.read(frame);
            // Flip image for easy object manipulation
            Core.flip(frame, frame, 1);

            // If the frame is not empty, process it
            if (!frame.empty()) {
                // Init
                Mat blurredImage = new Mat();
                Mat hsvImage = new Mat();
                Mat mask = new Mat();
                Mat morphOutput = new Mat();

                // Remove some noise
                Imgproc.blur(frame, blurredImage, new Size(7, 7));

                // Convert the frame to HSV
                Imgproc.cvtColor(blurredImage, hsvImage, Imgproc.COLOR_BGR2HSV);

                // Get thresholding values from the UI
                // Remember: H ranges 0-180, S and V range 0-255
                Scalar minValues = new Scalar(this.hueStart.getValue(), this.saturationStart.getValue(),
                        this.valueStart.getValue());
                Scalar maxValues = new Scalar(this.hueStop.getValue(), this.saturationStop.getValue(),
                        this.valueStop.getValue());

                // Show the current selected HSV range
                String valuesToPrint = "Hue range: " + minValues.val[0] + "-" + maxValues.val[0]
                        + ". Sat. range: " + minValues.val[1] + "-" + maxValues.val[1] + ". Value range: "
                        + minValues.val[2] + "-" + maxValues.val[2];
                hsvCurrentValues.setText(valuesToPrint);

                // Threshold HSV image to select object
                Core.inRange(hsvImage, minValues, maxValues, mask);
                // Show the partial output
                maskImage.getGraphics().drawImage(this.mat2Image(mask), 0, 0, 205, 154, null);

                // Morphological operators
                // Dilate with large element, erode with small ones
                Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(24, 24));
                Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12, 12));

                Imgproc.erode(mask, morphOutput, erodeElement);
                Imgproc.erode(mask, morphOutput, erodeElement);

                Imgproc.dilate(mask, morphOutput, dilateElement);
                Imgproc.dilate(mask, morphOutput, dilateElement);

                // Show the partial output
                morphImage.getGraphics().drawImage(this.mat2Image(morphOutput), 0, 0, 205, 154, null);

                // Find the object(s) contours and show them
                frame = this.findAndDrawObjects(morphOutput, frame);

                // Calculate centers and move ball
                Mat temp = new Mat();
                morphOutput.copyTo(temp);
                List<MatOfPoint> contours = new ArrayList<>();
                Imgproc.findContours(temp, contours, new Mat(), Imgproc.RETR_EXTERNAL,
                        Imgproc.CHAIN_APPROX_SIMPLE);
                for (int i = 0; i < contours.size(); i++) {
                    Rect objectBoundingRectangle = Imgproc.boundingRect(contours.get(i));
                    int x = objectBoundingRectangle.x + objectBoundingRectangle.width / 2;
                    int y = objectBoundingRectangle.y + objectBoundingRectangle.height / 2;

                    // Move ball
                    if (!ballChanged) {
                        if (b.x > objectBoundingRectangle.x
                                && b.x < objectBoundingRectangle.x + objectBoundingRectangle.width
                                && b.y > objectBoundingRectangle.y
                                && b.y < objectBoundingRectangle.y + objectBoundingRectangle.height) {
                            b.dx = -b.dx;
                            b.dy = -b.dy;
                            ballChanged = true;
                        }
                    }

                    // Show crosshair
                    Imgproc.circle(frame, new Point(x, y), 20, new Scalar(0, 255, 0), 2);
                    Imgproc.line(frame, new Point(x, y), new Point(x, y - 25), new Scalar(0, 255, 0), 2);
                    Imgproc.line(frame, new Point(x, y), new Point(x, y + 25), new Scalar(0, 255, 0), 2);
                    Imgproc.line(frame, new Point(x, y), new Point(x - 25, y), new Scalar(0, 255, 0), 2);
                    Imgproc.line(frame, new Point(x, y), new Point(x + 25, y), new Scalar(0, 255, 0), 2);
                    Imgproc.putText(frame, "Tracking object at (" + x + "," + y + ")", new Point(x, y), 1, 1,
                            new Scalar(255, 0, 0), 2);
                }
                ballChanged = false;

                // Move and draw the ball
                if (b.dx < 0)
                    b.dx = ballSpeed.getValue() * -1;
                else
                    b.dx = ballSpeed.getValue();
                if (b.dy < 0)
                    b.dy = ballSpeed.getValue() * -1;
                else
                    b.dy = ballSpeed.getValue();
                b.move();
                Imgproc.circle(frame, new Point(b.x, b.y), b.r, new Scalar(255, 0, 255), -1);

                // convert the Mat object (OpenCV) to Image (Java AWT)
                imageToShow = mat2Image(frame);
            }

        } catch (Exception e) {
            // log the error
            System.err.println("Exception during the frame elaboration: " + e);
        }
    }

    return imageToShow;
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerDetector.java

License:Open Source License

@Override
public void process(ImageBuffers buffers) {
    final ArrayList<MatOfPoint> contours = new ArrayList<>();
    final Mat hierarchy = new Mat();
    // Make sure the image is rotated before the contours are generated, if necessary
    //if (Feature.get(this.context, R.bool.feature_combined_markers).isEnabled() || outlineDisplay != OutlineDisplay.none || codeDisplay == CodeDisplay.visible)
    //{/*from  w ww  . j  a  v a 2s  .  co  m*/
    // if statement commented out as there was a bug where the overlay was not getting cleared
    // after cycling through the threshold views.
    buffers.getOverlay();
    //}
    try {
        final List<Marker> foundMarkers = new ArrayList<>();
        Imgproc.findContours(buffers.getImageInGrey(), contours, hierarchy, Imgproc.RETR_TREE,
                Imgproc.CHAIN_APPROX_NONE);
        for (int i = 0; i < contours.size(); i++) {
            final Marker marker = createMarkerForNode(i, contours, hierarchy);
            if (marker != null) {
                final String markerCode = getCodeKey(marker);
                if (validCodes.isEmpty() || validCodes.contains(markerCode)) {
                    // If this marker has a minimum size set and is smaller: continue in loop.
                    Action action = experience.getActionForCode(markerCode);
                    if (action != null && action.getMinimumSize() != null) {
                        double minimumSize = action.getMinimumSize();
                        Rect boundingRect = Imgproc.boundingRect(contours.get(i));
                        if (!(boundingRect.width / (float) buffers.getImageInAnyFormat().cols() > minimumSize
                                || boundingRect.height
                                        / (float) buffers.getImageInAnyFormat().rows() > minimumSize)) {
                            continue;
                        }
                    }

                    foundMarkers.add(marker);

                    if (outlineDisplay != OutlineDisplay.none) {
                        Mat overlay = buffers.getOverlay();
                        if (outlineDisplay == OutlineDisplay.regions) {
                            double[] nodes = hierarchy.get(0, i);
                            int currentRegionIndex = (int) nodes[FIRST_NODE];

                            while (currentRegionIndex >= 0) {
                                Imgproc.drawContours(overlay, contours, currentRegionIndex, outlineColour, 4);
                                Imgproc.drawContours(overlay, contours, currentRegionIndex, regionColour, 2);

                                nodes = hierarchy.get(0, currentRegionIndex);
                                currentRegionIndex = (int) nodes[NEXT_NODE];
                            }
                        }

                        Imgproc.drawContours(overlay, contours, i, outlineColour, 7);
                        Imgproc.drawContours(overlay, contours, i, detectedColour, 5);
                    }

                    if (codeDisplay == CodeDisplay.visible) {
                        Mat overlay = buffers.getOverlay();
                        Rect bounds = Imgproc.boundingRect(contours.get(i));
                        Imgproc.putText(overlay, markerCode, bounds.tl(), Core.FONT_HERSHEY_SIMPLEX, 1,
                                outlineColour, 5);
                        Imgproc.putText(overlay, markerCode, bounds.tl(), Core.FONT_HERSHEY_SIMPLEX, 1,
                                detectedColour, 3);
                    }
                }
            }
        }

        buffers.setDetected(!foundMarkers.isEmpty());
        handler.onMarkersDetected(foundMarkers, contours, hierarchy, buffers.getImageInGrey().size());
    } finally {
        contours.clear();
        hierarchy.release();
    }
}