culane dataset processing

culane dataset (IMG:image(size(590*1640)), GT:line_txt)

Purpose: To process into a uniform size, which is convenient for model train.

(resize to 800*320 after clipping based on the vanishing point of the lane line, GT format includes mask, line_txt in point format)

import os
import cv2
import numpy as np
path="/culane"
file_path="/culane/list/train_gt.txt"
img_path="/merge/culane/image"
mask_path="/merge/culane/mask"
txt_path="/merge/culane/line_txt"
n_0 =0
with open(file_path,'r') as f:
    for line in f:
        ori_image = cv2.imread(path + line.split()[0])
        h = ori_image.shape[0]
        w = ori_image.shape[1]
        fileHandler_txt = path + line.split()[0][:-4] + ".lines.txt"
        fileHandler = open(fileHandler_txt, "r")
        listOfLines = fileHandler.readlines()
        fileHandler.close()
        if len(listOfLines)==0:
            continue
        jamp = []
        for line_yuan in listOfLines:
            dataline = line_yuan.strip().split(" ")
            jamp0 = []
            for n in range(0, int(len(dataline) / 2)):
                jamp1 = []
                jamp1.append(float(dataline[n * 2]))
                jamp1.append(float(dataline[n * 2 + 1]))
                jamp0.append(jamp1)
            jamp.append(jamp0)
        y = []
        for e in jamp:
            for coord in e:
                y.append(coord[1])
        crop_y = min(y)
        print(crop_y)
        line_ = []
        for e in jamp:
            lines_ = []
            for coord in e:
                lines_.append(str(round(coord[0] / w * 800, 2)))
                lines_.append(str(round((coord[1] - crop_y) / (h - crop_y) * 320, 2)))
            line_.append(lines_)
        image_name_old = line.split()[0].split('/')[-1]
        mask_name_old = line.split()[1].split('/')[-1]
        print(image_name_old)
        mask =      cv2.imread(path+line.split()[1])
        Num= np.amax(mask[:,:,0],axis=0)
        dice=np.argmax(mask[:,:,0],axis=0)
        used_dice=[]
        for i in range(0,len(dice)):
            if dice[i]!=0:
                used_dice.append(dice[i])
        if len(used_dice)==0:
            used_dice.append(0)
        crop_y=np.amin(used_dice)
        img = np.zeros((mask.shape[0]-crop_y, mask.shape[1], 3), np.uint8)
        img[:mask.shape[0]-crop_y, :, :] = ori_image[crop_y:, ...]
        mas = np.zeros((mask.shape[0] - crop_y, mask.shape[1], 3), np.uint8)
        mas[:mask.shape[0] - crop_y, :, :] = mask[crop_y:, ...]
        img_new = cv2.resize(img, (800, 320), interpolation=1)
        mas_new = cv2.resize(mas, (800, 320), interpolation=1)
        mas_new =np.where(mas_new>0 ,255,0)
        len_num = len(str(n_0))
        split_0 = 8 - len_num
        Public_name = "0000000000000000000000000000000000000000000000000000000000000000"
        split_name = Public_name[0:split_0]
        image_name = 'culane' + "_" + split_name + str(n_0)+".png"
        mask_name = 'culane' + "_" + split_name + str(n_0)+".png"
        file_name='culane' + "_" + split_name + str(n_0)
        with open(os.path.join(txt_path, file_name + ".lines.txt"), "w", encoding="utf-8") as linetxt:
            linetxt.write('\n'.join([' '.join(i) for i in line_]))
        linetxt.close()
        cv2.imwrite(os.path.join(img_path, image_name), img_new)
        cv2.imwrite(os.path.join(mask_path, mask_name), mas_new)
        n_0 = n_0 + 1





Guess you like

Origin blog.csdn.net/weixin_42234696/article/details/127801941