Skip to content

Commit a4fd21c

Browse files
committed
v1.0.4
Updated OpenCVForUnity version to 2.3.3.
1 parent 1431256 commit a4fd21c

17 files changed

+177
-1058
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,99 @@
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+
}
9499
}

Assets/NatCamWithOpenCVForUnityExample/IntegrationWithNatShareExample/IntegrationWithNatShareExample.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7-
using OpenCVForUnity;
87
using NatCamU.Core;
98
using NatShareU;
9+
using OpenCVForUnity.CoreModule;
10+
using OpenCVForUnity.ImgprocModule;
11+
using OpenCVForUnity.UnityUtils;
1012

1113
namespace NatCamWithOpenCVForUnityExample
1214
{
@@ -97,8 +99,8 @@ protected override void UpdateTexture ()
9799
if (applyComicFilterToggle.isOn)
98100
comicFilter.Process (frameMatrix, frameMatrix);
99101

100-
Imgproc.putText (frameMatrix, "[NatCam With OpenCVForUnity Example]", new Point (5, frameMatrix.rows () - 50), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
101-
Imgproc.putText (frameMatrix, "- Integration With NatShare Example", new Point (5, frameMatrix.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
102+
Imgproc.putText (frameMatrix, "[NatCam With OpenCVForUnity Example]", new Point (5, frameMatrix.rows () - 50), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
103+
Imgproc.putText (frameMatrix, "- Integration With NatShare Example", new Point (5, frameMatrix.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
102104
// Convert to Texture2D
103105
Utils.fastMatToTexture2D (frameMatrix, texture, true, 0, false);
104106
}

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewToMatExample/NatCamPreviewToMatExample.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using UnityEngine;
2-
using NatCamU.Core;
3-
using System.Collections.Generic;
4-
using System;
52
using UnityEngine.UI;
6-
using System.Collections;
7-
using OpenCVForUnity;
8-
9-
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
103
using UnityEngine.SceneManagement;
11-
#endif
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using NatCamU.Core;
8+
using OpenCVForUnity.CoreModule;
9+
using OpenCVForUnity.UnityUtils;
1210

1311
namespace NatCamWithOpenCVForUnityExample
1412
{

Assets/NatCamWithOpenCVForUnityExample/NatCamPreviewToMatHelperExample/NatCamPreviewToMatHelperExample.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
using UnityEngine;
44
using UnityEngine.UI;
55
using UnityEngine.SceneManagement;
6-
using OpenCVForUnity;
6+
using OpenCVForUnity.CoreModule;
7+
using OpenCVForUnity.ImgprocModule;
8+
using OpenCVForUnity.UnityUtils;
9+
using OpenCVForUnity.UnityUtils.Helper;
710

811
namespace NatCamWithOpenCVForUnityExample
912
{
@@ -154,7 +157,7 @@ void Update ()
154157

155158
Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
156159

157-
//Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
160+
//Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
158161

159162
// Restore the coordinate system of the image by OpenCV's Flip function.
160163
Utils.fastMatToTexture2D (rgbaMat, texture);

Assets/NatCamWithOpenCVForUnityExample/NatCamWithOpenCVForUnityExample.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22
using UnityEngine.SceneManagement;
3-
using System.Collections;
43
using UnityEngine.UI;
4+
using System.Collections;
55
using System;
66

77
namespace NatCamWithOpenCVForUnityExample
@@ -103,7 +103,7 @@ void Awake ()
103103
void Start ()
104104
{
105105
exampleTitle.text = "NatCamWithOpenCVForUnity Example " + Application.version;
106-
versionInfo.text = OpenCVForUnity.Core.NATIVE_LIBRARY_NAME + " " + OpenCVForUnity.Utils.getVersion () + " (" + OpenCVForUnity.Core.VERSION + ")";
106+
versionInfo.text = OpenCVForUnity.CoreModule.Core.NATIVE_LIBRARY_NAME + " " + OpenCVForUnity.UnityUtils.Utils.getVersion () + " (" + OpenCVForUnity.CoreModule.Core.VERSION + ")";
107107
versionInfo.text += " / UnityEditor " + Application.unityVersion;
108108
versionInfo.text += " / ";
109109
#if UNITY_EDITOR

Assets/NatCamWithOpenCVForUnityExample/Scripts/ExampleBase.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using UnityEngine;
22
using UnityEngine.SceneManagement;
33
using UnityEngine.UI;
4-
using OpenCVForUnity;
4+
using OpenCVForUnity.CoreModule;
5+
using OpenCVForUnity.ImgprocModule;
56

67
namespace NatCamWithOpenCVForUnityExample
78
{

Assets/NatCamWithOpenCVForUnityExample/Scripts/ICameraSource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using UnityEngine;
22
using System;
3-
using OpenCVForUnity;
3+
using OpenCVForUnity.CoreModule;
44

55
namespace NatCamWithOpenCVForUnityExample
66
{

Assets/NatCamWithOpenCVForUnityExample/Scripts/NatCamSource.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using UnityEngine;
22
using System;
3-
using OpenCVForUnity;
4-
using NatCamU.Core;
53
using System.Runtime.InteropServices;
4+
using NatCamU.Core;
5+
using OpenCVForUnity.CoreModule;
6+
using OpenCVForUnity.UnityUtils;
67

78
namespace NatCamWithOpenCVForUnityExample
89
{

Assets/NatCamWithOpenCVForUnityExample/Scripts/Utils/NatCamPreviewToMatHelper.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
using System.Collections;
33
using UnityEngine;
44
using UnityEngine.Serialization;
5-
using OpenCVForUnity;
65
using NatCamU.Core;
6+
using OpenCVForUnity.UnityUtils.Helper;
7+
using OpenCVForUnity.CoreModule;
8+
using OpenCVForUnity.UnityUtils;
79

810
namespace NatCamWithOpenCVForUnityExample
911
{
1012
/// <summary>
1113
/// NatCamPreview to mat helper.
12-
/// v 1.0.4
14+
/// v 1.0.5
1315
/// Depends on NatCam version 2.1f3 or later.
16+
/// Depends on OpenCVForUnity version 2.3.3 or later.
1417
/// </summary>
1518
public class NatCamPreviewToMatHelper : WebCamTextureToMatHelper
1619
{

0 commit comments

Comments
 (0)