title: Image is rotated and stretched automatically using Open CV iOS. How to fix?
body: <p>I am using OpenCV 3. I have installed the framework in my Xcode project using POD. To convert the image (Captured by camera) into black and white, I am using <em>Adaptive Gaussian Thresholding</em>. Below is the code I have used </p>
  
  <pre><code>@implementation MyClass
  
  +(UIImage *)toBlackAndWhite:(UIImage *)s {    
          cv::Mat input;
  cv::Mat output;
  input = [MyClass cvMatFromUIImage:s];
  cv::cvtColor(input, input, cv::COLOR_BGR2GRAY);
  output = cv::Mat(input.cols, input.rows, IPL_DEPTH_8U, 1);
  cv::adaptiveThreshold(input, output, 255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY, 75, 25);
  return [MyClass imageWithCVMat:output];    
  }
  //Ref:Open CV documentation
  + (cv::Mat)cvMatFromUIImage:(UIImage *)image
  {
      CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
      CGFloat cols = image.size.width;
      CGFloat rows = image.size.height;
  
      cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
  
      CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to  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), image.CGImage);
      CGContextRelease(contextRef);
  
      return cvMat;
  }
  
  //Ref:Open CV documentation
  + (UIImage *)imageWithCVMat:(const cv::Mat&amp;)cvMat
  {
      NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize() * cvMat.total()];
  
      CGColorSpaceRef colorSpace;
  
      if (cvMat.elemSize() == 1) {
          colorSpace = CGColorSpaceCreateDeviceGray();
      } else {
          colorSpace = CGColorSpaceCreateDeviceRGB();
      }
  
      CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  
      CGImageRef imageRef = CGImageCreate(cvMat.cols,                                     // Width
                                          cvMat.rows,                                     // Height
                                          8,                                              // Bits per component
                                          8 * cvMat.elemSize(),                           // Bits per pixel
                                          cvMat.step[0],                                  // Bytes per row
                                          colorSpace,                                     // Colorspace
                                          kCGImageAlphaNone | kCGBitmapByteOrderDefault,  // Bitmap info flags
                                          provider,                                       // CGDataProviderRef
                                          NULL,                                           // Decode
                                          false,                                          // Should interpolate
                                          kCGRenderingIntentDefault);                     // Intent
  
      UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
      CGImageRelease(imageRef);
      CGDataProviderRelease(provider);
      CGColorSpaceRelease(colorSpace);
  
      return image;
  }
  
  
  @end
  </code></pre>
  
  <p>Problem: The image I'm getting, is rotated 90°, anti-clock wise and stretched. Please suggest something how can I fix it. Please see</p>
  
  <p><a href="https://i.stack.imgur.com/6GSxU.jpg" rel="nofollow noreferrer">Original Image</a></p>
  
  <p><a href="https://i.stack.imgur.com/NriwE.jpg" rel="nofollow noreferrer">Processed Image</a></p>