Skip to content

Commit aa3f860

Browse files
committed
v2.3.0
Updated NatCam version to 2.3.0.
1 parent d12d265 commit aa3f860

35 files changed

+1870
-1379
lines changed

Assets/NatCamWithOpenCVForUnityExample/IntegrationWithNatShareExample/ComicFilter.cs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using OpenCVForUnity.CoreModule;
1+
using OpenCVForUnity.CoreModule;
32
using OpenCVForUnity.ImgprocModule;
3+
using System;
44

55
namespace NatCamWithOpenCVForUnityExample
66
{
@@ -16,77 +16,85 @@ public class ComicFilter
1616
byte[] grayPixels;
1717
byte[] maskPixels;
1818

19-
public void Process (Mat src, Mat dst)
19+
public void Process(Mat src, Mat dst)
2020
{
2121
if (src == null)
22-
throw new ArgumentNullException ("src == null");
22+
throw new ArgumentNullException("src == null");
2323
if (dst == null)
24-
throw new ArgumentNullException ("dst == null");
24+
throw new ArgumentNullException("dst == null");
2525

26-
if (grayMat != null && (grayMat.width () != src.width () || grayMat.height () != src.height ())) {
27-
grayMat.Dispose ();
26+
if (grayMat != null && (grayMat.width() != src.width() || grayMat.height() != src.height()))
27+
{
28+
grayMat.Dispose();
2829
grayMat = null;
29-
lineMat.Dispose ();
30+
lineMat.Dispose();
3031
lineMat = null;
31-
maskMat.Dispose ();
32+
maskMat.Dispose();
3233
maskMat = null;
33-
bgMat.Dispose ();
34+
bgMat.Dispose();
3435
bgMat = null;
35-
grayDstMat.Dispose ();
36+
grayDstMat.Dispose();
3637
grayDstMat = null;
3738

3839
grayPixels = null;
3940
maskPixels = null;
4041
}
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);
42+
grayMat = grayMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1);
43+
lineMat = lineMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1);
44+
maskMat = maskMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1);
4445
//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);
46+
bgMat = new Mat(src.height(), src.width(), CvType.CV_8UC1, new Scalar(255));
47+
for (int i = 0; i < bgMat.rows() * 2.5f; i = i + 4)
48+
{
49+
Imgproc.line(bgMat, new Point(0, 0 + i), new Point(bgMat.cols(), -bgMat.cols() + i), new Scalar(0), 1);
4850
}
49-
grayDstMat = grayDstMat ?? new Mat (src.height (), src.width (), CvType.CV_8UC1);
51+
grayDstMat = grayDstMat ?? new Mat(src.height(), src.width(), CvType.CV_8UC1);
5052

51-
grayPixels = grayPixels ?? new byte[grayMat.cols () * grayMat.rows () * grayMat.channels ()];
52-
maskPixels = maskPixels ?? new byte[maskMat.cols () * maskMat.rows () * maskMat.channels ()];
53+
grayPixels = grayPixels ?? new byte[grayMat.cols() * grayMat.rows() * grayMat.channels()];
54+
maskPixels = maskPixels ?? new byte[maskMat.cols() * maskMat.rows() * maskMat.channels()];
5355

5456

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);
57+
Imgproc.cvtColor(src, grayMat, Imgproc.COLOR_RGBA2GRAY);
58+
bgMat.copyTo(grayDstMat);
59+
Imgproc.GaussianBlur(grayMat, lineMat, new Size(3, 3), 0);
60+
grayMat.get(0, 0, grayPixels);
5961

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;
62+
for (int i = 0; i < grayPixels.Length; i++)
63+
{
64+
maskPixels[i] = 0;
65+
if (grayPixels[i] < 70)
66+
{
67+
grayPixels[i] = 0;
68+
maskPixels[i] = 1;
69+
}
70+
else if (70 <= grayPixels[i] && grayPixels[i] < 120)
71+
{
72+
grayPixels[i] = 100;
73+
}
74+
else
75+
{
76+
grayPixels[i] = 255;
77+
maskPixels[i] = 1;
7078
}
7179
}
7280

73-
grayMat.put (0, 0, grayPixels);
74-
maskMat.put (0, 0, maskPixels);
75-
grayMat.copyTo (grayDstMat, maskMat);
81+
grayMat.put(0, 0, grayPixels);
82+
maskMat.put(0, 0, maskPixels);
83+
grayMat.copyTo(grayDstMat, maskMat);
7684

77-
Imgproc.Canny (lineMat, lineMat, 20, 120);
78-
lineMat.copyTo (maskMat);
79-
Core.bitwise_not (lineMat, lineMat);
80-
lineMat.copyTo (grayDstMat, maskMat);
85+
Imgproc.Canny(lineMat, lineMat, 20, 120);
86+
lineMat.copyTo(maskMat);
87+
Core.bitwise_not(lineMat, lineMat);
88+
lineMat.copyTo(grayDstMat, maskMat);
8189

