title: iOS detect rectangles from camera with openCV
body: <p>I'm trying to detect the edges of a business card (and draw them) with an iPhone camera, using openCV. I'm new to this framework, as well as Computer Vision or C++.</p>
  
  <p>I'm trying to use the solution explained here: <a href="https://stackoverflow.com/a/14123682/3708095">https://stackoverflow.com/a/14123682/3708095</a>, which github project is <a href="https://github.com/foundry/OpenCVSquares" rel="nofollow noreferrer">https://github.com/foundry/OpenCVSquares</a></p>
  
  <p>It works with a predefined image, but I'm trying to get it working with the camera.</p>
  
  <p>To do so, I'm using the <code>CvVideoCameraDelegate protocol</code> implementing it in <code>CVViewController.mm</code> like they explain in <a href="http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html" rel="nofollow noreferrer">http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html</a> like this:</p>
  
  <pre><code>#ifdef __cplusplus
  -(void)processImage:(cv::Mat &amp;)matImage
  {
  //NSLog (@"Processing Image");
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
      matImage = CVSquares::detectedSquaresInImage(matImage, self.tolerance, self.threshold, self.levels, [self accuracy]);
  
      UIImage *image = [[UIImage alloc]initWithCVMat:matImage orientation:UIImageOrientationDown];
  
      dispatch_async(dispatch_get_main_queue(), ^{
          self.imageView.image = image;
      });
  });
  
  }
  #endif
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>If I do it like this, it gives me a EXC_BAD_ACCESS...</p>
  
  <p>If I clone the matImage before processing it, by logging it, it seems to process the image and even find rectangles, but the rectangle is not drawn to the image output to the imageView.</p>
  
  <pre><code>cv::Mat temp = matImage.clone();    
  
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
      UIImage *image = [[UIImage alloc]CVSquares::detectedSquaresInImage(temp, self.tolerance, self.threshold, self.levels, [self accuracy])
                                         orientation:UIImageOrientationDown];
  
      dispatch_async(dispatch_get_main_queue(), ^{
          self.imageView.image = image;
      });
  });
  </code></pre>
  
  <p>I'm pretty sure I'm missing something, probably because I'm not passing correctly some object, ot pointer to object or so, and the object that I need to be modified is not.</p>
  
  <p>Anyway, If this is not the right approach, I would really appreciate a tutorial or example where they do something like this, either using openCV or <a href="https://github.com/BradLarson/GPUImage" rel="nofollow noreferrer">GPUImage</a> (not familiar with it either)...</p>