Text enhancement using opencv

Text enhancement:

import cv2
import numpy as np

# 读取图像
image = cv2.imread('E:/image.jpg', cv2.IMREAD_GRAYSCALE)

# 二值化图像
_, binary_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# 膨胀操作
kernel = np.ones((3, 3), np.uint8)
dilated_image = cv2.dilate(binary_image, kernel, iterations=1)

# 腐蚀操作
eroded_image = cv2.erode(dilated_image, kernel, iterations=1)

# 显示增强后的图像
cv2.imshow('Enhanced Image', eroded_image)
cv2.waitKey(0)

# 保存增强后的图像
cv2.imwrite('enhanced_image.jpg', eroded_image)

# 关闭窗口
cv2.destroyAllWindows()

1. First, use the cv2.imread() function to read the image file in grayscale mode and store it in the variable image.
2. Then, use the cv2.threshold() function to binarize the image, use Otsu's threshold method to automatically determine the threshold, and store the result in the variable binary_image.
3. Next, create a 3x3 square structural element and use the cv2.dilate() function to dilate the binary image and store the result in the variable dilated_image.
4. Then, use the cv2.erode() function to perform erosion operations on the expanded image and store the result

Guess you like

Origin blog.csdn.net/m0_45447650/article/details/132355356