[图像处理]——图像阈值

图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。OpenCV中提供了函数cv::threshold();

如下图所示即为OpenCV中关于阈值的函数,其中:设置的阈值是针对每个像素点判断的,如果当前像素点的值大于阈值,赋什么值,maxval最大取值为255(0<黑>~255<白>) 

 简单使用:

import matplotlib.pyplot as plt
import cv2 as cv

img = cv.imread("images/girl.jpg", cv.IMREAD_COLOR)
image_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

ret, thresh1 = cv.threshold(image_gray, 127, 255, cv.THRESH_BINARY)
ret, thresh2 = cv.threshold(image_gray, 127, 255, cv.THRESH_BINARY_INV)
ret, thresh3 = cv.threshold(image_gray, 127, 255, cv.THRESH_TRUNC)
ret, thresh4 = cv.threshold(image_gray, 127, 255, cv.THRESH_TOZERO)
ret, thresh5 = cv.threshold(image_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)
    if(i == 0):
        plt.imshow(images[i])
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值