iOS Black and White Image and C API

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

iOS Black and White Image and C API

Post by tomcontr »

Hi guys,

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: http://www.fmwconcepts.com/imagemagick/textcleaner/

But I'm not too familiarized with the API. Does anyone knows how to do something like that using the API?

This is my code by the way.

regards,

Code: Select all

-(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];
}
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

Use the API equivalent of the command -lat. That is what I used in my script, textcleaner.
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

Thanks!

This is what I´ve found.

Code: Select all

 MagickBooleanType MagickAdaptiveThresholdImage(MagickWand *wand,
    const size_t width,const size_t height,const ssize_t offset)
I will try it and see what comes out.

Regards!
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

Ok, so after diggin around I came up with the code below. The input image and output image are at the bottom:

My goal is to clean the image and have a clean monochrome image which black font and white background.

Hope thant you could give me some pointers,
regards!

Code: Select all

-(UIImage *)drawMonochromeImage:(UIImage *)image
{
    // Create temporary file
    NSString *tempFilePath = [NSTemporaryDirectory()
                              stringByAppendingPathComponent:@"temp.jpg"];
    
    UIImage *image2 = [UIImage imageNamed:@"IMG_2517.JPG"];
    CGImageRef imageRef = [image2 CGImage];
    
    MagickWandGenesis();
    MagickWand *wand = NewMagickWand();
    MagickWand *wand2 = NewMagickWand();
    NSData *data = UIImagePNGRepresentation(image2);
    MagickReadImageBlob(wand, [data bytes], [data length]);
    
    // Monochrome image
    //MagickQuantizeImage(wand,2,GRAYColorspace,1,MagickFalse,MagickFalse);
    
    MagickQuantizeImage(wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);
    
    //MagickDespeckleImage(wand);
    //MagickEnhanceImage(wand);
    
    wand2 = CloneMagickWand(wand);
    MagickNegateImage(wand2,TRUE);
    MagickAdaptiveThresholdImage(wand2,320,103,0); //self.aa);
    MagickContrastImage(wand2,0);
    
    MagickContrastImage(wand,0);
    
    
    MagickSetImageCompose(wand2,CopyOpacityCompositeOp);
    
    
    PixelWand *color = NewPixelWand();
    PixelSetColor(color, "white");
    
    PixelWand *color2 = NewPixelWand();
    PixelSetColor(color2, "black");
    
    
    MagickSetImageBackgroundColor(wand2,color);
    
    MagickCompositeImage(wand,wand2,OverCompositeOp,0,0);
    
    MagickOpaquePaintImage(wand,color2,color,40,true); // this doesnt do anything.
    
    MagickSharpenImage(wand,0.0,1.0);

    
    //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];
}
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

try the equivalent of this:

convert test.png -gamma 0.25 -auto-level -negate -lat 30x30+10% -negate result.png

-lat works best if the "object" is white and the background is black when you use the bias term
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

It worked!!

this is the code:


MagickLevelImage(wand,0.0,0.25,MaxRGB);
MagickNegateImage(wand,false);
MagickAdaptiveThresholdImage(wand,30,30,10);
MagickNegateImage(wand,false);

Im almos there. Now I need to do two more things.

1. clear the image a little bit more. For example remove black edges, and sometimes there are small black dots. Is there anything to clean that as much as it can?

2. Is there anything I can do so the numbers stay as much clear as possible. Some times they are a little bit distorted.

Regards!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

1) If the black dots are small, then remove them using -morphology open or close depending upon polarity of the dots. If they are black on white background then use close.

2) Not much that I can think of besides adjusting the contrast, which is what the -gamma did. If they have small holes in them then you can use -morphology open. Often I just do both in series (or use -morphology smooth)

Better resolution images and not using JPG could help further.


see
http://www.imagemagick.org/Usage/morphology/#open
http://www.imagemagick.org/Usage/morphology/#close
http://www.imagemagick.org/Usage/morphology/#smooth
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

sorry for my ignorance, but same thing for the edges?

Regards!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

Sorry, what is the issue with the edges?
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

Some times the edges are black (or shadows) but they are not even on each one of them and that causes the OCR engine to read them as something. What I would like to do is to gently remove them trying to keep what it is in the middle.

For example in the image that I uploaded the white borders would be the black bordes that Im talking about.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

Only way I know would be to shave or chop off those edges. See
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#chop

or just crop the part you want
http://www.imagemagick.org/Usage/crop/#crop
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

Ok, I`ve made some progress. I was able to use for the first time the "ConvertImageCommand()" with the API,which I thnk would make things easier.

This code works fine:

Code: Select all

char *argv[] = {"convert", input, "-colorspace", "gray", "-type", "grayscale","-gamma", "0.25", "-auto-level", "-negate", "-lat", "30x30+10%", "-negate", output, NULL};
But This one gives me the erros below:

Code: Select all

char *argv[] = {"convert","-respect-parenthesis","\( ", input, "-set", "-colorspace", "RGB", "-colorspace", "gray", "-type", "-set","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};
MyStore: UnableToOpenBlob `( ': No such file or directory @ error/blob.c/OpenBlob/2645.
MyStore: UnableToOpenConfigureFile `delegates.xml' @ warning/configure.c/GetConfigureOptions/705.
MyStore: UnableToOpenBlob `( ': No such file or directory @ error/blob.c/OpenBlob/2645.
MyStore: NoDecodeDelegateForThisImageFormat `( ' @ error/constitute.c/ReadImage/501.
MyStore: UnrecognizedImageType `-set' @ error/convert.c/ConvertImageCommand/2986.
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

Never mind, found the error.
Here is the final code:

Code: Select all

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};
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: iOS Black and White Image and C API

Post by fmw42 »

You are using -colorspace RGB, which is linear and not the norm. It makes the image darker. Is that what you really want. Perhaps you should try -colorspace sRGB and see if that is better or worse. Sometimes one or the other works better for what you want to do.
tomcontr
Posts: 10
Joined: 2014-12-21T21:32:32-07:00
Authentication code: 6789

Re: iOS Black and White Image and C API

Post by tomcontr »

There is a tiny difference, but I think RGB serves more the purpose because it gives me a more define image.

Here is the comparison:

Image

Thanks a lot for your help!

regards and merry Christmas!
Post Reply