《LearnOpenCV》学习笔记(1)——OpenCV Threshold

OpenCV Threshold


# coding: utf-8

# In[20]:


import cv2


# In[21]:


src = cv2.imread('threshold.png',cv2.IMREAD_GRAYSCALE)


# In[22]:


#set the threshold and maxValue
thresh = 0
maxValue = 255


# In[23]:


#binary threshold example
th_binary_1, dst_binary_1 = cv2.threshold(src,thresh,maxValue,cv2.THRESH_BINARY)
cv2.imwrite('dst_binary_1.png',dst_binary_1)
th_binary_2, dst_binary_2 = cv2.threshold(src,127,maxValue,cv2.THRESH_BINARY)
cv2.imwrite('dst_binary_2.png',dst_binary_2)


# In[24]:


#inverse binary thresholding
#Inverse binary thresholding is just the opposite of binary thresholding. 
#The destination pixel is set to zero if the corresponding source pixel is greater than the threshold, 
#and to maxValue if the source pixel is less than the threshold.
th_binary_inv, dst_binary_inv = cv2.threshold(src,127,200,cv2.THRESH_BINARY)
cv2.imwrite('dst_binary_inv.png',dst_binary_inv)


# In[25]:


#inverse truncate thresholding
#the destination pixel is set to the threshold ( thresh ) if the source pixel value is greater than the threshold. 
#Otherwise it is set to the source pixel value. maxValue is ignored.
th_trunc, dst_trunc = cv2.threshold(src,127,200,cv2.THRESH_TRUNC)
cv2.imwrite('dst_trunc.png',dst_trunc)


# In[26]:


#threshold to zero
th_zero, dst_zero = cv2.threshold(src,127,200,cv2.THRESH_TOZERO)
cv2.imwrite('dst_zero.png',dst_zero)


# In[27]:


#inverted threshold to zero
th_zero_inv, dst_zero_inv = cv2.threshold(src,127,200,cv2.THRESH_TOZERO_INV)
cv2.imwrite('dst_zero_inv.png',dst_zero_inv)

相应的输入和输出

threshold.png

 

dst_binary_1.png

dst_binary_2.png

 

dst_binary_inv.png

dst_trunc.png

 

dst_zero.png

dst_zero_inv.png

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值