4

I need to convert between OpenCV Mat image and Leptonica Pix image formats in C++. This is being used for binarization of 8bit gray images.

loot
  • 150
  • 1
  • 9

3 Answers3

9

I found found @ikarliga's answer worked for me because what I needed was to actually convert to the Mat format not necessarily use it with the Tesseract API which is what that OP was asking.

Here are both the functions I use. The pix8ToMat function is taken from the node-dv project

Pix *mat8ToPix(cv::Mat *mat8)
{
    Pix *pixd = pixCreate(mat8->size().width, mat8->size().height, 8);
    for(int y=0; y<mat8->rows; y++) {
        for(int x=0; x<mat8->cols; x++) {
            pixSetPixel(pixd, x, y, (l_uint32) mat8->at<uchar>(y,x));
        }
    }
    return pixd;
}

cv::Mat pix8ToMat(Pix *pix8)
{
    cv::Mat mat(cv::Size(pix8->w, pix8->h), CV_8UC1);
    uint32_t *line = pix8->data;
    for (uint32_t y = 0; y < pix8->h; ++y) {
        for (uint32_t x = 0; x < pix8->w; ++x) {
            mat.at<uchar>(y, x) = GET_DATA_BYTE(line, x);
        }
        line += pix8->wpl;
    }
    return mat;
}
Community
  • 1
  • 1
loot
  • 150
  • 1
  • 9
  • Was going to post this as an answer [here](http://stackoverflow.com/questions/27000797/converting-mat-to-pix-to-setimage/39273665) but it is not quite appropriate hence I'm answering my own question. – loot Sep 02 '16 at 13:49
  • I'm quite unsure if this code really works. It could fail if the source image is greyscale and we'd like to output an RGB one. – Aurus Huang Dec 08 '17 at 05:32
  • @AurusHuang I think the problem is `mat.at(y, x)`. Opencv seem don't support usinged uint32 [as this list](https://i.stack.imgur.com/tIUjg.png) – yode Dec 08 '17 at 07:09
  • @yode No, it seems to be, but actually it's not, based on my observation on the order of bytes in Mat and PIX*. I'm expecting an efficient and correct conversion between them, no matter what colour mode and what depth the source image is. – Aurus Huang Dec 08 '17 at 09:29
  • By the way, why leptonica is not using raw storage? – ceztko Aug 26 '18 at 22:22
2

I know that i'm about 4 years late and nobody cares, but this task has frustrated me a lot 'cause leptonica's documentation is just awful.
The problem with @loot's answer is that it does not work with colored images, so here is my (maybe crude) modification of his pix8ToMat:

cv::Mat pixToMat(Pix *pix) {
    int width = pixGetWidth(pix);
    int height = pixGetHeight(pix);
    int depth = pixGetDepth(pix);

    cv::Mat mat(cv::Size(width, height), depth == 1 ? CV_8UC1 : CV_8UC3);

    for (uint32_t y = 0; y < height; ++y) {
        for (uint32_t x = 0; x < width; ++x) {
            if (depth == 1) {
                l_uint32 val;
                pixGetPixel(pix, x, y, &val);
                mat.at<uchar>(cv::Point(x, y)) = static_cast<uchar>(255 * val);
            } else {
                l_int32 r, g, b;
                pixGetRGBPixel(pix, x, y, &r, &g, &b);

                cv::Vec3b color(b, g, r);
                mat.at<cv::Vec3b>(cv::Point(x, y)) = color;
            }
        }
    }

    return mat;
}
  • I marked as correct answer because as you say it supports color images. I have not tested but looks like it should work fine. It might perform better by moving the depth check out of the for loops. – loot Dec 04 '20 at 20:42
  • @ProstoiChelovek, how about coversion from Mat to Pix, which is the reverse progress? – Gary Chen Apr 26 '21 at 11:27
1

To convert pix to Mat add these line in @loot code before calling pix8toMat.

Pix *8bitPix = pixConvert1To8(NULL, pixt, 255, 0);

Now send this 8bitPix to mat conversion. [it works for binary image]