我正在將一個使用 leptonica 庫的 go 程式翻譯成一個使用 openCV 的 python 腳本,但我找不到一種方法來翻譯以下內容:
img = pixReduceRankBinary2(img, 2, NULL);
img = pixReduceRankBinary2(img, 2, NULL);
img = pixCloseBrick(NULL, img, 2, 3);
openCV 中是否有這些功能的實作?
uj5u.com熱心網友回復:
這是使用 Python/OpenCV/Skimage 做到這一點的一種方法。只需讀取影像并確保它是二進制的。使用 Skimage downscale_local_mean 使用 2x2 鄰域將采樣降低 2,生成兩個較小影像的因子,其值是 2x2 鄰域的均值。然后再次以 25% 的增量設定閾值,以對應其 pixReduceRankBinary2 函式的 leptonica 計數。
輸入(米粒):

import cv2
import numpy as np
import skimage.transform
# read image
img = cv2.imread('rice_bw.png')
# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold to binary
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
# decimate by factor of 2 to mean values in 2x2 regions
decimate = skimage.transform.downscale_local_mean(thresh, (2,2))
# set count to 1,2,3 or 4
count=2
# convert count to percent of 255 in increments of 25%
count_thresh = 255 * (25/100) * count
# threshold again
result = cv2.threshold(decimate, count_thresh, 255, cv2.THRESH_BINARY)[1]
# write result to disk
cv2.imwrite("rice_bw_binary_decimate.png", result)
# show result
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
count=2 的結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338187.html
