How to use Python to binarize images
Abstract:
In digital image processing, binarization is a very common processing method , which converts a color or grayscale image into an image with only two values, typically black and white. Binarizing the image can highlight the contours and features of the image and facilitate subsequent image analysis operations. This article will introduce how to use Python to binarize images and provide code examples to help readers better understand.
1. Introduce necessary libraries
Before we begin, we need to introduce some necessary libraries. In Python, there are many image processing libraries to choose from, such as PIL (Python Imaging Library) and OpenCV. This article will use the PIL library for binary processing of images, so you need to install and introduce the PIL library first.
Code example:
from PIL import Image
2. Read and display the original image
Before binary processing, you first need to read the original image and display it so that the processed image can be processed The results are more intuitively understood.
Code example:
# 读取原始图片 image = Image.open('original_image.jpg') # 显示原始图片 image.show()
3. Image binarization processing
Next, we need to binarize the image. In the PIL library, the image object is a matrix composed of pixel values, and the value of each pixel represents the color information corresponding to that point. To convert an image into a binary image, pixel values need to be distinguished according to a certain threshold. Pixels greater than the threshold are set to white, and pixels less than the threshold are set to black.
Code example:
# 设置二值化阈值 threshold = 128 # 获取图片的宽度和高度 width, height = image.size # 创建一个新的图片对象,用来存储二值化处理后的结果 binary_image = Image.new('1', (width, height)) # 遍历原始图片的每个像素点 for y in range(height): for x in range(width): # 获取当前像素点的像素值 pixel = image.getpixel((x, y)) # 判断像素值是否大于阈值,如果大于则设置为白色,否则设置为黑色 if pixel >= threshold: binary_image.putpixel((x, y), 255) else: binary_image.putpixel((x, y), 0) # 显示二值化处理后的结果 binary_image.show()
4. Save and display the binarized image
Finally, we save the processed binarized image locally and display it.
Code example:
# 保存二值化图片到本地 binary_image.save('binary_image.jpg') # 显示二值化图片 binary_image.show()
Summary:
This article introduces how to use Python to binarize images. By reading the original image, setting the binarization threshold, traversing each pixel, and setting the color of the pixel according to the size of the pixel value, the effect of converting a color or grayscale image into a binary image can be achieved. This binary processing method can be used in many image processing application scenarios such as contour detection, image segmentation, and character recognition. I hope the sample code in this article can help readers better understand and master the method of image binarization processing.
The above is the detailed content of How to binarize images using Python. For more information, please follow other related articles on the PHP Chinese website!