title: Scanned Document - Text & Background clarity not good using OpenCV + iOS
body: <p>After the scanning the document, I am applying the image processing using OpenCV library. I am not getting the quality of the scanned document like the Scannable iOS app.</p>
  
  <p>I am using below code for image processing:</p>
  
  <pre><code>- (UIImage *)applyImageProcessing:(UIImage *)aImage
  {
      cv::Mat originalMat = [self cvMatFromUIImage:aImage];
      cv::Mat dest_mat(aImage.size.width, aImage.size.height, CV_8UC4);
      cv::Mat intermediate_mat(aImage.size.width, aImage.size.height, CV_8UC4);
  
      cv::multiply(originalMat, 0.5, intermediate_mat);
      cv::add(originalMat, intermediate_mat, dest_mat);
  
      return [self UIImageFromCVMat:dest_mat];
  }
  
  - (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,     // Color space
                                                  kCGImageAlphaNoneSkipLast
                                                  | kCGBitmapByteOrderDefault); // Bitmap info flags
  
      CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
      CGContextRelease(contextRef);
      return cvMat;
  }
  
  - (UIImage *)UIImageFromCVMat:(cv::Mat)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);
  
      // Create CGImage from cv::Mat
      CGImageRef imageRef = CGImageCreate(cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorspace, kCGImageAlphaNone | kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault);
  
      // get uiimage from cgimage
      UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
      CGImageRelease(imageRef);
      CGDataProviderRelease(provider);
      CGColorSpaceRelease(colorspace);
      return finalImage;
  }
  </code></pre>
  
  <p><strong>My App Scanned document quality &amp; clearity</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ZVgGk.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ZVgGk.jpg" alt=""></a></p>
  
  <p><strong>Scannable iOS App Scanned document quality &amp; clearity</strong></p>
  
  <p><a href="https://i.stack.imgur.com/1K91k.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/1K91k.jpg" alt=""></a></p>
  
  <p>How can I get result of my scanned document like as scannble app?</p>
  
  <hr>
  
  <p>Original image:</p>
  
  <p><a href="https://s31.postimg.cc/4kzs6ha23/IMG04.jpg" rel="noreferrer"><img src="https://s31.postimg.cc/4kzs6ha23/IMG04.jpg" alt=""></a></p>
  
  <p>Scannable app original image:</p>
  
  <p><a href="https://s31.postimg.cc/4xr6con6z/IMG05.jpg" rel="noreferrer"><img src="https://s31.postimg.cc/4xr6con6z/IMG05.jpg" alt=""></a></p>