在使用Python+opencv实现图片文字的分割之前,首先要导入必要的库。通常需要使用的库包括cv2
、numpy
、PIL
和matplotlib
,其中cv2
为opencv对Python的接口。
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
使用opencv读取图片,将其转为灰度图像。
img = cv2.imread('test.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
为了能够更好的从图像中分离文字,通常需要二值化图像。可以使用大津算法(Otsu)来自动找到最佳的阈值。
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
使用形态学操作来进行图像处理,进一步分离文字。通常使用开运算和闭运算。
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
查找轮廓,并根据轮廓信息,剪切分离出来的文字。
contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
x, y, w, h = cv2.boundingRect(contour)
if w < 10 or h < 10:
continue
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
crop_img = img[y:y+h, x:x+w]
cv2.imwrite('crop' + str(i) + '.png', crop_img)
通过以上的步骤,就可以实现将图片中的文字分割出来,并保存到单独的图像文件中。
示例1:
图像地址:https://cdn.pixabay.com/photo/2018/01/12/10/19/ford-3089868_960_720.jpg
img = cv2.imread('ford-3089868_960_720.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
x, y, w, h = cv2.boundingRect(contour)
if w < 10 or h < 10:
continue
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
crop_img = img[y:y+h, x:x+w]
cv2.imwrite('crop' + str(i) + '.png', crop_img)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
示例2:
图像地址:https://cdn.pixabay.com/photo/2015/02/18/12/38/wood-640828_960_720.jpg
img = cv2.imread('wood-640828_960_720.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
contours, hierarchy = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
x, y, w, h = cv2.boundingRect(contour)
if w < 10 or h < 10:
continue
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
crop_img = img[y:y+h, x:x+w]
cv2.imwrite('crop' + str(i) + '.png', crop_img)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
通过以上两个示例,可以看到通过Python+opencv
实现图片文字的分割,可以在图片中准确的分离出每个字,并保存为单独的图像文件。