gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:43 30 4
gpt4 key购买 nike

我正在对图像中的对象进行分类。

我正在使用Marvin Image Processing Framework ,并且我成功分割了对象,但我想在图像上插入文本

enter image description here

这是我的图像分割的输出,我想按条件在对象上方绘制文本。

例如,我编写了计算每个矩形的平均对角线的函数,如果矩形的对角线大于平均值,则插入“ bolt ”。

但是,我找不到任何使用 Marvin 图像处理框架插入文本的方法。

这是我的代码的一部分:

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");
}

如果我没有任何使用 Marvin 图像处理框架插入的方法,我如何使用这些代码插入文本?

最佳答案

每当您需要非 Marvin 提供但由 Java Graphics 提供的渲染功能时,您可以执行以下操作:

  1. 使用 image.getBufferedImageNoAlpha(); 从 MarvinImage 对象获取 BufferedImage 表示形式
  2. 从 BufferedImage 对象获取 Graphics2D。
  3. 使用 Graphics2D 渲染算法
  4. 使用 image.setBufferedImage(bufImage); 将 BufferedImage 设置回 MarvinImage;

下面的示例使用一个假设的 MarvinSegment 对象,该对象是根据 output.jpg 图像的坐标创建的。您只需将 drawStringMarvin(...) 添加到代码中即可。

Parts1_output_2.jpg:

enter image description here

源代码:

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();
}
}

关于java - 在 Marvin 图像处理框架 Java 中绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519130/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com