传统文本检测并合并三通道
import cv2
import numpy as np
import glob
import os.path
def text_connect(imagedir,outdir):
img = cv2.imread(imagedir)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 9))
element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (24, 6))
dilation = cv2.dilate(binary, element2, iterations=1)
erosion = cv2.erode(dilation, element1, iterations=1)
dilation2 = cv2.dilate(erosion, element2, iterations=2)
region = []
contours, hierarchy = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for i in range(len(contours)):
cnt = contours[i]
area = cv2.contourArea(cnt)
if (area < 1000):
continue
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
if (height > width * 1.3):
continue
if (height>150 or height<30):
continue
region.append(box)
'''
for obj in np.array(region):
t1 = obj.flatten()#降维
t2 = [str(x) for x in t1]#转为字符串格式
t3 = ','.join(t2)
name = os.path.join(outdir,os.path.basename(imagedir))
f = open('name.txt', "a", encoding='utf-8')
f.writelines([t3,',','word','\n'])
f.close()
'''
w = img.shape[0]
h = img.shape[1]
img_one = np.zeros((w,h),dtype=np.uint8)
for box in region:
x1=int(box[1][0])
y1=int(box[1][1])
x2=int(box[3][0])
y2=int(box[3][1])
cv2.rectangle(img_one,(x1,y1),(x2,y2),255,thickness=-1)
B = img[:, :, 0]
G = img[:, :, 1]
R = img_one
img_one = cv2.merge([B, G, R])
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(os.path.join(outdir,os.path.basename(imagedir)), img_one)
for imagedir in glob.glob(r'/home/jun/opencv/VOCdevkit/VOC2007/JPEGImages/*.png'):
text_connect(imagedir,r'/home/jun/opencv/img/outdir')