在OpenCV Mat和Leptonica Pix之间转换 [英] Convert between OpenCV Mat and Leptonica Pix

查看:306
本文介绍了在OpenCV Mat和Leptonica Pix之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C ++中的OpenCV Mat图像和Leptonica Pix图像格式之间转换.这用于8位灰度图像的二值化.

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.

推荐答案

我发现找到了 @ikarliga的答案因为我需要的是实际转换为Mat格式,而不一定要与OP要求的Tesseract API结合使用.

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.

这是我使用的两个功能. pix8ToMat函数取自 node-dv项目

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

这篇关于在OpenCV Mat和Leptonica Pix之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