我試圖用leptonica處理後將圖像保存爲jpeg。我使用Python和ctypes的,我的代碼是:Leptonica - 應用otsu閾值後無法寫入圖像
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。我錯過了什麼?
編輯 -
我現在已經ammended每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)
可以檢查類型'otsu'?它看起來像一個帶有錯誤代碼的int,而不是一個pix指針。 – 101 2014-10-01 11:45:51
是的類型是int。感謝你,我剛剛意識到這是爲什麼,並已修改函數,以反映這通過添加ctypes.c_void_p到otsu的argtypes列表的末尾,並提供一個輸出圖像的功能。它不返回一個不同的錯誤。請參閱上述問題的修正。 – ajrasp 2014-10-01 13:22:15
'pixOtsuAdaptiveThreshold()'是否返回通過或錯誤?看起來你有一個比需要更少的參數。 – 101 2014-10-01 23:21:26