文章目录
前言
局部二值化较全局二值化难,我们将在此实现Python与FPGA的局部二值化处理。
一、局部二值化
局部二值化就是使用一个窗口,在图像上进行扫描,每扫出9个像素求平均,再和阈值进行比较。如果9个像素的平均值大与等于阈值,就将窗口中心对应的图像像素设置为255,否则就设置为0。
二、Python局部二值化
以下虽然是局部二值化处理,但是在此基础上,增加了阈值乘以一个ratio小数,可以调整阈值,实现局部阈值二值化处理。
import numpy as np
import matplotlib.pyplot as plt
img = plt.imread("lenna.png")
gray = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2]
gray = gray * 255#图像是[0-1]--->[0-255]
def local_threshold(gray, ratio, threshold, size=3):
h, w = gray.shape
m = int((size - 1) / 2)
local_image = np.zeros((h, w))
for i in range(m, h - m):
for j in range(m, w - m