2 回答

TA貢獻1829條經驗 獲得超4個贊
這是在 Python/OpenCV 中執行此操作的一種方法。
讀取輸入
增加對比度
將原始圖像轉換為灰度
自適應閾值
使用閾值圖像使對比度增加圖像上的背景變白
保存結果
輸入:
import cv2
import numpy as np
# read image
img = cv2.imread("math_diagram.jpg")
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 15)
# make background of input white where thresh is white
result = img.copy()
result[thresh==255] = (255,255,255)
# write results to disk
cv2.imwrite("math_diagram_threshold.jpg", thresh)
cv2.imwrite("math_diagram_processed.jpg", result)
# display it
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
閾值圖像:
結果:

TA貢獻1851條經驗 獲得超3個贊
您可以使用任何本地二值化方法。在 OpenCV 中,有一種稱為 Wolf-Julion 局部二值化的方法可以應用于輸入圖像。以下是代碼片段作為示例:
import cv2
image = cv2.imread('input.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)[:,:,2]
T = cv2.ximgproc.niBlackThreshold(gray, maxValue=255, type=cv2.THRESH_BINARY_INV, blockSize=81, k=0.1, binarizationMethod=cv2.ximgproc.BINARIZATION_WOLF)
grayb = (gray > T).astype("uint8") * 255
cv2.imshow("Binary", grayb)
cv2.waitKey(0)
上面代碼的輸出結果如下。請注意,要使用ximgproc模塊,您需要安裝 opencv contrib 包。
添加回答
舉報