传统文本检测并合并三通道

传统文本检测并合并三通道

# -*- coding: utf-8 -*-


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边缘检测生成二值图
    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)#对框内部填充

        #cv2.drawContours(img, [box], 0, (0, 255, 0), 2)

    #合并三通道
    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)
    
    #cv2.imwrite('./results_imgs/{}.png',img)
    

for imagedir in glob.glob(r'/home/jun/opencv/VOCdevkit/VOC2007/JPEGImages/*.png'):
    text_connect(imagedir,r'/home/jun/opencv/img/outdir')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小俊俊的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值