By changing the sample code as follows, you can show correct results in portrait mode.
Can be detected object from another direction by rotating the Mat converted from WebCamTexture.
Edit fix the memory leak that occurs in rgbaMat.t().
using UnityEngine;
using System.Collections;
using OpenCVForUnity;
namespace OpenCVForUnitySample
{
/// <summary>
/// WebCamTexture detect face sample.
/// </summary>
public class RoteteWebCamTextureDetectFaceSample : MonoBehaviour
{
WebCamTexture webCamTexture;
Color32[] colors;
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
bool isFront = false;
#endif
int width = 640;
int height = 480;
Mat rgbaMat;
Mat RotatedRgbaMat;
Mat grayMat;
Texture2D texture;
CascadeClassifier cascade;
MatOfRect faces;
bool initDone = false;
// Use this for initialization
void Start ()
{
// Checks how many and which cameras are available on the device
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
if (WebCamTexture.devices [cameraIndex].isFrontFacing == isFront) {
#endif
Debug.Log (cameraIndex + " name " + WebCamTexture.devices [cameraIndex].name + " isFrontFacing " + WebCamTexture.devices [cameraIndex].isFrontFacing);
//Set the appropriate fps
webCamTexture = new WebCamTexture (WebCamTexture.devices [cameraIndex].name, width, height, 3);
#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
break;
}
#endif
}
Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
// Starts the camera
webCamTexture.Play ();
StartCoroutine (init ());
}
private IEnumerator init ()
{
while (true) {
//If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
if (webCamTexture.didUpdateThisFrame) {
Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
colors = new Color32[webCamTexture.width * webCamTexture.height];
rgbaMat = new Mat (webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
//replace width and height
RotatedRgbaMat = new Mat (webCamTexture.width, webCamTexture.height, CvType.CV_8UC4);
grayMat = new Mat (webCamTexture.width, webCamTexture.height, CvType.CV_8UC1);
texture = new Texture2D (webCamTexture.height, webCamTexture.width, TextureFormat.RGBA32, false);
gameObject.transform.eulerAngles = new Vector3 (0, 0, 0);
gameObject.transform.localScale = new Vector3 (webCamTexture.height, webCamTexture.width, 1);
cascade = new CascadeClassifier (Utils.getFilePath ("haarcascade_frontalface_alt.xml"));
faces = new MatOfRect ();
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
initDone = true;
break;
} else {
yield return 0;
}
}
}
// Update is called once per frame
void Update ()
{
if (!initDone)
return;
if (webCamTexture.didUpdateThisFrame) {
Utils.WebCamTextureToMat (webCamTexture, rgbaMat, colors);
// //rotate by -90 degrees.
// Core.flip(rgbaMat.t(),RotatedRgbaMat,1);
//fix the memory leak that occurs in rgbaMat.t().
//rotate by -90 degrees.
using (Mat transposeRgbaMat = rgbaMat.t()) {
Core.flip (transposeRgbaMat, RotatedRgbaMat, 1);
}
Imgproc.cvtColor (RotatedRgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
Imgproc.equalizeHist (grayMat, grayMat);
if (cascade != null)
cascade.detectMultiScale (grayMat, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size (50, 50), new Size ());
OpenCVForUnity.Rect[] rects = faces.toArray ();
for (int i = 0; i < rects.Length; i++) {
// Debug.Log ("detect faces " + rects );
Core.rectangle (RotatedRgbaMat, new Point (rects .x, rects .y), new Point (rects .x + rects .width, rects .y + rects .height), new Scalar (255, 0, 0, 255), 2);
}
Utils.matToTexture2D (RotatedRgbaMat, texture, colors);
}
}
void OnDisable ()
{
webCamTexture.Stop ();
}
void OnGUI ()
{
float screenScale = Screen.width / 240.0f;
Matrix4x4 scaledMatrix = Matrix4x4.Scale (new Vector3 (screenScale, screenScale, screenScale));
GUI.matrix = scaledMatrix;
GUILayout.BeginVertical ();
if (GUILayout.Button ("back")) {
Application.LoadLevel ("OpenCVForUnitySample");
}
GUILayout.EndVertical ();
}
}
}