82-
Imgproc.cvtColor (grayDstMat, dst, Imgproc.COLOR_GRAY2RGBA);
90+
Imgproc.cvtColor(grayDstMat, dst, Imgproc.COLOR_GRAY2RGBA);
8391
}
8492

85-
public void Dispose ()
93+
public void Dispose()
8694
{
87-
foreach (var mat in new [] { grayMat, lineMat, maskMat, bgMat, grayDstMat })
95+
foreach (var mat in new[] { grayMat, lineMat, maskMat, bgMat, grayDstMat })
8896
if (mat != null)
89-
maskMat.Dispose ();
97+
maskMat.Dispose();
9098
grayDstMat =
9199
bgMat =
92100
maskMat =
Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using UnityEngine;
2-
using UnityEngine.SceneManagement;
3-
using UnityEngine.UI;
4-
using System;
5-
using System.Collections;
6-
using System.Collections.Generic;
7-
using NatCam;
8-
using NatShareU;
1+
using NatShareU;
92
using OpenCVForUnity.CoreModule;
103
using OpenCVForUnity.ImgprocModule;
114
using OpenCVForUnity.UnityUtils;
5+
using UnityEngine;
6+
using UnityEngine.UI;
127

138
namespace NatCamWithOpenCVForUnityExample
149
{
@@ -27,40 +22,55 @@ public class IntegrationWithNatShareExample : ExampleBase<NatCamSource>
2722

2823
FpsMonitor fpsMonitor;
2924

30-
protected override void Start ()
25+
string exampleTitle = "";
26+
string exampleSceneTitle = "";
27+
string settingInfo1 = "";
28+
Scalar textColor = new Scalar(255, 255, 255, 255);
29+
Point textPos = new Point();
30+
31+
protected override void Start()
3132
{
3233
// Load global camera benchmark settings.
3334
int width, height, framerate;
34-
NatCamWithOpenCVForUnityExample.CameraConfiguration (out width, out height, out framerate);
35-
NatCamWithOpenCVForUnityExample.ExampleSceneConfiguration (out performImageProcessingEachTime);
35+
NatCamWithOpenCVForUnityExample.CameraConfiguration(out width, out height, out framerate);
36+
NatCamWithOpenCVForUnityExample.ExampleSceneConfiguration(out performImageProcessingEachTime);
3637
// Create camera source
37-
cameraSource = new NatCamSource (width, height, framerate, useFrontCamera);
38-
cameraSource.StartPreview (OnStart, OnFrame);
38+
cameraSource = new NatCamSource(width, height, framerate, useFrontCamera);
39+
if (!cameraSource.activeCamera)
40+
cameraSource = new NatCamSource(width, height, framerate, !useFrontCamera);
41+
cameraSource.StartPreview(OnStart, OnFrame);
3942
// Create comic filter
40-
comicFilter = new ComicFilter ();
41-
42-
fpsMonitor = GetComponent<FpsMonitor> ();
43-
if (fpsMonitor != null) {
44-
fpsMonitor.Add ("Name", "IntegrationWithNatShareExample");
45-
fpsMonitor.Add ("performImageProcessingEveryTime", performImageProcessingEachTime.ToString ());
46-
fpsMonitor.Add ("onFrameFPS", onFrameFPS.ToString ("F1"));
47-
fpsMonitor.Add ("drawFPS", drawFPS.ToString ("F1"));
48-
fpsMonitor.Add ("width", "");
49-
fpsMonitor.Add ("height", "");
50-
fpsMonitor.Add ("orientation", "");
43+
comicFilter = new ComicFilter();
44+
45+
exampleTitle = "[NatCamWithOpenCVForUnity Example] (" + NatCamWithOpenCVForUnityExample.GetNatCamVersion() + ")";
46+
exampleSceneTitle = "- Integration With NatShare Example";
47+
48+
fpsMonitor = GetComponent<FpsMonitor>();
49+
if (fpsMonitor != null)
50+
{
51+
fpsMonitor.Add("Name", "IntegrationWithNatShareExample");
52+
fpsMonitor.Add("performImageProcessingEveryTime", performImageProcessingEachTime.ToString());
53+
fpsMonitor.Add("onFrameFPS", onFrameFPS.ToString("F1"));
54+
fpsMonitor.Add("drawFPS", drawFPS.ToString("F1"));
55+
fpsMonitor.Add("width", "");
56+
fpsMonitor.Add("height", "");
57+
fpsMonitor.Add("isFrontFacing", "");
58+
fpsMonitor.Add("orientation", "");
5159
}
5260
}
5361

54-
protected override void OnStart ()
62+
protected override void OnStart()
5563
{
64+
settingInfo1 = "- resolution: " + cameraSource.width + "x" + cameraSource.height;
65+
5666
// Create matrix
5767
if (frameMatrix != null)
58-
frameMatrix.Dispose ();
59-
frameMatrix = new Mat (cameraSource.height, cameraSource.width, CvType.CV_8UC4);
68+
frameMatrix.Dispose();
69+
frameMatrix = new Mat(cameraSource.height, cameraSource.width, CvType.CV_8UC4);
6070
// Create texture
6171
if (texture != null)
62-
Texture2D.Destroy (texture);
63-
texture = new Texture2D (
72+
Texture2D.Destroy(texture);
73+
texture = new Texture2D(
6474
cameraSource.width,
6575
cameraSource.height,
6676
TextureFormat.RGBA32,
@@ -70,67 +80,79 @@ protected override void OnStart ()
7080
// Display preview
7181
rawImage.texture = texture;
7282
aspectFitter.aspectRatio = cameraSource.width / (float)cameraSource.height;
73-
Debug.Log ("NatCam camera source started with resolution: " + cameraSource.width + "x" + cameraSource.height);
74-
75-
if (fpsMonitor != null) {
76-
fpsMonitor.Add ("width", cameraSource.width.ToString ());
77-
fpsMonitor.Add ("height", cameraSource.height.ToString ());
78-
fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
83+
Debug.Log("NatCam camera source started with resolution: " + cameraSource.width + "x" + cameraSource.height + " isFrontFacing: " + cameraSource.isFrontFacing);
84+
85+
if (fpsMonitor != null)
86+
{
87+
fpsMonitor.Add("width", cameraSource.width.ToString());
88+
fpsMonitor.Add("height", cameraSource.height.ToString());
89+
fpsMonitor.Add("isFrontFacing", cameraSource.isFrontFacing.ToString());
90+
fpsMonitor.Add("orientation", Screen.orientation.ToString());
7991
}
8092
}
8193

82-
protected override void Update ()
94+
protected override void Update()
8395
{
84-
base.Update ();
85-
86-
if (updateCount == 0) {
87-
if (fpsMonitor != null) {
88-
fpsMonitor.Add ("onFrameFPS", onFrameFPS.ToString ("F1"));
89-
fpsMonitor.Add ("drawFPS", drawFPS.ToString ("F1"));
90-
fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
96+
base.Update();
97+
98+
if (updateCount == 0)
99+
{
100+
if (fpsMonitor != null)
101+
{
102+
fpsMonitor.Add("onFrameFPS", onFrameFPS.ToString("F1"));
103+
fpsMonitor.Add("drawFPS", drawFPS.ToString("F1"));
104+
fpsMonitor.Add("orientation", Screen.orientation.ToString());
91105
}
92106
}
93107
}
94108

95-
protected override void UpdateTexture ()
109+
protected override void UpdateTexture()
96110
{
97-
cameraSource.CaptureFrame (frameMatrix);
111+
cameraSource.CaptureFrame(frameMatrix);
98112

99113
if (applyComicFilterToggle.isOn)
100-
comicFilter.Process (frameMatrix, frameMatrix);
114+
comicFilter.Process(frameMatrix, frameMatrix);
115+
116+
textPos.x = 5;
117+
textPos.y = frameMatrix.rows() - 50;
118+
Imgproc.putText(frameMatrix, exampleTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
119+
textPos.y = frameMatrix.rows() - 30;
120+
Imgproc.putText(frameMatrix, exampleSceneTitle, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
121+
textPos.y = frameMatrix.rows() - 10;
122+
Imgproc.putText(frameMatrix, settingInfo1, textPos, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, Imgproc.LINE_AA, false);
101123

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);
104124
// Convert to Texture2D
105-
Utils.fastMatToTexture2D (frameMatrix, texture, true, 0, false);
125+
Utils.fastMatToTexture2D(frameMatrix, texture, true, 0, false);
106126
}
107127

108-
protected override void OnDestroy ()
128+
protected override void OnDestroy()
109129
{
110-
if (cameraSource != null) {
111-
cameraSource.Dispose ();
130+
if (cameraSource != null)
131+
{
132+
cameraSource.Dispose();
112133
cameraSource = null;
113134
}
114135
if (frameMatrix != null)
115-
frameMatrix.Dispose ();
136+
frameMatrix.Dispose();
116137
frameMatrix = null;
117-
Texture2D.Destroy (texture);
138+
Texture2D.Destroy(texture);
118139
texture = null;
119-
comicFilter.Dispose ();
140+
comicFilter.Dispose();
120141
comicFilter = null;
121142
}
122143

123-
public void OnShareButtonClick ()
144+
public void OnShareButtonClick()
124145
{
125-
NatShare.Share (texture,
126-
() => {
127-
Debug.Log ("sharing is complete.");
146+
NatShare.Share(texture,
147+
() =>
148+
{
149+
Debug.Log("sharing is complete.");
128150
});
129151
}
130152

131-
public void OnSaveToCameraRollButtonClick ()
153+
public void OnSaveToCameraRollButtonClick()
132154
{
133-
NatShare.SaveToCameraRoll (texture, "NatCamWithOpenCVForUnityExample");
155+
NatShare.SaveToCameraRoll(texture, "NatCamWithOpenCVForUnityExample");
134156
}
135157
}
136158
}

0 commit comments

Comments
 (0)