Convert between OpenCV Mat and Leptonica Pix

只愿长相守 提交于 2019-12-06 09:59:49
loot

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;
}

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]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!