Java OpenCV函式庫的org.opencv.imgproc套件中包含一個名為Imgproc的類別。要繪製一個填滿的多邊形,您需要呼叫這個類別的fillPoly()方法。此方法接受以下參數:
表示要在其上繪製多邊形的影像的Mat物件。
一個List對象,其中包含MatOfPoint類型的物件。
表示多邊形顏色的Scalar物件。
表示線類型的整數。
import java.util.ArrayList; import java.util.List; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; 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 DrawingFilledPolygon { public static void main(String args[]) { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the source image in to a Mat object Mat src = Imgcodecs.imread("D:\images\blank.jpg"); //Drawing a polygon List<MatOfPoint>list = new ArrayList<MatOfPoint>(); list.add(new MatOfPoint ( new Point(208, 71), new Point(421, 161), new Point(332, 52), new Point(369, 250), new Point(421, 161), new Point(226, 232), new Point(369, 250), new Point(208, 71), new Point(226, 232), new Point(332, 52))); Scalar color = new Scalar(64, 64, 64); int lineType = Imgproc.LINE_8; Imgproc.fillPoly(src, list, color, lineType); //Saving and displaying the image Imgcodecs.imwrite("arrowed_line.jpg", src); HighGui.imshow("Drawing a polygon", src); HighGui.waitKey(); } }
#執行上述程式後,會產生以下視窗−
以上是如何使用Java在OpenCV中繪製填充的多邊形?的詳細內容。更多資訊請關注PHP中文網其他相關文章!