首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Leptonica -应用otsu阈值后无法写入图像

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

Stack Overflow用户
提问于 2014-09-30 16:09:03
回答 1查看 1.5K关注 0票数 3

我试图保存一个图像后,与轻子处理jpeg。我对ctype使用python,我的代码是:

代码语言:javascript
复制
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之后,但在写新的图片之前,做一些事情。我遗漏了什么?

编辑-

我现在已经核对了下面的每一个轻子子文档8c.html

代码语言:javascript
复制
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)

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

最大支持的图像维数是pixWriteStreamJpeg中的65500像素错误:pixWriteJpeg中的内部jpeg错误: pix未写入流。

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

编辑2*

当我检查输出img使用轻子子,它是告诉我,宽度是一些很大的数字,似乎变化,每次我运行的函数(如149996048),高度保持在相同的值与输入图像。它看起来像otsu函数,由于某种原因将图像宽度更改为这个大值。

编辑3

下面的jsbueno为我提供了这个问题的解决方案,我将在此与大家分享。这个问题是因为我将图像直接传递给函数,而实际上需要传递指向函数的指针,然后函数才能工作。最后工作守则如下:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-07 15:25:38

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

此外,最后两个参数是轻子子PIX对象的“指针”--您可以创建包含ctypes.c_void_p对象的ctypes.addressof变量,并将它传递给轻子子函数。

outsu函数的文档如下:

代码语言:javascript
复制
/*------------------------------------------------------------------*
 *                 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
 *

PS。在研究这个问题的过程中,我成功地为轻子子1.7.1构建了python-轻子子绑定--一旦我清理了我为达到这个目标所做的混乱,我就应该发布另一个版本。

对于任何能够运行python-leptonica的人来说,就像现在一样(在没有黑客攻击的情况下,软弱无力地运行轻子子1.6.0 )--使用这个函数的代码应该是这样的:

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26125333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档