Python 图片二值化处理

 1 import cv2
 2 import numpy as np
 3 
 4 
 5 # OpenCV进行二值化图片处理
 6 def open_cv_deal_image():
 7     # 读取图像
 8     image = cv2.imread('image/output.jpg')
 9     # 灰度化
10     gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11     # 二值化
12     _, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
13     # 去噪
14     done_image = cv2.fastNlMeansDenoising(binary_image, None, 10, 7, 21)
15     # 锐化
16     kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
17     share_image = cv2.filter2D(done_image, -1, kernel)
18     # 保存图片
19     cv2.imwrite('image/xxx.jpg', share_image)
20     print("~~~~~~~~~~>>>>>图片处理完成<<<<<~~~~~~~~~~")
21 
22 
23 if __name__ == '__main__':
24     open_cv_deal_image()

 

posted @ 2024-07-26 13:26  逍遥散人95  阅读(60)  评论(0编辑  收藏  举报