Python-opencv学习 5 阈值分割

阈值分割 - opencv

  • threshold()
  • 阈值方法
  • 阈值方法实例

threshold()

retval,dst=cv2.threshold(src,thresh,maxval,type)

  • retval,阈值
  • dst,处理结果
  • src,原图像
  • thresh,阈值
  • maxval,最大值
  • type,类型

阈值方法

• cv2.THRESH_BINARY
• cv2.THRESH_BINARY_INV
• cv2.THRESH_TRUNC
• cv2.THRESH_TOZERO
• cv2.THRESH_TOZERO_INV
Python-opencv学习 5 阈值分割_第1张图片

阈值方法实例


import cv2
a=cv2.imread("C:\\Users\\Chester\\Downloads\\opencv-python_simpletest\\image\\lena512.bmp",cv2.IMREAD_UNCHANGED)
#注意原始图像的类型,必须是8位单通道图像,彩色图像无意义
r,b1=cv2.threshold(a,127,255,cv2.THRESH_BINARY)
r,b2=cv2.threshold(a,127,255,cv2.THRESH_BINARY_INV)
r,b3=cv2.threshold(a,127,255,cv2.THRESH_TRUNC )
r,b4=cv2.threshold(a,127,255,cv2.THRESH_TOZERO )
r,b5=cv2.threshold(a,127,255,cv2.THRESH_TOZERO_INV)
cv2.imshow("original",a)
cv2.imshow("THRESH_BINARY",b1)
cv2.imshow("THRESH_BINARY_INV",b1)
cv2.imshow("THRESH_TRUNC",b1)
cv2.imshow("THRESH_TOZERO",b1)
cv2.imshow("THRESH_TOZERO_INV",b1)

print(r)
cv2.waitKey()
cv2.destroyAllWindows()



Python-opencv学习 5 阈值分割_第2张图片
Python-opencv学习 5 阈值分割_第3张图片

你可能感兴趣的:(opencv)