title: Silhouette detection in iOS
body: <p>I am trying to detect the silhouette of a person in iOS using OpenCV 2.4.9. I am using the background subtraction using mixture of gaussians to achieve it. Ref : <a href="http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf" rel="nofollow">http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf</a> and  <a href="http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#backgroundsubtractormog" rel="nofollow">http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html#backgroundsubtractormog</a></p>
  
  <p>Here is my code snippet ; </p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
  
      //UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!"      message:@"Welcome to OpenCV" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
      //[alert show];
  
      self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView]; // Sets up the camera
      self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront; // Back-Front  Camera
      self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
      self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
      self.videoCamera.defaultFPS = 30; // FPS
      self.videoCamera.delegate = self;
      //self.videoCamera.grayscale = NO; 
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (void)processImage:(Mat&amp;)image
  {
      cvtColor( image, image, COLOR_BGR2GRAY );
      vector&lt;vector&lt;cv::Point&gt; &gt; contours;
      vector&lt;Vec4i&gt; hierarchy;
  
      // Canny edge detector ...
      Canny(image, image, 100, 300); // Adjust thresholds 
      threshold(image, image, 100, 255, THRESH_BINARY_INV);
      // find contours
      findContours(image, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE,     cv::Point(0,0));
  
      int idx = 0;
      for (; idx &gt;= 0; idx = hierarchy[idx][0] )
      {
          drawContours( image, contours, idx, 255, 1, 4, hierarchy ); 
      }
      bitwise_not(image, image);
  
      Ptr&lt;BackgroundSubtractorMOG2&gt; B = Algorithm::create&lt;BackgroundSubtractorMOG2&gt;("BackgroundSubtractor.MOG2");
  
      B-&gt;setDetectShadows(false);
      B-&gt;setNMixtures(3);
  
      B-&gt;createBackgroundSubtractorMOG2(image,fore);
      B-&gt;apply(image,fore);
      B-&gt;getBackgroundImage(bg);
      erode(fore, fore, Mat());
      dilate(fore, fore, Mat());
  
      findContours(fore, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
      drawContours(image, contours, -1, Scalar(0,255,0),2);
  }
  </code></pre>
  
  <p>And I get a Thread7:EXC_BAD_ACCESS(code=1,address=0x0) at </p>
  
  <pre><code>B-&gt;setDetectShadows(false);
  </code></pre>
  
  <p>meaning my B is NULL.</p>
  
  <p>I did some debugging and found that B gets initialized first and then set to NULL within OpenCV ;</p>
  
  <pre><code>inline
  String::String(const char* s)
      : cstr_(0), len_(0)
  {
      if (!s) return;
      size_t len = strlen(s);
      memcpy(allocate(len), s, len);
  }
  
  
  template&lt;typename _Tp&gt; inline
  Ptr&lt;_Tp&gt; Algorithm::create(const String&amp; name)
  {
      return _create(name).dynamicCast&lt;_Tp&gt;();
  }
  
  template&lt;typename T&gt;
  template&lt;typename Y&gt;
  Ptr&lt;Y&gt; Ptr&lt;T&gt;::dynamicCast() const
  {
      return Ptr&lt;Y&gt;(*this, dynamic_cast&lt;Y*&gt;(stored));
  }
  </code></pre>
  
  <p>and then finally set to NULL !?</p>
  
  <pre><code>template&lt;typename T&gt;
  void Ptr&lt;T&gt;::release()
  {
      if (owner) owner-&gt;decRef();
      owner = NULL;
      stored = NULL;
  }
  </code></pre>
  
  <p>Am I doing something wrong and/or how can this be corrected ?</p>
  
  <p>I have done this project in the past in C++ using Visual Studio and OpenCV 2.3 and I could just create an object of type BackgroundSubtractorMOG2 and just use setNMixtures and so on. But for some reason this class in iOS is abstract.</p>