|
| 1 | +using OpenCVForUnity.CoreModule; |
| 2 | +using OpenCVForUnity.ImgprocModule; |
| 3 | +using OpenCVForUnity.UtilsModule; |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace VideoPlayerWithOpenCVForUnityExample |
| 7 | +{ |
| 8 | + |
| 9 | + public class ComicFilter |
| 10 | + { |
| 11 | + |
| 12 | + Mat grayMat; |
| 13 | + Mat maskMat; |
| 14 | + Mat screentoneMat; |
| 15 | + Mat grayDstMat; |
| 16 | + |
| 17 | + Mat grayLUT; |
| 18 | + Mat contrastAdjustmentsLUT; |
| 19 | + Mat kernel_dilate; |
| 20 | + Mat kernel_erode; |
| 21 | + Size blurSize; |
| 22 | + int blackThresh; |
| 23 | + bool drawMainLine; |
| 24 | + bool useNoiseFilter; |
| 25 | + |
| 26 | + |
| 27 | + public ComicFilter(int blackThresh = 60, int grayThresh = 120, int thickness = 5, bool useNoiseFilter = true) |
| 28 | + { |
| 29 | + this.blackThresh = blackThresh; |
| 30 | + this.drawMainLine = (thickness != 0); |
| 31 | + this.useNoiseFilter = useNoiseFilter; |
| 32 | + |
| 33 | + grayLUT = new Mat(1, 256, CvType.CV_8UC1); |
| 34 | + byte[] lutArray = new byte[256]; |
| 35 | + for (int i = 0; i < lutArray.Length; i++) |
| 36 | + { |
| 37 | + if (blackThresh <= i && i < grayThresh) |
| 38 | + lutArray[i] = 255; |
| 39 | + } |
| 40 | + MatUtils.copyToMat(lutArray, grayLUT); |
| 41 | + |
| 42 | + if (drawMainLine) |
| 43 | + { |
| 44 | + kernel_dilate = new Mat(thickness, thickness, CvType.CV_8UC1, new Scalar(1)); |
| 45 | + |
| 46 | + int erode = (thickness >= 5) ? 2 : 1; |
| 47 | + kernel_erode = new Mat(erode, erode, CvType.CV_8UC1, new Scalar(1)); |
| 48 | + |
| 49 | + int blur = (thickness >= 4) ? thickness - 1 : 3; |
| 50 | + blurSize = new Size(blur, blur); |
| 51 | + |
| 52 | + contrastAdjustmentsLUT = new Mat(1, 256, CvType.CV_8UC1); |
| 53 | + byte[] contrastAdjustmentsLUTArray = new byte[256]; |
| 54 | + for (int i = 0; i < contrastAdjustmentsLUTArray.Length; i++) |
| 55 | + { |
| 56 | + int a = (int)(i * 1.5f); |
| 57 | + contrastAdjustmentsLUTArray[i] = (a > byte.MaxValue) ? (byte)255 : (byte)a; |
| 58 | + |
| 59 | + } |
| 60 | + MatUtils.copyToMat(contrastAdjustmentsLUTArray, contrastAdjustmentsLUT); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void Process(Mat src, Mat dst, bool isBGR = false) |
| 65 | + { |
| 66 | + if (src == null) |
| 67 | + throw new ArgumentNullException("src == null"); |
| 68 | + if (dst == null) |
| 69 | + throw new ArgumentNullException("dst == null"); |
| 70 | + |
| 71 | + if (grayMat != null && (grayMat.width() != src.width() || grayMat.height() != src.height())) |
| 72 | + { |
| 73 | + grayMat.Dispose(); |
| 74 | + grayMat = null; |
| 75 | + maskMat.Dispose(); |
| 76 | + maskMat = null; |
| 77 | + screentoneMat.Dispose(); |
| 78 | + screentoneMat = null; |
| 79 | + grayDstMat.Dispose(); |
| 80 | + grayDstMat = null; |
| 81 | + } |
| 82 | + grayMat = grayMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1); |
| 83 | + maskMat = maskMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1); |
| 84 | + grayDstMat = grayDstMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1); |
| 85 | + |
| 86 | + if (screentoneMat == null) |
| 87 | + { |
| 88 | + // create a striped screentone. |
| 89 | + screentoneMat = new Mat(src.height(), src.width(), CvType.CV_8UC1, new Scalar(255)); |
| 90 | + for (int i = 0; i < screentoneMat.rows() * 2.5f; i = i + 4) |
| 91 | + { |
| 92 | + Imgproc.line(screentoneMat, new Point(0, 0 + i), new Point(screentoneMat.cols(), -screentoneMat.cols() + i), new Scalar(0), 1); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (src.type() == CvType.CV_8UC1) |
| 97 | + { |
| 98 | + src.copyTo(grayMat); |
| 99 | + } |
| 100 | + else if (dst.type() == CvType.CV_8UC3) |
| 101 | + { |
| 102 | + Imgproc.cvtColor(src, grayMat, (isBGR) ? Imgproc.COLOR_BGR2GRAY : Imgproc.COLOR_RGB2GRAY); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + Imgproc.cvtColor(src, grayMat, (isBGR) ? Imgproc.COLOR_BGRA2GRAY : Imgproc.COLOR_RGBA2GRAY); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + // binarize. |
| 111 | + Imgproc.threshold(grayMat, grayDstMat, blackThresh, 255.0, Imgproc.THRESH_BINARY); |
| 112 | + |
| 113 | + // draw striped screentone. |
| 114 | + Core.LUT(grayMat, grayLUT, maskMat); |
| 115 | + screentoneMat.copyTo(grayDstMat, maskMat); |
| 116 | + |
| 117 | + // draw main line. |
| 118 | + if (drawMainLine) |
| 119 | + { |
| 120 | + Core.LUT(grayMat, contrastAdjustmentsLUT, maskMat); // = grayMat.convertTo(maskMat, -1, 1.5, 0); |
| 121 | + |
| 122 | + if (useNoiseFilter) |
| 123 | + { |
| 124 | + Imgproc.blur(maskMat, grayMat, blurSize); |
| 125 | + Imgproc.dilate(grayMat, maskMat, kernel_dilate); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + Imgproc.dilate(maskMat, grayMat, kernel_dilate); |
| 130 | + } |
| 131 | + Core.absdiff(grayMat, maskMat, grayMat); |
| 132 | + Imgproc.threshold(grayMat, maskMat, 25, 255.0, Imgproc.THRESH_BINARY); |
| 133 | + if (useNoiseFilter) |
| 134 | + { |
| 135 | + Imgproc.erode(maskMat, grayMat, kernel_erode); |
| 136 | + Core.bitwise_not(grayMat, maskMat); |
| 137 | + maskMat.copyTo(grayDstMat, grayMat); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + Core.bitwise_not(maskMat, grayMat); |
| 142 | + grayMat.copyTo(grayDstMat, maskMat); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + if (dst.type() == CvType.CV_8UC1) |
| 148 | + { |
| 149 | + grayDstMat.copyTo(dst); |
| 150 | + } |
| 151 | + else if (dst.type() == CvType.CV_8UC3) |
| 152 | + { |
| 153 | + Imgproc.cvtColor(grayDstMat, dst, (isBGR) ? Imgproc.COLOR_GRAY2BGR : Imgproc.COLOR_GRAY2RGB); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + Imgproc.cvtColor(grayDstMat, dst, (isBGR) ? Imgproc.COLOR_GRAY2BGRA : Imgproc.COLOR_GRAY2RGBA); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public void Dispose() |
| 162 | + { |
| 163 | + foreach (var mat in new[] { grayMat, maskMat, screentoneMat, grayDstMat, grayLUT, kernel_dilate, kernel_erode, contrastAdjustmentsLUT }) |
| 164 | + if (mat != null) mat.Dispose(); |
| 165 | + |
| 166 | + grayDstMat = |
| 167 | + screentoneMat = |
| 168 | + maskMat = |
| 169 | + grayMat = |
| 170 | + grayLUT = |
| 171 | + kernel_dilate = |
| 172 | + kernel_erode = |
| 173 | + contrastAdjustmentsLUT = null; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments