Skip to main content

图像二值化

图像二值化1、实现原理2、实现效果3、实现代码

1、实现原理

使用cv2.threshold()函数对图像进行二值化。

2、实现效果

cd ~/opencv xxxxxxxxxx python3 09.image_binarize.py

注意:选中图片按q可以退出程序!

image-20250106161124620

3、实现代码

​ x import cv2 def binarize_image(input_path, output_path, threshold): image = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE) if image is None: print("Error: Unable to open image file.") return _, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY) if cv2.imwrite(output_path, binary_image): print(f"Image saved to {output_path}") cv2.imshow('Image Preview', cv2.imread(output_path)) cv2.waitKey(0) cv2.destroyAllWindows() else: print("Error: Unable to save image file.") binarize_image('/home/jetson/opencv/images/yahboom_logo.png', \ '/home/jetson/opencv/images/yahboom_logo_binarize.png', \ 127)