Convert between OpenCV Mat and Leptonica Pix

前端 未结 3 1568
清酒与你
清酒与你 2021-02-06 16:06

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.

相关标签:
3条回答
  • 2021-02-06 16:52

    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]

    0 讨论(0)
  • 2021-02-06 16:53

    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;
    }
    
    0 讨论(0)
  • 2021-02-06 17:07

    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;
    }
    
    0 讨论(0)
提交回复
热议问题