title: ios GPUImage,bad result of image processing with small-size images?
body: <p>I'm trying prepare image for OCR,I use GPUImage to do it,code work fine till i crop image!!After cropping i got bad result...</p>
  
  <p>Crop area:
  <a href="https://www.dropbox.com/s/e3mlp25sl6m55yk/IMG_0709.PNG" rel="nofollow">https://www.dropbox.com/s/e3mlp25sl6m55yk/IMG_0709.PNG</a></p>
  
  <p>Bad Result=(
  <a href="https://www.dropbox.com/s/wtxw7li6paltx21/IMG_0710.PNG" rel="nofollow">https://www.dropbox.com/s/wtxw7li6paltx21/IMG_0710.PNG</a></p>
  
  <pre><code>+ (UIImage *) doBinarize:(UIImage *)sourceImage
  {
  
      //first off, try to grayscale the image using iOS core Image routine
      UIImage * grayScaledImg = [self grayImage:sourceImage];
  
      GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:grayScaledImg];
  
      GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init];
      stillImageFilter.blurRadiusInPixels = 8.0;
      [stillImageFilter prepareForImageCapture];
  
      [imageSource addTarget:stillImageFilter];
      [imageSource processImage];
      UIImage *retImage = [stillImageFilter imageFromCurrentlyProcessedOutput];
  
      [imageSource removeAllTargets];
  
      return retImage;
  }
  
  
  + (UIImage *) grayImage :(UIImage *)inputImage
  {
      // Create a graphic context.
      UIGraphicsBeginImageContextWithOptions(inputImage.size, NO, 1.0);
      CGRect imageRect = CGRectMake(0, 0, inputImage.size.width, inputImage.size.height);
  
      // Draw the image with the luminosity blend mode.
      // On top of a white background, this will give a black and white image.
      [inputImage drawInRect:imageRect blendMode:kCGBlendModeLuminosity alpha:1.0];
  
      // Get the resulting image.
      UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return outputImage;
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <blockquote>
    <p>In the meantime, when you crop your images, do so to the nearest
    multiple of 8 pixels in width and you should see the correct result</p>
  </blockquote>
  
  <p>Thank u @Brad Larson !  i resize image width to nearest multiple of 8 and get what i want</p>
  
  <pre><code>-(UIImage*)imageWithMultiple8ImageWidth:(UIImage*)image
  {
      float fixSize = next8(image.size.width);
  
      CGSize newSize = CGSizeMake(fixSize, image.size.height);
      UIGraphicsBeginImageContext( newSize );
      [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      return newImage;
  }
  
  float next8(float n) {
  
      int bits = (int)n &amp; 7; // give us the distance to the previous 8
      if (bits == 0)
          return (float)n;
      return (float)n + (8-bits);
  }
  </code></pre>