如何使用 Java OpenCV 庫將文字新增到影像?


可以使用 org.opencv.imgproc.Imgproc 類putText() 方法將文字新增到影像。此方法在給定的影像中呈現指定的文字。它接受−

  • 一個空的 mat 物件來儲存源影像。

  • 一個字串物件來指定所需的文字。

  • 一個 Point 物件,指定文字的位置。

  • 指定文字字型的整型常量。

  • 對字型特定的基本大小相乘的比例因子。

  • 一個指定文字顏色的 Scalar 物件。

  • 指定文字顏色的整數值

示例

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class AddingText {
   public static void main(String args[]) throws Exception {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the contents of the image
      String file ="D:\Images\shapes.jpg";
      Mat src = Imgcodecs.imread(file);
      //Preparing the arguments
      String text = "JavaFX 2D shapes";
      Point position = new Point(170, 280);
      Scalar color = new Scalar(0, 0, 255);
      int font = Imgproc.FONT_HERSHEY_SIMPLEX;
      int scale = 1;
      int thickness = 3;
      //Adding text to the image
      Imgproc.putText(src, text, position, font, scale, color, thickness);
      //Displaying the resultant Image
      HighGui.imshow("Contours operation", src);
      HighGui.waitKey();
   }
}

輸入影像

輸出

更新於: 2020 年 4 月 13 日

1K+ 瀏覽

開啟你的 職業

透過完成課程取得認證

開始
廣告