参考书目:《OpenCV计算机视觉基础教程》–夏帮贵。
代码编写:Jupyter Notebook。
# 1.全局阈值处理
# 全局阈值处理:将大于阈值的像素值设置为255,将其他像素值设置为0;
# 或将大于阈值的像素值设置为0,将其他像素值设置为255;
# 语法格式:retval, dst = cv2.threshold(src, thresh, maxval, type)
# 参数说明:
# a.retval:返回的阈值;
# b.dst:处理后的结果图像;
# c.src:原图像;
# d.thresh:设置的阈值;
# e.maxval:阈值类型;THRESH_BINARY、THRESH_BINARY_INV时使用的最大值;
# f.type:阈值类型;
# 1.1 二值化阈值处理
# 二值化阈值处理:将大于阈值的像素值设置为255,将其他像素值设置为0;
"""
假设图像像素如下:
[[123, 230, 187],
[176, 150, 200],
[230, 180, 135]]
将阈值设置为150,二值化阈值处理如下:
[[0, 255, 255],
[255, 0, 255],
[255, 255, 0]]
"""
import cv2 as cv
img1 = cv.imread("apollo_car.JPG")
ret2, img2 = cv.threshold(img1, 100, 255, cv.THRESH_BINARY)
ret3, img3 = cv.threshold(img1, 150, 255, cv.THRESH_BINARY)
ret4, img4 = cv.threshold(img1, 200, 255, cv.THRESH_BINARY)
cv.imshow("apollo", img1)
cv.imshow("apollo100", img2)
cv.imshow("apollo150", img3)
cv.imshow("apollo200", img4)
cv.waitKey(0)
# 1.2 反二值化阈值处理
# 反二值化阈值处理:将大于阈值的像素值设置为0,将其他像素值设置为255;
"""
假设图像像素如下:
[[123, 230, 187],
[176, 150, 200],
[230, 180, 135]]
将阈值设置为150,二值化阈值处理如下:
[[255, 0, 0],
[0, 255, 0],
[0, 0, 255]]
"""
import cv2 as cv
img1 = cv.imread("apollo_car.JPG")
ret2, img2 = cv.threshold(img1, 100, 255, cv.THRESH_BINARY_INV)
ret3, img3 = cv.threshold(img1, 150, 255, cv.THRESH_BINARY_INV)
ret4, img4 = cv.threshold(img1, 200, 255, cv.THRESH_BINARY_INV)
cv.imshow("apollo", img1)
cv.imshow("apollo100", img2)
cv.imshow("apollo150", img3)
cv.imshow("apollo200", img4)
cv.waitKey(0)
# 1.3 截断阈值处理
# 截断阈值处理:将大于阈值的像素值设置为阈值,其他像素值保持不变
"""
假设图像像素如下:
[[123, 230, 187],
[176, 150, 200],
[230, 180, 135]]
将阈值设置为150,二值化阈值处理如下:
[[123, 150, 150],
[150, 150, 150],
[150, 150, 135]]
"""
import cv2 as cv
img1 = cv.imread("apollo_car.JPG")
ret2, img2 = cv.threshold(img1, 100, 255, cv.THRESH_TRUNC)
ret3, img3 = cv.threshold(img1, 150, 255, cv.THRESH_TRUNC)
ret4, img4 = cv.threshold(img1, 200, 255, cv.THRESH_TRUNC)
cv.imshow("apollo", img1)
cv.imshow("apollo100", img2)
cv.imshow("apollo150", img3)
cv.imshow("apollo200", img4)
cv.waitKey(0)
# 1.4 超阈值零处理
# 超阈值零处理:将大于阈值的像素值设置为0,其他像素保持不变;
"""
假设图像像素如下:
[[123, 230, 187],
[176, 150, 200],
[230, 180, 135]]
将阈值设置为150,二值化阈值处理如下:
[[123, 0, 0],
[0, 150, 0],
[0, 0, 135]]
"""
import cv2 as cv
img1 = cv.imread("apollo_car.JPG")
ret2, img2 = cv.threshold(img1, 100, 255, cv.THRESH_TOZERO)
ret3, img3 = cv.threshold(img1, 150, 255, cv.THRESH_TOZERO)
ret4, img4 = cv.threshold(img1, 200, 255, cv.THRESH_TOZERO)
cv.imshow("apollo", img1)
cv.imshow("apollo100", img2)
cv.imshow("apollo150", img3)
cv.imshow("apollo200", img4)
cv.waitKey(0)
# 1.5 低阈值零处理
# 低阈值零处理:将小于阈值的像素值设置为0,其他像素保持不变;
"""
假设图像像素如下:
[[123, 230, 187],
[176, 150, 200],
[230, 180, 135]]
将阈值设置为150,二值化阈值处理如下:
[[0, 230, 187],
[176, 150, 200],
[230, 180, 0]]
"""
import cv2 as cv
img1 = cv.imread("apollo_car.JPG")
ret2, img2 = cv.threshold(img1, 100, 255, cv.THRESH_TOZERO_INV)
ret3, img3 = cv.threshold(img1, 150, 255, cv.THRESH_TOZERO_INV)
ret4, img4 = cv.threshold(img1, 200, 255, cv.THRESH_TOZERO_INV)
cv.imshow("apollo", img1)
cv.imshow("apollo100", img2)
cv.imshow("apollo150", img3)
cv.imshow("apollo200", img4)
cv.waitKey(0)
# 1.6 Otsu算法阈值处理
# 对于色彩不均衡图像,Otsu算法阈值处理方式更好,该算法会遍历当前图像的所有阈值,再选择最佳阈值;
import cv2 as cv
img1 = cv.imread("apollo_car.JPG", cv.IMREAD_GRAYSCALE)
ret2, img2 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY)
ret3, img3 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
ret4, img4 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
cv.imshow("apollo", img1)
cv.imshow("apollo_binary", img2)
cv.imshow("apollo_binary_otsu", img3)
cv.imshow("apollo_binary_inv_otsu", img4)
cv.waitKey(0)
# 1.7 三角算法阈值处理
import cv2 as cv
img1 = cv.imread("apollo_car.JPG", cv.IMREAD_GRAYSCALE)
ret2, img2 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY)
ret3, img3 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY + cv.THRESH_TRIANGLE)
ret4, img4 = cv.threshold(img1, 127, 255, cv.THRESH_BINARY_INV + cv.THRESH_TRIANGLE)
cv.imshow("apollo", img1)
cv.imshow("apollo_binary", img2)
cv.imshow("apollo_binary_triangle", img3)
cv.imshow("apollo_binary_inv_triangle", img4)
cv.waitKey(0)
# 2.自适应阈值处理
# 自适应阈值处理(局部阈值处理):通过计算每个像素点邻域的加权平均值确定阈值,并用该阈值处理当前像素点;
# 全局阈值处理适用于色彩均衡的图像,自适应阈值处理适用于明暗差异较大的图像;
# 语法格式:dst = cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
# 参数说明:
# a.dst:处理后结果图像;
# b.src:原图像;
# c.maxValue:最大值;
# d.adaptiveMethod:自适应方法;
# d.1:cv2.ADAPTIVE_THRESH_MEAN_C,领域中所有像素点权重值相同;
# d.2:cv2.ADAPTIVE_THRESH_GAUSSIAN_C,领域中像素点的权重值与其到中心点的距离有关,通过高斯方程计算各个点的权重值;
# e.thresholdType:阈值处理方式;
# e.1:cv2.THRESH_BINARY,二值化阈值处理;
# e.2:cv2.THRESH_BINARY_INV,反二值化阈值处理;
# f.blockSize:计算局部阈值的邻域的大小;
# g.C:常量;自适应阈值为blockSize指定领域的加权平均值减去C;
import cv2 as cv
img1 = cv.imread("apollo_car.JPG", cv.IMREAD_GRAYSCALE)
img2 = cv.adaptiveThreshold(img1, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 5, 10)
img3 = cv.adaptiveThreshold(img1, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 5, 10)
img4 = cv.adaptiveThreshold(img1, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 5, 10)
img5 = cv.adaptiveThreshold(img1, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 5, 10)
cv.imshow("apollo", img1)
cv.imshow("mean_binary", img2)
cv.imshow("mean_binary_inv", img3)
cv.imshow("gaussian_binary", img4)
cv.imshow("gaussian_binary_inv", img5)
cv.waitKey(0)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)