码迷,mamicode.com
首页 > 其他好文 > 详细

图像阈值化处理

时间:2018-07-21 21:21:37      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:lan   dir   range   user   rac   file   route   UNC   use   

#Author C
import cv2
import os
#腐蚀
# kernel=np.ones((5,5),np.uint8)
# erosion=cv2.erode(img,kernel,iterations=1)
list=[]
for filename in os.listdir(r"C:\Users\Uaena\Desktop\crack"):
route=r"C:\Users\Uaena\Desktop\crack\\"+filename
list.append(route)
# print(list)
for i in range(len(list)):
img=cv2.imread(list[i])

img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#转换为灰度图像
blurred=cv2.GaussianBlur(img,(5,5),0)#高斯滤波 https://baike.so.com/doc/1781738-1884180.html
# print(blurred)

(T,thresh)=cv2.threshold(blurred,11,100,cv2.THRESH_BINARY )#二值化处理,阈值为11,低于阈值的像素点灰度值置为0;高于阈值的值置为参数3
#(T, thresh)=cv2.threshold(blurred,11,100,cv2.THRESH_TRUNC)#小于阈值的像素点灰度值不变,大于阈值的像素点置为0
(T, threshInv) = cv2.threshold(blurred, 10, 100, cv2.THRESH_BINARY_INV)#反阈值化,大于阈值的像素点灰度值置为0,小于阈值的值置为参数3
    #小于阈值的像素点灰度值不变,大于阈值的像素点置为0,其中参数3任取
    ret,thresh4 = cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO)
    cv2.imshow(‘BINARY_TOZERO‘,thresh4)


    #大于阈值的像素点灰度值不变,小于阈值的像素点置为0,其中参数3任取
    ret,thresh5 = cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO_INV)
    cv2.imshow(‘BINARY_TOZERO_INV‘,thresh5)

cv2.imwrite(r"C:\Users\Uaena\Desktop\output\%s.jpg"%(i),threshInv)
# cv2.imshow("3",threshInv)

图像阈值化处理

标签:lan   dir   range   user   rac   file   route   UNC   use   

原文地址:https://www.cnblogs.com/mrc-369-com/p/9347884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!