python - Leptonica - 应用 otsu 阈值后无法写入图像

标签 python ctypes leptonica

我试图在使用 leptonica 处理后将图像保存为 jpeg。我将 python 与 ctypes 一起使用,我的代码是:

import ctypes

leptlib = "liblept.so"
leptonica = ctypes.cdll.LoadLibrary(leptlib)

filename = "IMAG0724.jpg"
img = leptonica.pixRead(filename)

leptonica.pixConvertTo8.argtypes = [ctypes.c_void_p, 
                                    ctypes.c_int32]

pix_image = leptonica.pixConvertTo8(img, False)

leptonica.pixOtsuAdaptiveThreshold.argtypes = [ctypes.c_void_p, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_float]

otsu = leptonica.pixOtsuAdaptiveThreshold(pix_image,20,20,0,0,0.1)

leptonica.pixWriteJpeg.argtypes = [ctypes.c_void_p, 
                                   ctypes.c_void_p, 
                                   ctypes.c_int32, 
                                   ctypes.c_int32]

leptonica.pixWriteJpeg("otsu-lept", otsu, 75, 0)

此代码产生错误:

pixWriteJpeg 错误:pix 未定义

我相信这是因为我需要在应用 otsu 之后但在写入新图像之前做一些事情。我错过了什么?

编辑-

我现在根据 leptonica 文档修改了以下内容 http://tpgit.github.io/Leptonica/binarize_8c.html :

leptonica.pixOtsuAdaptiveThreshold.argtypes = [ctypes.c_void_p, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_float,
                                               ctypes.c_void_p]

leptonica.pixOtsuAdaptiveThreshold(pix_image,20,20,0,0,0.1, img)

leptonica.pixWriteJpeg("otsu-lept", img, 75, 0)

现在出现了一个新的错误:

支持的最大图像尺寸为 65500 像素 pixWriteStreamJpeg 错误:内部 jpeg 错误 pixWriteJpeg 错误:pix 未写入流

我的图像分辨率是 1552 x 2592,当 otsu 函数行被注释掉时,leptonica.pixWriteJpeg 可以工作,所以问题似乎仍然出在 otsu 函数返回的图像上。

**** 编辑 2 ****

当我使用 leptonica 检查输出 img 时,它告诉我宽度是一个很大的数字,每次我运行该函数时似乎都会有所不同(例如 149996048),并且高度保持正确,与输入图像的值相同。看起来 otsu 函数出于某种原因将图像宽度更改为这么大的值。

编辑 3

下面的 jsbueno 为我提供了解决这个问题的方法,我将在这里分享。问题是因为当实际上需要将指针的指针传递给函数然后工作时,我将图像直接传递给函数。最终工作代码如下:

import ctypes

leptlib = "liblept.so"
leptonica = ctypes.cdll.LoadLibrary(leptlib)

filename = "IMAG0724.jpg"
img = leptonica.pixRead(filename)

leptonica.pixConvertTo8.argtypes = [ctypes.c_void_p, 
                                    ctypes.c_int32
                                    ]

pix_image = leptonica.pixConvertTo8(img, False)
w = leptonica.pixGetWidth(img)
h = leptonica.pixGetHeight(img)
pixa_out = leptonica.pixCreate(w,h,8)
pixa = ctypes.c_void_p(pixa_out)
leptonica.pixOtsuAdaptiveThreshold.argtypes = [ctypes.c_void_p, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_int32, 
                                               ctypes.c_float,
                                               ctypes.c_void_p,
                                               ctypes.c_void_p
                                               ]

otsu = leptonica.pixOtsuAdaptiveThreshold(pix_image,
                                          20,
                                          20,
                                          0,
                                          0,
                                          0.1,
                                          None,
                                          ctypes.addressof(pixa)
                                          )


leptonica.pixWritePng("otsu-lept", pixa, 8)

最佳答案

我发现你做错了什么 - 如果你检查函数签名,它在末尾可选择 2 个互斥参数 - 其中一个是返回系数的数组,而不是图像 - 非常最后一个参数是一个空图像,用于绘制算法的应用。

此外,最后两个参数是 leptonica PIX 对象的“指向指针的指针”- 您可以创建一个包含 ctypes.c_void_p 对象的 Python 变量,并将 ctypes.addressof 到 leptonica 函数。

outsu函数的文档如下:

/*------------------------------------------------------------------*
 *                 Adaptive Otsu-based thresholding                 *
 *------------------------------------------------------------------*/
/*!
 *  pixOtsuAdaptiveThreshold()
 *
 *      Input:  pixs (8 bpp)
 *              sx, sy (desired tile dimensions; actual size may vary)
 *              smoothx, smoothy (half-width of convolution kernel applied to
 *                                threshold array: use 0 for no smoothing)
 *              scorefract (fraction of the max Otsu score; typ. 0.1;
 *                          use 0.0 for standard Otsu)
 *              &pixth (<optional return> array of threshold values
 *                      found for each tile)
 *              &pixd (<optional return> thresholded input pixs, based on
 *                     the threshold array)
 *      Return: 0 if OK, 1 on error
 *

附言。在研究这一点时,我设法为 leptonica 1.7.1 构建了 python-leptonica 绑定(bind)——一旦我清理了到达那里所做的困惑,我应该发布另一个版本。

对于任何能够运行 python-leptonica 的人来说,就像现在一样(仅限于 leptonica 1.6.0 而无需破解)- 使用此功能的代码将是这样的:

import leptonica
import ctypes
img = leptonica.functions.pixRead("t1.png")
imggray = leptonica.functions.pixConvertRGBToGrayMinMax(img, 1)
img = leptonica.functions.pixRead("t1.png")
output = leptonica.functions.pixCreate(imggray.w, imggray.h, 1)
a = ctypes.c_voidp()
leptonica.functions.pixOtsuAdaptiveThreshold(imggray, 20, 20, 0, 0, .1, None, ctypes.addressof(a))
output  = leptonica.PIX(from_address=a)
leptonica.functions.pixWritePng("t3.png", c, 1)

关于python - Leptonica - 应用 otsu 阈值后无法写入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125333/

相关文章:

python - 在 Python 中使用 C 类型

python - 使用 Ctypes 来自 Void 指针的 C 类实例

c++ - 如何将位图转换为内存中的 PIX?

image-segmentation - 使用 Leptonica 进行字符分割

python - Django BinaryField 检索为内存位置?

python - 将字符串解析为类层次结构

python找到两个列表之间的差异

python - 在 Python 中使用上面的箱线图绘制直方图

python - 预期字节或整数地址而不是 str 实例 Python 3

opencv - Leptonica OpenCV Java 将 Mat 转换为 Pix,反之亦然