title: CGBitmapContextCreate for CV_8UC3 (to use in OpenCV)
body: <p>I am trying to use the people detection function in OpenCV:</p>
  
  <pre><code>cv::HOGDescriptor hog;
  hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
  std::vector&lt;cv::Rect&gt; found;
  hog.detectMultiScale(noMask, found, 0.2, cv::Size(8,8), cv::Size(16,16), 1.05, 2);
  </code></pre>
  
  <p>But I get the following assertion:</p>
  
  <blockquote>
    <p>OpenCV Error: Assertion failed (img.type() == CV_8U || img.type() ==
    CV_8UC3) in computeGradient, file
    /Users/robin/Projects/OpenCVForiPhone/opencv/opencv/modules/objdetect/src/hog.cpp,
    line 174</p>
  </blockquote>
  
  <p>And it makes sense because I am passing a CV_8UC4 image. </p>
  
  <p>So I guessed I should create a cvmat with this characteristics. Right now I have these 2 methods. which allow me to get gray or color cvmats (CV_8UC1/CV_8UC4)</p>
  
  <p><strong>For Color:</strong></p>
  
  <pre><code>-(cv::Mat)CVMat
  {
  
      CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
      CGFloat cols = self.size.width;
      CGFloat rows = self.size.height;
  
      cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
  
      CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing data
                                                      cols,                      // Width of bitmap
                                                      rows,                     // Height of bitmap
                                                      8,                          // Bits per component
                                                      cvMat.step[0],              // Bytes per row
                                                      colorSpace,                 // Colorspace
                                                      kCGImageAlphaNoneSkipLast |
                                                      kCGBitmapByteOrderDefault); // Bitmap info flags
  
      CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
      CGContextRelease(contextRef);
  
      return cvMat;
  }
  </code></pre>
  
  <p><strong>For Grayscale:</strong>    </p>
  
  <pre><code>-(cv::Mat)CVGrayscaleMat
  {
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
      CGFloat cols = self.size.width;
      CGFloat rows = self.size.height;
  
      cv::Mat cvMat = cv::Mat(rows, cols, CV_8UC1); // 8 bits per component, 1 channel
  
      CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing data
                                                      cols,                      // Width of bitmap
                                                      rows,                     // Height of bitmap
                                                      8,                          // Bits per component
                                                      cvMat.step[0],              // Bytes per row
                                                      colorSpace,                 // Colorspace
                                                      kCGImageAlphaNone |
                                                      kCGBitmapByteOrderDefault); // Bitmap info flags
  
      CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
      CGContextRelease(contextRef);
      CGColorSpaceRelease(colorSpace);
  
      return cvMat;
  }
  </code></pre>
  
  <p><strong>And this is my guess to make it 3 channels:</strong></p>
  
  <pre><code>-(cv::Mat)CVMat3Channels
  {
  
      //CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      CGFloat cols = self.size.width;
      CGFloat rows = self.size.height;
  
      cv::Mat cvMat(rows, cols, CV_8UC3); // 8 bits per component, 3 channels
  
      CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                // Pointer to backing data
                                                      cols,                      // Width of bitmap
                                                      rows,                     // Height of bitmap
                                                      8,                          // Bits per component
                                                      cvMat.step[0],              // Bytes per row
                                                      colorSpace,                 // Colorspace
                                                      kCGImageAlphaNoneSkipLast |
                                                      kCGBitmapByteOrderDefault); // Bitmap info flags
  
      CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
      CGContextRelease(contextRef);
      CGColorSpaceRelease(colorSpace);
  
      return cvMat;
  }
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <pre><code>&lt;Error&gt;: CGBitmapContextCreate: invalid data bytes/row: should be 
           at least 9792 for 8 integer bits/component, 3 components, 
           kCGImageAlphaNoneSkipLast. 
  &lt;Error&gt;: CGContextDrawImage: invalid context 0x0
  </code></pre>
  
  <p>So my question is, What is the correct way of creating a 8UC3 compatible CGBitmapContext?
  (I assume the 8UC3 means 8 bits per pixel with RGB channels)</p>
  
  <p>Thank you.</p>
  
  <p>PD: The image transformation code is from Robin Summerhill.</p>