for loop crashes unity
I intend to catch the camera, turn it to HSV and use this to do threshold for hand gesture tracking. But my for loop seems to crash Unity. I want to use a for loop since this shows the actions behind an image processing method, instead of using a library to do it for me. In the end i want to do convex hull detection and convex hull defects detection to return a number between 0 to 5 depending on the number of fingers showing, this 0 to 5 will then tell our game to use a spell or not depending on the number. This was just a bit of context tho, just need to get this for loop to work for now.
using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV.Features2D; using Emgu.CV.Util; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; using Emgu.CV.Aruco;
public class GestureTrackingScript : MonoBehaviour {
public VideoCapture cap;
public RawImage camImage;
int iteration = 0;
// Use this for initialization
void Start()
{
Debug.Log("Startup initiated...");
cap = new VideoCapture(0); // instantiate a webcamera
cap.SetCaptureProperty(CapProp.Fps, 30); // The FPS of the camera
Debug.Log("Startup finished. Running...");
}
// Update is called once per frame
void Update()
{
Image<Hsv, byte> frame = cap.QueryFrame().ToImage<Hsv, byte>(); // the input image (frame from camera)
for (int j = 0; j < frame.Height; j++){
for (int i = 0; i < frame.Width; i++){
//lower
frame = frame.ThresholdToZero(new Hsv(10.0, 22.0, 10.0));
//upper
frame = frame.ThresholdToZeroInv(new Hsv(100.0, 29.0, 100.0));
}
}
MemoryStream _memory = new MemoryStream();
frame.Bitmap.Save(_memory, frame.Bitmap.RawFormat);
Texture2D camera = new Texture2D(400, 400);
camera.LoadImage(_memory.ToArray());
camImage.texture = camera;
camImage.material.mainTexture = camera;
Debug.Log("Ping Number " + iteration);
iteration++;
}
}
Can update, that removing one axies of the for loop, shows that the program does in fact work, just very very laggy with even one axies, so i believe with two axies the program simply crashes ? What could cause this?
Answer by Gamborg · Nov 24, 2017 at 08:34 AM
Problem was that emguCV functions by default goes over all pixels, if i then put that into a for loop that also goes over all pixels, it because too big a load for the system :) So simply removing the for loops fixed it.
Your answer
