从UIImage/CGImage转换为Leptonica Pix结构

4

我想在我的iOS应用程序中使用Leptonica库进行图像处理,但是我不知道如何从UIImage获取Leptonica的Pix结构。我建议您尝试以下方法:

UIImage *image = [UIImage imageNamed:@"test.png"];
...
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *rasterData = CFDataGetBytePtr(data);

有人能建议如何正确将这些数据转换为Leptonica的Pix结构吗?
/*-------------------------------------------------------------------------*
 *                              Basic Pix                                  *
 *-------------------------------------------------------------------------*/
struct Pix
{
    l_uint32             w;           /* width in pixels                   */
    l_uint32             h;           /* height in pixels                  */
    l_uint32             d;           /* depth in bits                     */
    l_uint32             wpl;         /* 32-bit words/line                 */
    l_uint32             refcount;    /* reference count (1 if no clones)  */
    l_int32              xres;        /* image res (ppi) in x direction    */
                                      /* (use 0 if unknown)                */
    l_int32              yres;        /* image res (ppi) in y direction    */
                                      /* (use 0 if unknown)                */
    l_int32              informat;    /* input file format, IFF_*          */
    char                *text;        /* text string associated with pix   */
    struct PixColormap  *colormap;    /* colormap (may be null)            */
    l_uint32            *data;        /* the image data                    */
};

更新:

我正在这样做:

UIImage *image = [UIImage imageNamed:@"test.png"];

CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([image CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);

Pix *myPix = (Pix *) malloc(sizeof(Pix));

CGImageRef myCGImage = [image CGImage];

myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerComponent(myCGImage);
myPix->wpl = CGImageGetBytesPerRow (myCGImage) / 4;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;

NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));

但是我得到的结果与原图相差很大: http://dl.dropbox.com/u/4409984/so/lept-orig.png 对比: http://dl.dropbox.com/u/4409984/so/lept-res.png 我做错了什么?
2个回答

1
尝试以下代码:
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([picture CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);

Pix *myPix = (Pix *) malloc(sizeof(Pix));
CGImageRef myCGImage = [picture CGImage];

myPix->w = CGImageGetWidth (myCGImage);
myPix->h = CGImageGetHeight (myCGImage);
myPix->d = CGImageGetBitsPerPixel([picture CGImage]) ;
myPix->wpl = CGImageGetBytesPerRow (myCGImage)/4 ;
myPix->data = (l_uint32 *) imageData;
myPix->colormap = NULL;

pixEndianByteSwap(myPix);

NSLog(@"pixWrite=%d", pixWrite("/tmp/lept-res.bmp", myPix, IFF_BMP));

0
一旦您拥有了CGImage,您可以直接从CGImage中获取大部分信息。
CGImageRef myCGImage = [image CGImage];
struct Pix myPix;
myPix.w = CGImageGetWidth (myCGImage);
myPix.h = CGImageGetHeight (myCGImage);
myPix.d = CGImageGetBitsPerComponent (myCGImage);
myPix.wpl = CGImageGetBytesPerRow (myCGImage) / 4;
... etc. ...

这是一个显而易见的方法,但如何正确地执行它呢?我已经更新了我的问题,并附上了我得到的结果。我做错了什么? - mym
你可能只需要设置其他字段。如果你知道你的相机的分辨率,可以使用它,或者如果你有一种从输入图像中获取它的方法,可以使用它。否则,如果你不知道,72是一个标准默认值。 - user1118321
看起来有些东西误解了像素数据。我不熟悉Leptonica。Pix结构体中的“d”成员是表示每像素的位数还是每通道的位数?尝试将其乘以4,看看是否有帮助。否则,请查阅文档以了解每个字段的含义。 - user1118321

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接