为了在 scikit-image
中使用阈值算法,我们以 Otsu
的二值化算法对测试图像进行阈值处理为例进行介绍。第一步是导入所需的包:
from skimage.filters import threshold_otsu
from skimage import img_as_ubyte
然后使用 scikit-image
应用 Otsu
的二值化算法:
加载图像
image = cv2.imread(‘example.png’)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
基于 otsu 方法的返回阈值
thresh = threshold_otsu(gray_image)
生成布尔数组:
binary = gray_image > thresh
转换为uint8数据类型
binary = img_as_ubyte(binary)
可视化
def show_img_with_matplotlib(color_img, title, pos):
img_RGB = color_img[:, :, ::-1]
ax = plt.subplot(2, 2, pos)
plt.imshow(img_RGB)
plt.title(title, fontsize=8)
plt.axis(‘off’)
def show_hist_with_matplotlib_gray(hist, title, pos, color, t=-1):
ax = plt.subplot(2, 2, pos)
plt.xlabel(“bins”)
plt.ylabel(“number of pixels”)
plt.xlim([0, 256])
plt.axvline(x=t, color=‘m’, linestyle=‘–’)
plt.plot(hist, color=color)
show_img_with_matplotlib(image, “image”, 1)
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), “gray img”, 2)
show_hist_with_matplotlib_gray(hist, “grayscale histogram”, 3, ‘m’, thresh)
show_img_with_matplotlib(cv2.cvtColor(binary, cv2.COLOR_GRAY2BGR), “Otsu’s binarization (scikit-image)”, 4)
plt.show()
threshold_otsu(gray_image)
函数根据 Otsu
的二值化算法返回阈值。之后,使用此值构建二进制图像( dtype= bool
),最后应将其转换为 8 位无符号整数格式( dtype=uint8
)以进行可视化,使用img_as_ubyte()
函数完成转换过程。
程序输出如下图所示:
接下来,介绍下如何使用 scikit-image
中的一些其它阈值技术。
接下来,将对比 Otsu
、triangle
、Niblack
和 Sauvola
阈值技术进行阈值处理的不同效果。 Otsu
和 triangle
是全局阈值技术,而 Niblack
和 Sauvola
是局部阈值技术。当背景不均匀时,局部阈值技术则是更好的方法阈值处理方法。
同样的,第一步是导入所需的包:
from skimage.filters import threshold_otsu, threshold_triangle, threshold_niblack, threshold_sauvola
from skimage import img_as_ubyte
import cv2
import matplotlib.pyplot as plt
调用每个阈值方法( threshold_otsu()
、threshold_niblack()
、threshold_sauvola()
和 threshold_triangle()
),以使用 scikit-image
对比执行阈值操作:
Otsu
thresh_otsu = threshold_otsu(gray_image)
binary_otsu = gray_image > thresh_otsu
binary_otsu = img_as_ubyte(binary_otsu)
Niblack
thresh_niblack = threshold_niblack(gray_image, window_size=25, k=0.8)
binary_niblack = gray_image > thresh_niblack
binary_niblack = img_as_ubyte(binary_niblack)
Sauvola
thresh_sauvola = threshold_sauvola(gray_image, window_size=25)
binary_sauvola = gray_image > thresh_sauvola
binary_sauvola = img_as_ubyte(binary_sauvola)
triangle
thresh_triangle = threshold_triangle(gray_image)
binary_triangle = gray_image > thresh_triangle
binary_triangle = img_as_ubyte(binary_triangle)
程序输出如下图所示:
如上图所示,当图像不均匀时,局部阈值方法可以提供更好的结果。因此,可以将这些局部阈值方法应用于文本识别。
最后,我们了解一个更加有趣的阈值算法—— Multi-Otsu
阈值技术,其可用于将输入图像的像素分为多个不同的类别,每个类别根据图像内灰度的强度计算获得。
Multi-Otsu
根据所需类别的数量计算多个阈值,默认类数为3,此时将获得三个类别,算法返回两个阈值,由直方图中的红线表示。
import matplotlib
import numpy as np
import cv2
from skimage import data
from skimage.filters import threshold_multiotsu
image = cv2.imread(‘8.png’)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
使用默认值调用 threshold_multiotsu()
thresholds = threshold_multiotsu(image)
regions = np.digitize(image, bins=thresholds)
def show_img_with_matplotlib(img, title, pos, cmap):
ax = plt.subplot(1, 3, pos)
plt.imshow(img, cmap=cmap)
plt.title(title, fontsize=8)
plt.axis(‘off’)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(10, 3.5))
如果你也是看准了Python,想自学Python,在这里为大家准备了丰厚的免费学习大礼包,带大家一起学习,给大家剖析Python兼职、就业行情前景的这些事儿。
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
成为一个Python程序员专家或许需要花费数年时间,但是打下坚实的基础只要几周就可以,如果你按照我提供的学习路线以及资料有意识地去实践,你就有很大可能成功!
最后祝你好运!!!