0

I'm trying to create a code that allows me to clear images to be read by and OCR engine. So far I haven't been able to accomplish much. Only install Imagemagink on my iOS project and use some of the API functions to create a Gray Scale image.

What I'm trying to achieve is something like this:

convert \( $infile -colorspace gray -type grayscale -contrast-stretch 0 \) \ \( -clone 0 -colorspace gray -negate -lat ${filtersize}x${filtersize}+${offset}% -contrast-stretch 0 \) \ -compose copy_opacity -composite -fill "$bgcolor" -opaque none +matte \ -deskew 40% -sharpen 0x1 \ $outfile

Now the problem is that I haven't been able to replicate that on My Code. So my questions are:

  1. Does any one knows a way of using the "ConvertImageCommand()" method using an UIImage as input instead of a file path? (Example here: http://www.imagemagick.org/discourse-server/viewtopic.php?t=25704)

  2. As an alternative and keep on going with my actual code, could anyone point me in the right direction of converting the above command into an API method to complete the code below? So far I've found that the "-lat" option equivalent in the API is: "MagickAdaptiveThresholdImage()"

regards,

-(UIImage *)drawMonochromeImage:(UIImage *)image
{
    // Create temporary file
    NSString *tempFilePath = [NSTemporaryDirectory()
                              stringByAppendingPathComponent:@"temp.jpg"];

    MagickWandGenesis();
    MagickWand *wand = NewMagickWand();
    NSData *data = UIImagePNGRepresentation(image);
    MagickReadImageBlob(wand, [data bytes], [data length]);

    // Monochrome image
    //MagickQuantizeImage(wand,2,GRAYColorspace,1,MagickFalse,MagickFalse);


    MagickDespeckleImage(wand);
    MagickEnhanceImage(wand);

    MagickQuantizeImage(wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);
    MagickBrightnessContrastImage(wand,-40,30);

    // Write to temporary file
    MagickWriteImage(wand,
                     [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]
                     );

    // Load UIImage from temporary file
    UIImage *imgObj = [UIImage imageWithContentsOfFile:tempFilePath];

    // Display on device
    return imgObj;
   // [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
tomcontr
  • 98
  • 9
  • possible duplicate of [Using the C API for ImageMagick (on iPhone?) to convert to monochrome?](http://stackoverflow.com/questions/18267432/using-the-c-api-for-imagemagick-on-iphone-to-convert-to-monochrome) – emcconville Dec 22 '14 at 12:22
  • I managed to use the code on that post and make it work passing a UIImage instead of a file and then return a UIImage. I also added two other functions for Enhance the image and B&C. It works but now I need is to be able to do something like this (code below) but just using the API not the Convert tool: `convert \( $infile -colorspace gray -type grayscale -contrast-stretch 0 \) \ \( -clone 0 -colorspace gray -negate -lat ${filtersize}x${filtersize}+${offset}% -contrast-stretch 0 \) \ -compose copy_opacity -composite -fill "$bgcolor" -opaque none +matte \ -deskew 40% -sharpen 0x1 \ $outfile` – tomcontr Dec 22 '14 at 12:56
  • Great! Please updated your question with the additional details, and clarify your issue to a [minimal question](http://stackoverflow.com/help/mcve). – emcconville Dec 22 '14 at 16:26

1 Answers1

0

Finally I was able to use the "ConvertImageCommand()" function of the ImageMagick API for C and then used this code to generate the image with grate results!.

char *argv[] = {"convert","-respect-parenthesis","\(", input, "-set", "-colorspace", "RGB", "-colorspace", "gray", "-type", "grayscale","-colorspace","RGB", "-normalize","\)","\(","-clone", "0","-set","-colorspace", "RGB","-colorspace","gray","-negate", "-lat", "15x15+5%", "-contrast-stretch", "0","\)","-compose","copy_opacity","-composite","-fill","white","-opaque","none","+matte", output, NULL};

I hope this can be useful to someone.

Regards,

tomcontr
  • 98
  • 9