forked from EnoxSoftware/OpenCVForUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchTemplateSample.cs
More file actions
77 lines (55 loc) · 2.18 KB
/
Copy pathMatchTemplateSample.cs
File metadata and controls
77 lines (55 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using UnityEngine;
using System.Collections;
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
using UnityEngine.SceneManagement;
#endif
using OpenCVForUnity;
namespace OpenCVForUnitySample
{
/// <summary>
/// Match template sample.
/// </summary>
public class MatchTemplateSample : MonoBehaviour
{
// Use this for initialization
void Start ()
{
Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
Texture2D tempTexture = Resources.Load ("template") as Texture2D;
Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
Mat tempMat = new Mat (tempTexture.height, tempTexture.width, CvType.CV_8UC4);
Utils.texture2DToMat (imgTexture, imgMat);
Utils.texture2DToMat (tempTexture, tempMat);
//Create the result mat
int result_cols = imgMat.cols () - tempMat.cols () + 1;
int result_rows = imgMat.rows () - tempMat.rows () + 1;
Mat result = new Mat (result_rows, result_cols, CvType.CV_32FC1);
int match_method = Imgproc.TM_CCOEFF_NORMED;
Imgproc.matchTemplate (imgMat, tempMat, result, match_method);
Imgproc.threshold (result, result, 0.8, 1.0, Imgproc.THRESH_TOZERO);//threshold = 0.8
for (int i=0; i<result.rows(); i++) {
for (int j=0; j<result.cols(); j++) {
if (result.get (i, j) [0] > 0) {
Imgproc.rectangle (imgMat, new Point (j, i), new Point (j + tempMat.cols (), i + tempMat.rows ()), new Scalar (255, 0, 0, 255), 2);
Debug.Log ("value" + result.get (i, j) [0]);
}
}
}
Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
Utils.matToTexture2D (imgMat, texture);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}
// Update is called once per frame
void Update ()
{
}
public void OnBackButton ()
{
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
SceneManager.LoadScene ("OpenCVForUnitySample");
#else
Application.LoadLevel ("OpenCVForUnitySample");
#endif
}
}
}