Python-OpenCV中的cv2.threshold


  主要记录Python-OpenCV中的cv2,threshold()方法;官方文档


cv2.threshold()

def threshold(src, thresh, maxval, type, dst=None):
"""
设置固定级别的阈值应用于多通道矩阵
    例如,将灰度图像变换二值图像,或去除指定级别的噪声,或过滤掉过小或者过大的像素点;
Argument:
    src: 原图像
    dst: 目标图像
    thresh: 阈值
    type: 指定阈值类型;下面会列出具体类型;
    maxval: 当type指定为THRESH_BINARY或THRESH_BINARY_INV时,需要设置该值;
"""

  其中type的类型设置入下:

ubuntu_002.png

示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# @Time    : 19-4-20 下午5:07
# @Author  : chen

import cv2
import matplotlib.pyplot as plt

lena_BGR = cv2.imread("./input_01.png")
lena_RGB = cv2.cvtColor(lena_BGR, cv2.COLOR_BGR2RGB)

# display BGR lena
plt.subplot(1, 3, 1)
plt.imshow(lena_BGR)
plt.axis('off')
plt.title('img_BGR')

# display RGB lena
plt.subplot(1, 3, 2)
plt.imshow(lena_RGB)
plt.axis('off')
plt.title('img_RGB')

# 转换成灰度图像,并执行高斯模糊
gray = cv2.cvtColor(lena_RGB, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)

# 将图像中小于60的置为0,大于60的置为255
# 返回的temp是一个元组,temp[0]表示设置的阈值,也就是60; temp[1]是变换后的图像
temp = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)
thresh = temp[0]
lena_thresh = temp[1]


# display lena_thresh image
plt.subplot(1, 3, 3)
plt.imshow(lena_thresh, cmap='gray')
plt.axis('off')
plt.title('img_thresh')

plt.show()

ubuntu_003.png

转载于:https://www.cnblogs.com/chenzhen0530/p/10742540.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值