Page 1 of 2
iOS Black and White Image and C API
Posted: 2014-12-21T21:41:37-07:00
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];
}
Re: iOS Black and White Image and C API
Posted: 2014-12-21T21:52:39-07:00
by fmw42
Use the API equivalent of the command -lat. That is what I used in my script, textcleaner.
Re: iOS Black and White Image and C API
Posted: 2014-12-22T07:55:06-07:00
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!
Re: iOS Black and White Image and C API
Posted: 2014-12-22T18:45:32-07:00
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];
}
Re: iOS Black and White Image and C API
Posted: 2014-12-22T20:25:25-07:00
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
Re: iOS Black and White Image and C API
Posted: 2014-12-22T21:25:35-07:00
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!
Re: iOS Black and White Image and C API
Posted: 2014-12-22T21:43:13-07:00
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
Re: iOS Black and White Image and C API
Posted: 2014-12-22T21:55:11-07:00
by tomcontr
sorry for my ignorance, but same thing for the edges?
Regards!
Re: iOS Black and White Image and C API
Posted: 2014-12-22T22:17:59-07:00
by fmw42
Sorry, what is the issue with the edges?
Re: iOS Black and White Image and C API
Posted: 2014-12-23T06:10:20-07:00
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.
Re: iOS Black and White Image and C API
Posted: 2014-12-23T10:43:10-07:00
by fmw42
Re: iOS Black and White Image and C API
Posted: 2014-12-24T08:26:46-07:00
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.
Re: iOS Black and White Image and C API
Posted: 2014-12-24T08:56:55-07:00
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};
Re: iOS Black and White Image and C API
Posted: 2014-12-24T10:11:23-07:00
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.
Re: iOS Black and White Image and C API
Posted: 2014-12-24T14:38:55-07:00
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:
Thanks a lot for your help!
regards and merry Christmas!