[英]Draw text in the Marvin Image Processing Framework Java
我正在處理圖像中的分類對象。
我正在使用Marvin Image Processing Framework ,並且我成功地分割了對象,但我想在圖像上插入文本
這是我的圖像分割的輸出,我想按條件在對象上方繪制文本。
例如,我編寫了計算每個矩形的平均對角線的函數,如果矩形的對角線大於平均值,則插入“螺栓”。
但是,我找不到任何使用 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 提供的渲染特性時,你可以執行以下操作:
下面的示例使用使用output.jpg圖像的坐標創建的假設 MarvinSegment 對象。 您只需要將drawStringMarvin(...)添加到您的代碼中。
Parts1_output_2.jpg:
源代碼:
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.