参考我们的评论,我在这里编写了该算法的 MATLAB 实现:从图像中的统一背景中提取页面 https://stackoverflow.com/questions/30487127/extract-a-page-from-a-uniform-background-in-an-image/30496377#30496377,而且在大图像上速度相当快。
如果您想更好地解释该算法,请参阅我的其他答案:Bradley 自适应阈值——困惑(问题) https://stackoverflow.com/questions/29593939/bradley-adaptive-thresholding-confused-questions/29599155#29599155。如果您想更好地理解我编写的代码,这可能是一个很好的起点。
由于 MATLAB 和 NumPy 相似,因此这是 Bradley-Roth 阈值算法的重新实现,但在 NumPy 中。我将 PIL 图像转换为 NumPy 数组,对此图像进行处理,然后转换回 PIL 图像。该函数接受三个参数: 灰度图像image
,窗口的大小s
和阈值t
。该阈值与您所拥有的阈值不同,因为它完全遵循论文。门槛t
is a 百分比每个像素窗口的总面积之和。如果总面积小于此阈值,则输出应该是黑色像素 - 否则它是白色像素。默认为s
and t
分别是列数除以 8 并四舍五入后的 15%:
import numpy as np
from PIL import Image
def bradley_roth_numpy(image, s=None, t=None):
# Convert image to numpy array
img = np.array(image).astype(np.float)
# Default window size is round(cols/8)
if s is None:
s = np.round(img.shape[1]/8)
# Default threshold is 15% of the total
# area in the window
if t is None:
t = 15.0
# Compute integral image
intImage = np.cumsum(np.cumsum(img, axis=1), axis=0)
# Define grid of points
(rows,cols) = img.shape[:2]
(X,Y) = np.meshgrid(np.arange(cols), np.arange(rows))
# Make into 1D grid of coordinates for easier access
X = X.ravel()
Y = Y.ravel()
# Ensure s is even so that we are able to index into the image
# properly
s = s + np.mod(s,2)
# Access the four corners of each neighbourhood
x1 = X - s/2
x2 = X + s/2
y1 = Y - s/2
y2 = Y + s/2
# Ensure no coordinates are out of bounds
x1[x1 < 0] = 0
x2[x2 >= cols] = cols-1
y1[y1 < 0] = 0
y2[y2 >= rows] = rows-1
# Ensures coordinates are integer
x1 = x1.astype(np.int)
x2 = x2.astype(np.int)
y1 = y1.astype(np.int)
y2 = y2.astype(np.int)
# Count how many pixels are in each neighbourhood
count = (x2 - x1) * (y2 - y1)
# Compute the row and column coordinates to access
# each corner of the neighbourhood for the integral image
f1_x = x2
f1_y = y2
f2_x = x2
f2_y = y1 - 1
f2_y[f2_y < 0] = 0
f3_x = x1-1
f3_x[f3_x < 0] = 0
f3_y = y2
f4_x = f3_x
f4_y = f2_y
# Compute areas of each window
sums = intImage[f1_y, f1_x] - intImage[f2_y, f2_x] - intImage[f3_y, f3_x] + intImage[f4_y, f4_x]
# Compute thresholded image and reshape into a 2D grid
out = np.ones(rows*cols, dtype=np.bool)
out[img.ravel()*count <= sums*(100.0 - t)/100.0] = False
# Also convert back to uint8
out = 255*np.reshape(out, (rows, cols)).astype(np.uint8)
# Return PIL image back to user
return Image.fromarray(out)
if __name__ == '__main__':
img = Image.open('test.jpg').convert('L')
out = bradley_roth_numpy(img)
out.show()
out.save('output.jpg')
如果需要,图像被读入并转换为灰度。将显示输出图像,并将其保存到您将脚本运行到名为的图像的同一目录中output.jpg
。如果您想覆盖这些设置,只需执行以下操作:
out = bradley_roth_numpy(img, windowsize, threshold)
尝试这样做以获得良好的结果。使用默认参数并使用 IPython,我使用以下方法测量了平均执行时间timeit
,这就是我从您在帖子中上传的图片中得到的信息:
In [16]: %timeit bradley_roth_numpy(img)
100 loops, best of 3: 7.68 ms per loop
这意味着在您上传的图像上重复运行此函数 100 次,最好的 3 次执行时间平均每次运行 7.68 毫秒。
当我设置阈值时,我也得到了这个图像: