好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java OpenCV图像处理之图形与文字绘制

前言

代码地址

序號 名稱 方法
1 圖像 添加文字 Imgproc.putText
2 圖像 畫直綫 Imgproc.line
3 圖像 畫橢圓 Imgproc.ellipse
4 圖像 畫矩形 Imgproc.rectangle
5 圖像 畫園 Imgproc.circle
6 圖像 填充 Imgproc.fillConvexPoly

核心代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

package com.xu.opencv.canva;

 

import java.io.File;

import java.util.ArrayList;

import java.util.List;

 

import org.opencv.core.Mat;

import org.opencv.core.MatOfPoint;

import org.opencv.core.Point;

import org.opencv.core.Scalar;

import org.opencv.core.Size;

import org.opencv.highgui.HighGui;

import org.opencv.imgcodecs.Imgcodecs;

import org.opencv.imgproc.Imgproc;

 

/**

  * @version V-1.0

  * @Title: Canvas.java

  * @Package com.xu.opencv.canvas

  * @Description: TODO

  * @author: hyacinth

  * @date: 2019年1月26日 下午5:08:18

  * @Copyright: 2019 hyacinth

  */

public class Canvas {

 

     static {

         String os = System.getProperty( "os.name" );

         String type = System.getProperty( "sun.arch.data.model" );

         if (os.toUpperCase().contains( "WINDOWS" )) {

             File lib;

             if (type.endsWith( "64" )) {

                 lib = new File( "lib\\OpenCV-455\\x64\\" + System.mapLibraryName( "opencv_java455" ));

             } else {

                 lib = new File( "lib\\OpenCV-455\\x86\\" + System.mapLibraryName( "opencv_java455" ));

             }

             System.load(lib.getAbsolutePath());

         }

     }

 

     public static void main(String[] args) {

         fillPoly();

     }

 

     /**

      * OpenCV-4.0.0 图像文字

      *

      * @return: void

      * @date: 2019年1月26日 下午5:37:04

      */

     public static void putText() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         Imgproc.putText(src, "test text" , new Point( 60 , 60 ), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0 , new Scalar( 0 , 255 , 0 ), 1 , Imgproc.LINE_AA, false );

         HighGui.imshow( "putText" , src);

         HighGui.waitKey( 1 );

     }

 

     /**

      * OpenCV-4.0.0 图像画线

      *

      * @return: void

      * @date: 2019年1月17日 下午8:32:41

      */

     public static void line() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         Imgproc.line(src, new Point( 10 , 50 ), new Point( 100 , 50 ), new Scalar( 0 , 0 , 255 ), 1 , Imgproc.LINE_AA);

         HighGui.imshow( "图像画线" , src);

         HighGui.waitKey( 1 );

     }

 

     /**

      * OpenCV-4.0.0 图像椭圆

      *

      * @return: void

      * @date: 2019年1月17日 下午8:32:56

      */

     public static void ellipse() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         Imgproc.ellipse(src, new Point( 200 , 200 ), new Size( 90 , 50 ), 0 , 0 , 360 , new Scalar( 0 , 0 , 255 ), 1 , Imgproc.LINE_AA);

         HighGui.imshow( "图像椭圆" , src);

         HighGui.waitKey( 1 );

     }

 

     /**

      * OpenCV-4.0.0 图像矩形

      *

      * @return: void

      * @date: 2019年1月17日 下午8:33:08

      */

     public static void rectangle() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         Imgproc.rectangle(src, new Point( 10 , 10 ), new Point( 200 , 200 ), new Scalar( 0 , 255 , 0 ));

         HighGui.imshow( "图像矩形" , src);

         HighGui.waitKey( 1 );

     }

 

     /**

      * OpenCV-4.0.0 图像画圆

      *

      * @return: void

      * @date: 2019年1月17日 下午8:33:27

      */

     public static void circle() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         Imgproc.circle(src, new Point( 50 , 50 ), 20 , new Scalar( 0 , 255 , 0 ), 2 , Imgproc.LINE_8);

         HighGui.imshow( "图像画圆" , src);

         HighGui.waitKey( 1 );

     }

 

     /**

      * OpenCV-4.0.0 图像填充

      *

      * @return: void

      * @date: 2019年1月26日 下午6:25:55

      */

     public static void fillPoly() {

         Mat src = Imgcodecs.imread( "D:\\OneDrive\\桌面\\5.jpeg" );

         // 1

         List<Point> points1 = new ArrayList<>();

         points1.add( new Point( 0 , 100 ));

         points1.add( new Point( 50 , 50 ));

         points1.add( new Point( 100 , 100 ));

         points1.add( new Point( 150 , 50 ));

         points1.add( new Point( 200 , 100 ));

         points1.add( new Point( 100 , 250 ));

         Point[] array = points1.stream().toArray(Point[]:: new );

         MatOfPoint point = new MatOfPoint(array);

         Imgproc.fillConvexPoly(src, point, new Scalar( 0 , 255 , 0 ), Imgproc.LINE_AA);

         // 2

         List<Point> points2 = new ArrayList<>();

         points2.add( new Point( 100 , 100 ));

         points2.add( new Point( 150 , 50 ));

         points2.add( new Point( 200 , 100 ));

         Imgproc.fillConvexPoly(src, new MatOfPoint(points2.stream().toArray(Point[]:: new )), new Scalar( 0 , 255 , 0 ), Imgproc.LINE_AA);

 

         HighGui.imshow( "图像填充" , src);

         HighGui.waitKey( 1 );

     }

 

}

效果图

到此这篇关于Java OpenCV图像处理之图形与文字绘制的文章就介绍到这了,更多相关Java OpenCV绘制图形 文字内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_34814092/article/details/84990342

查看更多关于Java OpenCV图像处理之图形与文字绘制的详细内容...

  阅读:18次