【发布时间】:2014-12-22 04:54:32
【问题描述】:
我正在尝试创建一个代码,允许我清除要由 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
现在的问题是我无法在“我的代码”上复制它。所以我的问题是:
有没有人知道使用 UIImage 作为输入而不是文件路径的“ConvertImageCommand()”方法? (此处示例:http://www.imagemagick.org/discourse-server/viewtopic.php?t=25704)
作为替代方案并继续使用我的实际代码,谁能指出我将上述命令转换为 API 方法以完成以下代码的正确方向? 到目前为止,我发现 API 中等效的“-lat”选项是:“MagickAdaptiveThresholdImage()”
问候,
-(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];
}
【问题讨论】:
-
我设法使用该帖子上的代码并使其工作,传递一个 UIImage 而不是一个文件,然后返回一个 UIImage。我还为增强图像和 B&C 添加了另外两个功能。它可以工作,但现在我需要能够做这样的事情(下面的代码),但只使用 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 -
太棒了!请使用其他详细信息更新您的问题,并通过minimal question 澄清您的问题。
标签: ios imagemagick ocr tesseract imagemagick-convert