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, int lineType, boolean bottomLeftOrigin) 

Source Link

Usage

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

License:Open Source License

@NonNull
public static Mat createDescriptionMat(String desc, int width) {
    int[] baseline = new int[1];
    Size textSizeDesc = Imgproc.getTextSize(desc, Core.FONT_HERSHEY_SIMPLEX, TITLE_FONT_SIZE, 1, baseline);
    Mat descMat = new Mat((int) Math.ceil(textSizeDesc.height) * 3, width, CvType.CV_8UC3, LAB_WHITE);
    Imgproc.putText(descMat, desc, new Point(2, descMat.height() - textSizeDesc.height),
            Core.FONT_HERSHEY_SIMPLEX, TITLE_FONT_SIZE, LAB_GREY, 2, Core.LINE_AA, false);

    return descMat;
}

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

License:Open Source License

/**
 * Create Mat with swatches for the colors in the color chart range and also write the value.
 *
 * @param colors the colors to draw// w  w  w  .  ja  va  2 s. c  om
 * @param width  the final width of the Mat
 * @return the created Mat
 */
@NonNull
public static Mat createColorRangeMatSingle(@NonNull JSONArray colors, int width) {

    double gutterWidth = X_MARGIN;
    if (colors.length() > 10) {
        gutterWidth = 2d;
        width -= 10;
    }

    double xTranslate = (double) width / (double) colors.length();

    Mat colorRangeMat = new Mat((int) xTranslate + MEASURE_LINE_HEIGHT, width, CvType.CV_8UC3, LAB_WHITE);

    double previousPos = 0;
    for (int d = 0; d < colors.length(); d++) {
        try {

            JSONObject colorObj = colors.getJSONObject(d);

            double value = colorObj.getDouble(SensorConstants.VALUE);
            JSONArray lab = colorObj.getJSONArray(SensorConstants.LAB);
            Scalar scalarLab = new Scalar((lab.getDouble(0) / 100) * MAX_RGB_INT_VALUE, lab.getDouble(1) + 128,
                    lab.getDouble(2) + 128);

            //draw a rectangle filled with color for result value
            Point topLeft = new Point(xTranslate * d, Y_COLOR_RECT);
            Point bottomRight = new Point(topLeft.x + xTranslate - gutterWidth, Y_COLOR_RECT + xTranslate);
            Imgproc.rectangle(colorRangeMat, topLeft, bottomRight, scalarLab, -1);

            Size textSizeValue = Imgproc.getTextSize(DECIMAL_FORMAT.format(value), Core.FONT_HERSHEY_SIMPLEX,
                    NORMAL_FONT_SCALE, 2, null);
            double x = topLeft.x + (bottomRight.x - topLeft.x) / 2 - textSizeValue.width / 2;
            //draw color value below rectangle. Skip if too close to the previous label
            if (x > previousPos + MIN_COLOR_LABEL_WIDTH || d == 0) {
                previousPos = x;

                String label;
                //no decimal places if too many labels to fit
                if (colors.length() > 10) {
                    label = String.format(Locale.getDefault(), "%.0f", value);
                } else {
                    label = DECIMAL_FORMAT.format(value);
                }

                //adjust x if too close to edge
                if (x + textSizeValue.width > colorRangeMat.width()) {
                    x = colorRangeMat.width() - textSizeValue.width;
                }

                Imgproc.putText(colorRangeMat, label,
                        new Point(x, Y_COLOR_RECT + xTranslate + textSizeValue.height + BORDER_SIZE),
                        Core.FONT_HERSHEY_SIMPLEX, NORMAL_FONT_SCALE, LAB_GREY, 2, Core.LINE_AA, false);
            }

        } catch (JSONException e) {
            Timber.e(e);
        }
    }
    return colorRangeMat;
}

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

License:Open Source License

/**
 * Create Mat to hold a rectangle for each color with the corresponding value.
 *
 * @param patches the patches on the strip
 * @param width   the width of the Mat to be returned
 * @return the Mat with the color range/*from  w w w.  jav  a 2 s  .  c o m*/
 */
@NonNull
public static Mat createColorRangeMatGroup(@NonNull List<StripTest.Brand.Patch> patches, int width) {

    // vertical size of mat: size of color block - X_MARGIN + top distance

    double xTranslate = (double) width / (double) patches.get(0).getColors().length();
    int numPatches = patches.size();
    Mat colorRangeMat = new Mat((int) Math.ceil(numPatches * (xTranslate + X_MARGIN) - X_MARGIN), width,
            CvType.CV_8UC3, LAB_WHITE);

    JSONArray colors;
    int offset = 0;
    for (int p = 0; p < numPatches; p++) {
        colors = patches.get(p).getColors();
        for (int d = 0; d < colors.length(); d++) {
            try {

                JSONObject colorObj = colors.getJSONObject(d);

                double value = colorObj.getDouble(SensorConstants.VALUE);
                JSONArray lab = colorObj.getJSONArray(SensorConstants.LAB);
                Scalar scalarLab = new Scalar((lab.getDouble(0) / 100) * MAX_RGB_INT_VALUE,
                        lab.getDouble(1) + 128, lab.getDouble(2) + 128);

                //draw a rectangle filled with color for result value
                Point topLeft = new Point(xTranslate * d, offset);
                Point bottomRight = new Point(topLeft.x + xTranslate - X_MARGIN,
                        xTranslate + offset - X_MARGIN);
                Imgproc.rectangle(colorRangeMat, topLeft, bottomRight, scalarLab, -1);

                //draw color value below rectangle
                if (p == 0) {
                    Size textSizeValue = Imgproc.getTextSize(DECIMAL_FORMAT.format(value),
                            Core.FONT_HERSHEY_SIMPLEX, NORMAL_FONT_SCALE, 1, null);
                    Point centerText = new Point(
                            topLeft.x + (bottomRight.x - topLeft.x) / 2 - textSizeValue.width / 2,
                            colorRangeMat.height() - textSizeValue.height);
                    Imgproc.putText(colorRangeMat, DECIMAL_FORMAT.format(value), centerText,
                            Core.FONT_HERSHEY_SIMPLEX, NORMAL_FONT_SCALE, LAB_GREY, 2, Core.LINE_AA, false);
                }

            } catch (JSONException e) {
                Timber.e(e);
            }
        }
        offset += xTranslate;
    }
    return colorRangeMat;
}

From source file:org.lasarobotics.vision.image.Drawing.java

License:Open Source License

public static void drawText(Mat img, String text, Point origin, float scale, Color color,
        Anchor locationOnImage) {/*from www . ja  v a  2s.  c  om*/
    if (locationOnImage == Anchor.BOTTOMLEFT)
        Transform.flip(img, Transform.FlipType.FLIP_ACROSS_Y);
    Imgproc.putText(img, text, origin, Core.FONT_HERSHEY_SIMPLEX, scale, color.getScalarRGBA(), 2, Core.LINE_8,
            (locationOnImage == Anchor.BOTTOMLEFT || locationOnImage == Anchor.BOTTOMLEFT_UNFLIPPED_Y));
    if (locationOnImage == Anchor.BOTTOMLEFT)
        Transform.flip(img, Transform.FlipType.FLIP_ACROSS_Y);
}