I try to use opencv in unity3d. This page advices some steps to integration unity+opencv. To do first step, I create a static library project on visual studio 2013. I add opencv library to this vs project.
I have installed opencv2.4.10
, so I didn't install again opencv
.
My steps are below :
1. Download OpenCV 2.4.10
2. Create a Win32 console application - Select static library
3. Config opencv..
3.1. Menu Project ---- $ProjectName Properties
3.2. Choose Configuration Manager... and add x64 platform
3.3. At configuration field, choose All configuration
3.3.1. At Configuration Properties ---- C/C++ ---- Additional Include Directories, add opencv include folders
$opencv\build\include
$opencv\build\opencv
$opencv\build\opencv2 NOTE: $opencv is a folder that you have installed opencv
3.3.2. At Configuration Properties ---- Linker ---- Additional Library Directories, add opencv library folder
$opencv\build\x64\vc12\lib
3.4. At configuration field, choose Debug mode
< <3.4.1. At Configuration Properties ---- Linker ---- Additional dependencies ---- add dependence libraries
opencv_stitching248d.lib
opencv_contrib248d.lib
opencv_videostab248d.lib
opencv_superres248d.lib
opencv_nonfree248d.lib
opencv_gpu248d.lib
opencv_ocl248d.lib
opencv_legacy248d.lib
opencv_ts248d.lib
opencv_calib3d248d.lib
opencv_features2d248d.lib
opencv_objdetect248d.lib
opencv_highgui248d.lib
opencv_video248d.lib
opencv_photo248d.lib
opencv_imgproc248d.lib
opencv_flann248d.lib
opencv_ml248d.lib
opencv_core248d.lib
NOTE: These file above are located at .\opencv\build\x64\vc12\staticlib\
3.5. At configuration field, choose Release mode
3.5.1. At Configuration Properties ---- Linker ---- Additional dependencies ---- add dependence libraries
opencv_***.lib
NOTE: These file above are located at .\opencv\build\x64\vc12\staticlib\
3.6. Add $opencv\build\x64\v12\bin to System Environment Path
3.7. Restart Visual studio
3.8. Build project
Now I should do third step. But which files I should copy to inside the Asset > Plugins
folder? All project or a spesific file?
I put 2 files (that created using 64bit lib files) named Visual Studio 2013\Projects\OpenCVStaticDll\x64\Debug\OpenCVStatic
(object file library) and Visual Studio 2013\Projects\OpenCVStaticDll\OpenCVStaticDll\x64\Debug\OpenCVStatic
(precompiled header file) inside the Asset > Plugins folder on step 4
. I create them using 32bit same way, and put them under C:/Program Fİles(x86)/Unity/Editor
.
I put all opencv_**.lib(under x86/vc12/staticlib
) under C:/Program Fİles(x86)/Unity/Editor
.
Opencv-C++ Code :
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#pragma once
extern "C" {
int func();
}
extern "C" {
int func()
{
cv::Point p;
p.x = 1;
p.y = 0;
return p.y;
}
}
Unity C# code :
public class DefaultTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
[DllImport ("OpenCVStaticDll")]
private static extern int func();
void Update () {
if (Input.GetMouseButtonDown (0)) {
mTrackableBehaviour.gameObject.transform.position = new Vector3 (
mTrackableBehaviour.gameObject.transform.position.x + (int) (func ()*3),
mTrackableBehaviour.gameObject.transform.position.y,
mTrackableBehaviour.gameObject.transform.position.z);
}
}