简体   繁体   English

在 Marvin 图像处理框架 Java 中绘制文本

[英]Draw text in the Marvin Image Processing Framework Java

I'm working on the classifying object in the image.我正在处理图像中的分类对象。

I'm using Marvin Image Processing Framework , and I'm successfully segmenting object, but I want to insert text on the image我正在使用Marvin Image Processing Framework ,并且我成功地分割了对象,但我想在图像上插入文本

在此处输入图片说明

This is the output of my image segmentation, and I want to draw text above the object by condition.这是我的图像分割的输出,我想按条件在对象上方绘制文本。

For example, I write function that calculate average diagonal of each rectangle, and I insert "bolt" if rectangle's diagonal is larger than average.例如,我编写了计算每个矩形的平均对角线的函数,如果矩形的对角线大于平均值,则插入“螺栓”。

However, I couldn't find any method to insert text with using Marvin Image Processing Framework.但是,我找不到任何使用 Marvin 图像处理框架插入文本的方法。

This is part of my code:这是我的代码的一部分:

public Recognition() {
    MarvinImage input = MarvinImageIO.loadImage("Parts1.jpg");
    MarvinImage copy = input.clone();


    filterBlue(copy);
    MarvinImage bin = MarvinColorModelConverter.rgbToBinary(copy, 127);
    morphologicalClosing(bin.clone(), bin, MarvinMath.getTrueMatrix(30, 30));
    copy = MarvinColorModelConverter.binaryToRgb(bin);
    MarvinSegment[] marvSeg = floodfillSegmentation(copy);
    calculateAvg(marvSeg);
    for(int i = 1; i < marvSeg.length; i++)
    {
        MarvinSegment segment = marvSeg[i];
        input.drawRect(segment.x1, segment.y1, segment.width, segment.height, Color.ORANGE);
        input.drawRect(segment.x1+1, segment.y1+1, segment.width, segment.height, Color.ORANGE);
        if (calcDiag(segment.width, segment.height) > recDiagonalAverage)
        {
            //draw string "bolt" if current diagonal is larger than average
        }
    }

    MarvinImageIO.saveImage(input, "output.jpg");
}

If I don't have any method to insert with Marvin Image Processing Framework, How can I insert text with these code?如果我没有使用 Marvin 图像处理框架插入的任何方法,如何使用这些代码插入文本?

Every time you need a rendering feature not provided by Marvin, but provided by Java Graphics, you can do the following:每次你需要一个不是由 Marvin 提供,而是由 Java Graphics 提供的渲染特性时,你可以执行以下操作:

  1. Get a BufferedImage representation from a MarvinImage object using image.getBufferedImageNoAlpha();使用image.getBufferedImageNoAlpha()从 MarvinImage 对象获取 BufferedImage 表示
  2. Get the Graphics2D from the BufferedImage object.从 BufferedImage 对象中获取 Graphics2D。
  3. Use Graphics2D rendering algorithms使用 Graphics2D 渲染算法
  4. Set the BufferedImage back to the MarvinImage using image.setBufferedImage(bufImage);使用image.setBufferedImage(bufImage);将 BufferedImage 设置回MarvinImage;

The example below uses a hypothetical MarvinSegment object created using the coordinates of your output.jpg image.下面的示例使用使用output.jpg图像的坐标创建的假设 MarvinSegment 对象。 You just need to add the drawStringMarvin(...) to your code.您只需要将drawStringMarvin(...)添加到您的代码中。

Parts1_output_2.jpg: Parts1_output_2.jpg:

在此处输入图片说明

Source code:源代码:

public class DrawStringExample {

    private static Font FONT = new Font("Verdana", Font.BOLD, 28);

    public DrawStringExample() {
        MarvinImage image = MarvinImageIO.loadImage("./res/Parts1_output.jpg");
        MarvinSegment segment = new MarvinSegment(537, 26, 667, 96);
        drawStringMarvin("bolt", segment, image);
        MarvinImageIO.saveImage(image, "./res/Parts1_output_2.jpg");
    }

    private void drawStringMarvin(String text, MarvinSegment segment, MarvinImage image) {
        BufferedImage bufImage = image.getBufferedImageNoAlpha();
        Graphics2D g2d = (Graphics2D)bufImage.getGraphics();
        g2d.setFont(FONT);
        g2d.drawString(text, segment.x1, segment.y1+FONT.getSize());
        image.setBufferedImage(bufImage);   
    }

    public static void main(String[] args) {
        new DrawStringExample();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM