使用iOS中的Imagemagick清洁单色图像

时间:2014-12-22 04:54:32

标签: ios imagemagick ocr tesseract imagemagick-convert

我正在尝试创建一个代码,允许我清除要读取的图像和OCR引擎。到目前为止,我还没有取得多大成就。只在我的iOS项目上安装Imagemagink并使用一些API函数来创建灰度图像。

我想要实现的是这样的:

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

现在的问题是我无法在My Code上复制它。所以我的问题是:

  1. 有没有人知道使用UIImage作为输入而不是文件路径的“ConvertImageCommand()”方法? (例如:http://www.imagemagick.org/discourse-server/viewtopic.php?t=25704

  2. 作为替代方案并继续使用我的实际代码,有人能指出我将上述命令转换为API方法以完成下面的代码的正确方向吗? 到目前为止,我发现API中的“-lat”选项等价于:“MagickAdaptiveThresholdImage()”

  3. 的问候,

    -(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];
    }
    

1 个答案:

答案 0 :(得分:0)

最后我能够使用" ConvertImageCommand()" ImageMagick API for C的功能,然后使用此代码生成带有格栅结果的图像!。

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};

我希望这对某人有用。

此致