1
- using OpenCVForUnity ;
2
- using System ;
3
-
4
- namespace NatCamWithOpenCVForUnityExample {
5
-
6
- public class ComicFilter {
7
-
8
- Mat grayMat ;
9
- Mat lineMat ;
10
- Mat maskMat ;
11
- Mat bgMat ;
12
- Mat grayDstMat ;
13
- byte [ ] grayPixels ;
14
- byte [ ] maskPixels ;
15
-
16
- public void Process ( Mat src , Mat dst ) {
17
- if ( src == null )
18
- throw new ArgumentNullException ( "src == null" ) ;
19
- if ( dst == null )
20
- throw new ArgumentNullException ( "dst == null" ) ;
21
-
22
- if ( grayMat != null && ( grayMat . width ( ) != src . width ( ) || grayMat . height ( ) != src . height ( ) ) ) {
23
- grayMat . Dispose ( ) ;
24
- grayMat = null ;
25
- lineMat . Dispose ( ) ;
26
- lineMat = null ;
27
- maskMat . Dispose ( ) ;
28
- maskMat = null ;
29
- bgMat . Dispose ( ) ;
30
- bgMat = null ;
31
- grayDstMat . Dispose ( ) ;
32
- grayDstMat = null ;
33
-
34
- grayPixels = null ;
35
- maskPixels = null ;
36
- }
37
- grayMat = grayMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
38
- lineMat = lineMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
39
- maskMat = maskMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
40
- //create a striped background.
41
- bgMat = new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 , new Scalar ( 255 ) ) ;
42
- for ( int i = 0 ; i < bgMat . rows ( ) * 2.5f ; i = i + 4 ) {
43
- Imgproc . line ( bgMat , new Point ( 0 , 0 + i ) , new Point ( bgMat . cols ( ) , - bgMat . cols ( ) + i ) , new Scalar ( 0 ) , 1 ) ;
44
- }
45
- grayDstMat = grayDstMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
46
-
47
- grayPixels = grayPixels ?? new byte [ grayMat . cols ( ) * grayMat . rows ( ) * grayMat . channels ( ) ] ;
48
- maskPixels = maskPixels ?? new byte [ maskMat . cols ( ) * maskMat . rows ( ) * maskMat . channels ( ) ] ;
49
-
50
-
51
- Imgproc . cvtColor ( src , grayMat , Imgproc . COLOR_RGBA2GRAY ) ;
52
- bgMat . copyTo ( grayDstMat ) ;
53
- Imgproc . GaussianBlur ( grayMat , lineMat , new Size ( 3 , 3 ) , 0 ) ;
54
- grayMat . get ( 0 , 0 , grayPixels ) ;
55
-
56
- for ( int i = 0 ; i < grayPixels . Length ; i ++ ) {
57
- maskPixels [ i ] = 0 ;
58
- if ( grayPixels [ i ] < 70 ) {
59
- grayPixels [ i ] = 0 ;
60
- maskPixels [ i ] = 1 ;
61
- } else if ( 70 <= grayPixels [ i ] && grayPixels [ i ] < 120 ) {
62
- grayPixels [ i ] = 100 ;
63
- } else {
64
- grayPixels [ i ] = 255 ;
65
- maskPixels [ i ] = 1 ;
66
- }
67
- }
68
-
69
- grayMat . put ( 0 , 0 , grayPixels ) ;
70
- maskMat . put ( 0 , 0 , maskPixels ) ;
71
- grayMat . copyTo ( grayDstMat , maskMat ) ;
72
-
73
- Imgproc . Canny ( lineMat , lineMat , 20 , 120 ) ;
74
- lineMat . copyTo ( maskMat ) ;
75
- Core . bitwise_not ( lineMat , lineMat ) ;
76
- lineMat . copyTo ( grayDstMat , maskMat ) ;
77
-
78
- Imgproc . cvtColor ( grayDstMat , dst , Imgproc . COLOR_GRAY2RGBA ) ;
79
- }
80
-
81
- public void Dispose ( ) {
82
- foreach ( var mat in new [ ] { grayMat , lineMat , maskMat , bgMat , grayDstMat } )
83
- if ( mat != null )
84
- maskMat . Dispose ( ) ;
85
- grayDstMat =
86
- bgMat =
87
- maskMat =
88
- lineMat =
89
- grayMat = null ;
90
- grayPixels =
91
- maskPixels = null ;
92
- }
93
- }
1
+ using System ;
2
+ using OpenCVForUnity . CoreModule ;
3
+ using OpenCVForUnity . ImgprocModule ;
4
+
5
+ namespace NatCamWithOpenCVForUnityExample
6
+ {
7
+
8
+ public class ComicFilter
9
+ {
10
+
11
+ Mat grayMat ;
12
+ Mat lineMat ;
13
+ Mat maskMat ;
14
+ Mat bgMat ;
15
+ Mat grayDstMat ;
16
+ byte [ ] grayPixels ;
17
+ byte [ ] maskPixels ;
18
+
19
+ public void Process ( Mat src , Mat dst )
20
+ {
21
+ if ( src == null )
22
+ throw new ArgumentNullException ( "src == null" ) ;
23
+ if ( dst == null )
24
+ throw new ArgumentNullException ( "dst == null" ) ;
25
+
26
+ if ( grayMat != null && ( grayMat . width ( ) != src . width ( ) || grayMat . height ( ) != src . height ( ) ) ) {
27
+ grayMat . Dispose ( ) ;
28
+ grayMat = null ;
29
+ lineMat . Dispose ( ) ;
30
+ lineMat = null ;
31
+ maskMat . Dispose ( ) ;
32
+ maskMat = null ;
33
+ bgMat . Dispose ( ) ;
34
+ bgMat = null ;
35
+ grayDstMat . Dispose ( ) ;
36
+ grayDstMat = null ;
37
+
38
+ grayPixels = null ;
39
+ maskPixels = null ;
40
+ }
41
+ grayMat = grayMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
42
+ lineMat = lineMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
43
+ maskMat = maskMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
44
+ //create a striped background.
45
+ bgMat = new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 , new Scalar ( 255 ) ) ;
46
+ for ( int i = 0 ; i < bgMat . rows ( ) * 2.5f ; i = i + 4 ) {
47
+ Imgproc . line ( bgMat , new Point ( 0 , 0 + i ) , new Point ( bgMat . cols ( ) , - bgMat . cols ( ) + i ) , new Scalar ( 0 ) , 1 ) ;
48
+ }
49
+ grayDstMat = grayDstMat ?? new Mat ( src . height ( ) , src . width ( ) , CvType . CV_8UC1 ) ;
50
+
51
+ grayPixels = grayPixels ?? new byte [ grayMat . cols ( ) * grayMat . rows ( ) * grayMat . channels ( ) ] ;
52
+ maskPixels = maskPixels ?? new byte [ maskMat . cols ( ) * maskMat . rows ( ) * maskMat . channels ( ) ] ;
53
+
54
+
55
+ Imgproc . cvtColor ( src , grayMat , Imgproc . COLOR_RGBA2GRAY ) ;
56
+ bgMat . copyTo ( grayDstMat ) ;
57
+ Imgproc . GaussianBlur ( grayMat , lineMat , new Size ( 3 , 3 ) , 0 ) ;
58
+ grayMat . get ( 0 , 0 , grayPixels ) ;
59
+
60
+ for ( int i = 0 ; i < grayPixels . Length ; i ++ ) {
61
+ maskPixels [ i ] = 0 ;
62
+ if ( grayPixels [ i ] < 70 ) {
63
+ grayPixels [ i ] = 0 ;
64
+ maskPixels [ i ] = 1 ;
65
+ } else if ( 70 <= grayPixels [ i ] && grayPixels [ i ] < 120 ) {
66
+ grayPixels [ i ] = 100 ;
67
+ } else {
68
+ grayPixels [ i ] = 255 ;
69
+ maskPixels [ i ] = 1 ;
70
+ }
71
+ }
72
+
73
+ grayMat . put ( 0 , 0 , grayPixels ) ;
74
+ maskMat . put ( 0 , 0 , maskPixels ) ;
75
+ grayMat . copyTo ( grayDstMat , maskMat ) ;
76
+
77
+ Imgproc . Canny ( lineMat , lineMat , 20 , 120 ) ;
78
+ lineMat . copyTo ( maskMat ) ;
79
+ Core . bitwise_not ( lineMat , lineMat ) ;
80
+ lineMat . copyTo ( grayDstMat , maskMat ) ;
81
+
82
+ Imgproc . cvtColor ( grayDstMat , dst , Imgproc . COLOR_GRAY2RGBA ) ;
83
+ }
84
+
85
+ public void Dispose ( )
86
+ {
87
+ foreach ( var mat in new [ ] { grayMat , lineMat , maskMat , bgMat , grayDstMat } )
88
+ if ( mat != null )
89
+ maskMat . Dispose ( ) ;
90
+ grayDstMat =
91
+ bgMat =
92
+ maskMat =
93
+ lineMat =
94
+ grayMat = null ;
95
+ grayPixels =
96
+ maskPixels = null ;
97
+ }
98
+ }
94
99
}
0 commit comments