opencv_6_阈值处理

  • 阈值处理是指剔除图像内像素值高于一定值或者低于一定值的像素点。

  • 例如,设定阈值为127,然后:

  • 将像素内所有像素值大于127的像素点的值设为255

  • 将像素内所有像素值低于127的像素点的值设为0

threshold函数

  • retval,dst = cv2.threshold(src, thresh,maxval.type)
  • 式中:
  • retval:代表返回的阈值
  • dst:代表阈值分割结果图像,与原始图像具有相同的大小和类型
  • src:代表要进行阈值分割的图像。
  • thresh:代表要设定的阈值
  • maxval:代表当type参数为THRESH_BINARY或者THRESH_BINARY_INV类型时,需要设定的最大值
  • type:代表阈值分割的类型。

二值化阈值处理(cv2.THRESH_BINARY)

  • 对于灰度值大于阈值thresh的像素点,将其灰度值设定为最大值
  • 对于灰度值小于或等于阈值thresh的像素点,将其灰度值设定为0
import cv2
from get_show_img import get_show
img = cv2.imread('data/dog.jpg',0)
r, rst = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
get_show(img, rst)


6output_3_0

反二值化阈值处理(cv2.THRESH_BINARY_INV)

  • 对于灰度值大于阈值的像素点,将其值设定为0
  • 对于灰度值小于或等于阈值的像素点,将其值设定为255
import cv2
from get_show_img import get_show
img = cv2.imread('data/dog.jpg',0)
r, rst = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
get_show(img, rst)


6output_5_0

截断阈值化处理(cv2.THRESH_TRUNC)

  • 对于像素大于127的像素点,其像素将被设定为127
  • 对于像素小于或等于127的像素点,其像素将保持不变
import cv2
from get_show_img import get_show
img = cv2.imread('data/dog.jpg',0)
r, rst = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
get_show(img, rst)


6output_7_0

超阈值零处理(cv2.THRESH_TOZERO_INV)

  • 对于像素值大于阈值的像素点,其像素值将被处理为0
  • 对于像素小于或等于阈值的像素点,其像素值将保持不变
import cv2
from get_show_img import get_show
img = cv2.imread('data/dog.jpg',0)
r, rst = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
get_show(img, rst)


6output_9_0

低阈值零处理(cv2.THRESH_TOZERO)

  • 对于像素值大于阈值的像素点,其值将保持不变。
  • 对于像素值小于或等于阈值的像素点,其值将被处理为0
import cv2
from get_show_img import get_show
img = cv2.imread('data/dog.jpg',0)
r, rst = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
get_show(img, rst)


6output_11_0

自适应阈值处理

  • dst=cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType,blockSize, C)
  • 式中:
  • dst:代表自适应阈值处理结果
  • src:代表要进行处理的原始图像。必须为8通道。
  • maxVaule:代表最大值
  • adaptiveMethod:代表自适应方法。
  • thresholdType:代表阈值处理方式,该值必须是cv2.THRESH_BINARY或者cv2.THRESH_BINARY_INV中的一个。
  • blockSize:代表块大小。表示一个像素在计算其阈值时所使用的领域尺寸,通常为3、5、7等。
  • C:是常量
import cv2

img = cv2.imread('data/car.jpg',0)
t1,thd = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
athdMEAN = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 3)
athdGAUS = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 3)
get_show(img, thd, athdMEAN, athdGAUS)


6output_13_0

Otsu处理

  • 有时候图像灰度级的分布是不均衡的,如果此时还将阈值设置为127,那么阈值处理的结果就是失败的。
  • Otsu方法能够根据当前图像给出最佳的类间分割阈值。简而言之,Otsu方法会遍历所有可能阈值,从而找到最佳的阈值。
  • 在opencv中,通过在函数cv2.threshold()中对参数type的类型对传递一个参数,cv2.THRESH_OTSU,即可实现Otsu方式的阈值分割。
  • 使用Otsu方法时,要把阈值设定为0
import cv2

img=cv2.imread('data/angle.jpg', 0)
t1, thd = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
t2, otsu = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print('otsu自适应阈值为:', t2)
get_show(img, thd, otsu)
otsu自适应阈值为: 156.0

6output_15_1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值