import cv2import numpy as np #此方法可以将彩色图根据一定的阈值转换为黑白图。其中阈值用于划分图片的黑白(局部方法)def threshold_deom(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray, 127,255,cv2.THRESH_BINARY)#可将图像中的颜色转为0或1,即黑和白。其中127表示阈值,大于127的是白色,小于127的是黑色。当然这个也取决于cv2.THRESH_XX函数 ret, binary = cv2.threshold(gray, 0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)#不自定义,而是使用cv2函数 print("threshold value=",ret) cv2.imshow("binary",binary) #自适应方法,效果更好def local_threshold(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) dst = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,25,10) cv2.imshow("binary",dst)
src = cv2.imread("woman.jpg")cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)cv2.imshow(‘input image‘,src)threshold_deom(src)cv2.waitKey(0)cv2.destroyAllWindows()
原文地址:https://www.cnblogs.com/August2019/p/12589994.html
时间: 2024-11-13 09:25:19