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

2024-04-19 22:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图保存一个图像后,与瘦肉精处理jpeg。我在ctypes中使用python,我的代码是:

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

我相信这是因为我需要做一些事情后,应用大津,但在写新的形象。我错过了什么?在

编辑-

我现在对每一个leptonica文档http://tpgit.github.io/Leptonica/binarize_8c.html进行了如下修改:

^{pr2}$

现在出现一个新错误:

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

我的图像分辨率是1552 x 2592瘦肉精当otsu函数行被注释掉时有效,因此看起来问题仍然是由otsu函数返回的图像。在

****编辑2****

当我使用leptonica检查输出img时,它告诉我宽度是一个很大的数字,每次我运行函数(例如149996048)时,宽度似乎都会发生变化,高度与输入图像的值相同。由于某种原因,otsu函数将图像宽度更改为这个大值。在

编辑3

下面的问题解决方案将与我分享。问题是因为我将图像直接传递给函数,而实际上需要传递指向函数的指针指针。最终工作代码如下:

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)

Tags: 函数图像imageimgfilenamectypesvoidint32
1条回答
网友
1楼 · 发布于 2024-04-19 22:41:15

我发现你做错了什么-如果你检查函数签名,它在结尾处有选择地使用两个互斥的参数-其中一个是一个数组,用来返回系数,而不是一个图像-最后一个参数是一个空图像,用来绘制算法的应用程序。在

此外,最后两个参数是leptonica PIX对象的“指针”- 您可以创建一个包含ctypes.c\u void\u p对象的Python变量,并传递 ctypes.addressof它是轻子函数。在

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
 *

在研究这个问题时,我设法为leptonica1.7.1构建了python-leptonica绑定——一旦我清理了之前做的工作,我就应该发布另一个版本。在

对于任何能够运行python leptonica的人来说,就像现在一样(仅限于leptonica 1.6.0而不受黑客攻击),使用此函数的代码应该是:

^{pr2}$

相关问题 更多 >