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