Text Region Mask


本系列文章由 @yhl_leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/52886351


Python code : yhlleo/textRegionMask


根据图像中文本字符的坐标信息,生成文本区域mask图像。如下图

文本字符信息记录格式为:

bjtextset01_0004.jpg
1
1 527.50 243.50 581.67 311.00 "2"

其中,bjtextset01_0004.jpg为图像名(全小写字符),紧接着的1为包含文本字符的数量,后面接着就是对应的文本字符的位置坐标527.50 243.50 581.67 311.00(格式为x, y, x, y,即两个顶点坐标),2为字符内容,该行最前面的1为标记符,可以忽略。

首先,读取文本内容:

import os
import copy as cp

class DataGt(object):
    """docstring for DataGt"""
    def __init__(self, fname, trlist):
        super(DataGt, self).__init__()
        self.fname = fname
        self.trlist = trlist


def loaddata(path):
    fp = open(path).read().splitlines()
    gt = DataGt([],[])
    niter = 0
    idx = 0
    while niter < len(fp):
        if '.jpg' in fp[idx]:
            textlst = []
            gt.fname.append(fp[idx]);
            idx = idx + 1
            num = int(fp[idx])
            for i in range(num):
                idx = idx + 1
                if '1' in fp[idx] and '\"' in fp[idx]:
                    loc = fp[idx].split(' ')[1:5]
                    textlst.append(loc)
            gt.trlist.append(textlst)
        else:
            idx = idx + 1
        niter = idx
    return gt

然后,绘制mask图:

import os
import cv2
import loadgt
import numpy as np

def im_lists( path ):
    return os.listdir(path);

def path_insensitive(lst, fn):
    for ln in lst:
        if ln.lower() == fn.lower():
            return ln
    return None

def genMask(gt, im_path, savepath):
    num = len(gt.fname)
    ims = im_lists(im_path)
    for idx in range(num):
        fn = path_insensitive( ims, gt.fname[idx] )
        fname = os.path.join(im_path, fn)
        sname = os.path.join(savepath, fn)
        im = cv2.imread(fname)
        size_im = im.shape
        #print size_im
        mask = np.zeros([size_im[0], size_im[1]], dtype=np.uint8)
        for ls in gt.trlist[idx]:
            mask[int(float(ls[1])):int(float(ls[3])), int(float(ls[0])): int(float(ls[2]))] = 255
        cv2.imwrite(sname, mask, [cv2.cv.CV_IMWRITE_PNG_COMPRESSION, 0])

im_path = "./data"
savepath = "./mask"
gtpath = "./test.txt"

gt = loadgt.loaddata(gtpath)
genMask(gt,im_path, savepath)
结果如图:
Image TextRegionMask

posted on 2016-10-21 19:27  疯子123  阅读(270)  评论(0编辑  收藏  举报

导航