其他分享
首页 > 其他分享> > 史上最简单&最全&最基础&入门到精通的opencv图像处理 第七课:图像阈值

史上最简单&最全&最基础&入门到精通的opencv图像处理 第七课:图像阈值

作者:互联网

一、代码部分

代码如下(示例):

#图像阈值
import cv2 as cv#opencv BGR
import matplotlib.pyplot as plt #包导入
import numpy as np

img=cv.imread('C:/Users/akaak/Pictures/OpenCV/cat.png')
img_gray=cv.imread('C:/Users/akaak/Pictures/OpenCV/cat.png',cv.IMREAD_GRAYSCALE)
ret, thresh1 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY)
ret, thresh2 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY_INV)
ret, thresh3 = cv.threshold(img_gray, 127, 255, cv.THRESH_TRUNC)
ret, thresh4 = cv.threshold(img_gray, 127, 255, cv.THRESH_TOZERO)
ret, thresh5 = cv.threshold(img_gray, 127, 255, cv.THRESH_TOZERO_INV)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

二、运行结果

在这里插入图片描述

在这里插入图片描述

总结

学习了图像阈值处理。

标签:gray,plt,阈值,img,第七课,opencv,THRESH,127,cv
来源: https://blog.csdn.net/qq_45825952/article/details/121295742