问题描述
我正在尝试使用leptonica的pixOtsuAdaptiveThreshold函数但运气不佳。我不确定我是否做得对,但我试图采用UIImage(目标-C),应用pixOtsuAdaptiveThreshold,然后转换回UIImage。当我用我传入的参数调用pixOtsuAdaptiveThreshold时它会崩溃。
I am trying to use leptonica's pixOtsuAdaptiveThreshold function but with little luck. I am not sure if I am doing it right but I am trying to take an UIImage (objective-C), apply the pixOtsuAdaptiveThreshold and then convert back to UIImage. It crashes when I call pixOtsuAdaptiveThreshold with the parameters I have passed in.
谁能告诉我我做错了什么?我一直在为此奋斗2天,我想我正在失去理智。谢谢!
Can anyone tell me what I am doing wrong? I have been struggling with this for 2 days and I think I am losing my mind. Thanks!
代码
-(void)leptnoica:(UIImage *)image {
CGImageRef myCGImage = image.CGImage;
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myCGImage));
const UInt8 *imageData = CFDataGetBytePtr(data);
PIX *myPix = (PIX *) malloc(sizeof(PIX));
myPix->w = (int)CGImageGetWidth (myCGImage);
myPix->h = (int)CGImageGetHeight (myCGImage);
myPix->d = (int)CGImageGetBitsPerComponent(myCGImage);
myPix->wpl = (CGImageGetBytesPerRow (myCGImage)/4);
// myPix->informat = IFF_TIFF;
myPix->informat= IFF_PNG;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;
l_int32 one=300;
PIX *pixg;
PIX *pixB;
pixg = (PIX *) malloc(sizeof(PIX));
pixg=pixConvertTo8(myPix, 0);
l_float32 scorefract=0.1f;
pixOtsuAdaptiveThreshold(pixg, one, one, 0, 0, scorefract,NULL,&pixB);
推荐答案
首先,你应该总是使用leptonica构造函数(例如,pixCreate())和leptonica访问器(例如,pixGetDimensions())。切勿直接访问任何字段。所有访问器测试参数,并且设计为不会使程序崩溃,即使您输入错误。查看prog目录中的一些程序以获取使用示例。
First, you should always use the leptonica constructors (e.g., pixCreate()) and the leptonica accessors (e.g., pixGetDimensions()). Never access any fields directly. All accessors test arguments and are designed not to crash your program, even if you give bad input. Look at some of the programs in the prog directory for examples of usage.
其次,CGImage可能具有与leptonica不同的字节,并且可能有不同的字节像素和线填充。您需要了解这两种数据格式并进行适当的转换。
Second, the CGImage may have the bytes in a different order from leptonica, and it may have different pixel and line padding. You need to understand both data formats and make the appropriate conversion.
这篇关于Leptonica适用门槛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!