Skip to content

Commit 01069a0

Browse files
author
satoo air
committed
Initial Commit.
0 parents  commit 01069a0

File tree

5 files changed

+577
-0
lines changed

5 files changed

+577
-0
lines changed

ColorFrameSample.cs

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using Windows.Kinect;
4+
5+
using OpenCVForUnity;
6+
7+
public class ColorFrameSample : MonoBehaviour
8+
{
9+
10+
KinectSensor sensor;
11+
ColorFrameReader reader;
12+
Texture2D texture;
13+
byte[] data;
14+
15+
Mat rgbaMat;
16+
17+
public enum modeType
18+
{
19+
original,
20+
sepia,
21+
pixelize,
22+
comic
23+
}
24+
public modeType mode;
25+
26+
27+
//sepia
28+
Mat sepiaKernel;
29+
30+
//pixelize
31+
Size pixelizeSize0;
32+
Mat pixelizeIntermediateMat;
33+
34+
//comic
35+
Mat comicGrayMat;
36+
Mat comicLineMat;
37+
Mat comicMaskMat;
38+
Mat comicBgMat;
39+
Mat comicDstMat;
40+
byte[] comicGrayPixels;
41+
byte[] comicMaskPixels;
42+
43+
44+
45+
void Start ()
46+
{
47+
sensor = KinectSensor.GetDefault ();
48+
49+
if (sensor != null) {
50+
reader = sensor.ColorFrameSource.OpenReader ();
51+
52+
FrameDescription frameDesc = sensor.ColorFrameSource.CreateFrameDescription (ColorImageFormat.Rgba);
53+
54+
55+
texture = new Texture2D (frameDesc.Width, frameDesc.Height, TextureFormat.RGBA32, false);
56+
data = new byte[frameDesc.BytesPerPixel * frameDesc.LengthInPixels];
57+
58+
if (!sensor.IsOpen) {
59+
sensor.Open ();
60+
}
61+
62+
63+
rgbaMat = new Mat (texture.height, texture.width, CvType.CV_8UC4);
64+
65+
Debug.Log ("rgbaMat " + rgbaMat.ToString ());
66+
67+
gameObject.transform.localScale = new Vector3 (texture.width, texture.height, 1);
68+
69+
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
70+
71+
Camera.main.orthographicSize = texture.height / 2;
72+
73+
74+
// sepia
75+
sepiaKernel = new Mat (4, 4, CvType.CV_32F);
76+
sepiaKernel.put (0, 0, /* R */0.189f, 0.769f, 0.393f, 0f);
77+
sepiaKernel.put (1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
78+
sepiaKernel.put (2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
79+
sepiaKernel.put (3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
80+
81+
82+
// pixelize
83+
pixelizeIntermediateMat = new Mat ();
84+
pixelizeSize0 = new Size ();
85+
86+
87+
//comic
88+
comicGrayMat = new Mat (texture.height, texture.width, CvType.CV_8UC1);
89+
comicLineMat = new Mat (texture.height, texture.width, CvType.CV_8UC1);
90+
comicMaskMat = new Mat (texture.height, texture.width, CvType.CV_8UC1);
91+
92+
//create a striped background.
93+
comicBgMat = new Mat (texture.height, texture.width, CvType.CV_8UC1, new Scalar (255));
94+
for (int i = 0; i < comicBgMat.rows ()*2.5f; i=i+4) {
95+
Core.line (comicBgMat, new Point (0, 0 + i), new Point (comicBgMat.cols (), -comicBgMat.cols () + i), new Scalar (0), 1);
96+
}
97+
98+
comicDstMat = new Mat (texture.height, texture.width, CvType.CV_8UC1);
99+
100+
comicGrayPixels = new byte[comicGrayMat.cols () * comicGrayMat.rows () * comicGrayMat.channels ()];
101+
comicMaskPixels = new byte[comicMaskMat.cols () * comicMaskMat.rows () * comicMaskMat.channels ()];
102+
} else {
103+
UnityEngine.Debug.LogError ("No ready Kinect found!");
104+
}
105+
106+
107+
}
108+
109+
void Update ()
110+
{
111+
if (reader != null) {
112+
ColorFrame frame = reader.AcquireLatestFrame ();
113+
114+
if (frame != null) {
115+
frame.CopyConvertedFrameDataToArray (data, ColorImageFormat.Rgba);
116+
117+
frame.Dispose ();
118+
frame = null;
119+
120+
}
121+
}else{
122+
return;
123+
}
124+
125+
Utils.copyToMat (data, rgbaMat);
126+
127+
128+
if (mode == modeType.original) {
129+
130+
Core.putText (rgbaMat, "ORIGINAL MODE " + texture.width + "x" + texture.height, new Point (5, texture.height - 5), Core.FONT_HERSHEY_PLAIN, 4.0, new Scalar (255, 0, 0, 255), 3);
131+
132+
} else if (mode == modeType.sepia) {
133+
134+
Core.transform (rgbaMat, rgbaMat, sepiaKernel);
135+
136+
Core.putText (rgbaMat, "SEPIA MODE " + texture.width + "x" + texture.height, new Point (5, texture.height - 5), Core.FONT_HERSHEY_PLAIN, 4.0, new Scalar (255, 0, 0, 255), 3);
137+
138+
} else if (mode == modeType.pixelize) {
139+
Imgproc.resize (rgbaMat, pixelizeIntermediateMat, pixelizeSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
140+
Imgproc.resize (pixelizeIntermediateMat, rgbaMat, rgbaMat.size (), 0.0, 0.0, Imgproc.INTER_NEAREST);
141+
142+
Core.putText (rgbaMat, "PIXELIZE MODE" + texture.width + "x" + texture.height, new Point (5, texture.height - 5), Core.FONT_HERSHEY_PLAIN, 4.0, new Scalar (255, 0, 0, 255), 3);
143+
144+
} else if (mode == modeType.comic) {
145+
Imgproc.cvtColor (rgbaMat, comicGrayMat, Imgproc.COLOR_RGBA2GRAY);
146+
147+
comicBgMat.copyTo (comicDstMat);
148+
149+
Imgproc.GaussianBlur (comicGrayMat, comicLineMat, new Size (3, 3), 0);
150+
151+
152+
Utils.copyFromMat (comicGrayMat, comicGrayPixels);
153+
154+
for (int i = 0; i < comicGrayPixels.Length; i++) {
155+
156+
comicMaskPixels [i] = 0;
157+
158+
if (comicGrayPixels [i] < 70) {
159+
comicGrayPixels [i] = 0;
160+
161+
comicMaskPixels [i] = 1;
162+
} else if (70 <= comicGrayPixels [i] && comicGrayPixels [i] < 120) {
163+
comicGrayPixels [i] = 100;
164+
165+
166+
} else {
167+
comicGrayPixels [i] = 255;
168+
169+
comicMaskPixels [i] = 1;
170+
}
171+
}
172+
173+
174+
Utils.copyToMat (comicGrayPixels, comicGrayMat);
175+
176+
Utils.copyToMat (comicMaskPixels, comicMaskMat);
177+
178+
comicGrayMat.copyTo (comicDstMat, comicMaskMat);
179+
180+
181+
Imgproc.Canny (comicLineMat, comicLineMat, 20, 120);
182+
183+
comicLineMat.copyTo (comicMaskMat);
184+
185+
Core.bitwise_not (comicLineMat, comicLineMat);
186+
187+
comicLineMat.copyTo (comicDstMat, comicMaskMat);
188+
189+
190+
Imgproc.cvtColor (comicDstMat, rgbaMat, Imgproc.COLOR_GRAY2RGBA);
191+
192+
Core.putText (rgbaMat, "COMIC MODE " + texture.width + "x" + texture.height, new Point (5, texture.height - 5), Core.FONT_HERSHEY_PLAIN, 4.0, new Scalar (255, 0, 0, 255), 3);
193+
194+
}
195+
196+
Utils.matToTexture (rgbaMat, texture);
197+
198+
}
199+
200+
void OnApplicationQuit ()
201+
{
202+
if (reader != null) {
203+
reader.Dispose ();
204+
reader = null;
205+
}
206+
207+
if (sensor != null) {
208+
if (sensor.IsOpen) {
209+
sensor.Close ();
210+
}
211+
212+
sensor = null;
213+
}
214+
}
215+
216+
void OnGUI ()
217+
{
218+
float screenScale = Screen.width / 480.0f;
219+
Matrix4x4 scaledMatrix = Matrix4x4.Scale (new Vector3 (screenScale, screenScale, screenScale));
220+
GUI.matrix = scaledMatrix;
221+
222+
223+
GUILayout.BeginVertical ();
224+
225+
if (GUILayout.Button ("original")) {
226+
mode = modeType.original;
227+
}
228+
229+
if (GUILayout.Button ("sepia")) {
230+
mode = modeType.sepia;
231+
}
232+
233+
if (GUILayout.Button ("pixelize")) {
234+
mode = modeType.pixelize;
235+
}
236+
237+
if (GUILayout.Button ("comic")) {
238+
mode = modeType.comic;
239+
}
240+
241+
242+
GUILayout.EndVertical ();
243+
}
244+
}

KinectOpenCVForUnitySample.unity

13.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)