python进行物体轮廓插值处理

该博客主要探讨了一种使用Python进行图像处理的方法,包括从文本文件读取图像路径,对图像进行缩放,并进行像素级的边界框和像素级标注。它涉及到了ROI(感兴趣区域)处理,阈值处理,以及如何将标注结果保存到文件中。此外,还使用了OpenCV库进行图像读取和颜色空间转换。
摘要由CSDN通过智能技术生成
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
import os
scale = 1#2
roi_start = 56
r_scale = 16
high_h = 1080
high_w = 1920

list_path = "list_camera0701.txt"

def inter_xy(img, xy, lpath):
    k=-1
    new_label = []
    for xxy in xy:
        if len(xxy) > 1:
            x_last = xxy[0][0]
            y_last = xxy[0][1]
            k = k +1
            for i in range(len(xxy) - 1):
                stride =  r_scale if x_last <= xxy[i+1][0] else -r_scale
                xstart =  x_last if x_last % r_scale == 0 else (int(x_last /r_scale)+1)*r_scale
                for xi in range(xstart, int(xxy[i+1][0]), stride):
                    if xxy[i+1][0]-x_last ==0:
                        yi = y_last
                        new_label.append([k,xi,yi])
                    else:
                        yi = (xi - x_last)/float((xxy[i+1][0]-x_last))*(xxy[i+1][1]-y_last)+y_last
                        new_label.append([k,xi,yi])
                        x_last = xxy[i+1][0]
                        y_last = xxy[i+1][1]
            if len(xxy) == 1:
                new_label.append([k,int(xxy[0][0] / r_scale)*r_scale,xxy[0][1]])
    return new_label

def inter_xy_x(img, xy, lpath):
    k=-1
    new_label = []
    for xxy in xy:
        if len(xxy) > 1:
            x_last = xxy[0][0]
            y_last = xxy[0][1]
            k = k +1
            for i in range(len(xxy) - 1):
                if xxy[i][1] < roi_start:
                    continue
                stride =  r_scale if y_last <= xxy[i+1][1] else -r_scale
                ystart =  y_last if (y_last-roi_start) % r_scale == 0 else (int((y_last-roi_start) /r_scale)+1)*r_scale+roi_start
                for yi  in range(ystart, int(xxy[i+1][1]), stride):
                    if xxy[i+1][1]-y_last ==0:
                        xi = x_last
                        new_label.append([k,xi,yi])
                    else:
                        xi = (yi - y_last)/float((xxy[i+1][1]-y_last))*(xxy[i+1][0]-x_last)+x_last
                        new_label.append([k,xi,yi])
                        x_last = xxy[i+1][0]
                        y_last = xxy[i+1][1]
            if len(xxy) == 1:
                new_label.append([k,int(xxy[0][0] / r_scale)*r_scale,xxy[0][1]])
    return new_label


def get_inter_gp(bbox, mask):
    gray = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray,0,255,0)

    width = high_w/scale
    height = high_h/scale

    index_vec=[]
    index_vec_l=[]
    index_vec_r=[]
    index_vec_t=[]
    index_cls= []
    for box in bbox:
        box = box.split(" ")
        for k in range(len(box)):
            box[k] = float(box[k])
        index = [-1 for n in range(int(width))]
        w = int(box[1]*width + box[3]*width*0.5) if int(box[1]*width + box[3]*width*0.5) < width else width -1
        h = int(box[2]*height + box[4]*height*0.5) if int(box[2]*height + box[4]*height*0.5) < height else height-1
        x = int(box[1]*width - box[3]*width*0.5) if int(box[1]*width - box[3]*width*0.5) < width else width -1
        y = int(box[2]*height - box[4]*height*0.5) if int(box[2]*height - box[4]*height*0.5) < height else height-1
        cls = int(box[0])
        index_cls.append(cls)
        #print w,h,x,y
        index = [-1 for n in range(int(width))]
        index_l = [10000 for n in range(int(height))]
        index_r = [-1 for n in range(int(height))]
        index_t = [10000 for n in range(int(width))]
        for i in range(y, h):
            for j in range(x, w):
                #print j*scale,i*scale
                if i*scale < high_h and j*scale < high_w and index[j] < i*float(scale) and binary[i*scale, j*scale] == 255:
                    index[j] = i * float(scale)
        index_vec.append(index)
        for i in range(y, h):
            for j in range(x, w):
                #print j*scale,i*scale
                if i*scale < high_h and j*scale < high_w and index_t[j] > i*float(scale) and binary[i*scale, j*scale] == 255:
                    index_t[j] = i * float(scale)
        index_vec_t.append(index_t)
        for i in range(y, h):
            for j in range(x, w):
                #print j*scale,i*scale
                if i*scale < high_h and j*scale < high_w and index_l[i] > j*float(scale) and binary[i*scale, j*scale] == 255:
                    index_l[i] = j * float(scale)
        index_vec_l.append(index_l)
        for i in range(y, h):
            for j in range(x, w):
                #print j*scale,i*scale
                if i*scale < high_h and j*scale < high_w and index_r[i] < j*float(scale) and binary[i*scale, j*scale] == 255:
                    index_r[i] = j * float(scale)
        index_vec_r.append(index_r)
    return index_vec,index_vec_l,index_vec_r,index_vec_t,index_cls
if __name__ == '__main__':
    lines = [line.strip() for line in open(list_path, 'r').readlines()]
    i_num = 0
    for line in lines:
        img = cv2.imread(line)
        maskpath = line.replace("input", "gt").rsplit(".jpg",1)[0]+'.png'
        boxpath = line.replace("input_gen_cls_v2", "bbox_cls_v2").rsplit(".jpg",1)[0]+'.txt'
        if not os.path.exists(boxpath):
            continue
        bbox = [bline.strip() for bline in open(boxpath, 'r').readlines()]
        if len(bbox) <= 0:
            continue
        mask = cv2.imread(maskpath)
        index, index_l,index_r,index_t,index_cls = get_inter_gp(bbox, mask)

        labelpath = line.replace("input_gen_cls_v2", "labels_cls_v2").rsplit(".jpg",1)[0]+'.txt'
        labeldir = labelpath.rsplit("/", 1)[0]
        print line
        if not os.path.exists(labeldir):
            os.makedirs(labeldir)
        xy = [[] for ii in range(len(index))]
        xy_l = [[] for ii in range(len(index_l))]
        xy_r = [[] for ii in range(len(index_r))]
        xy_t = [[] for ii in range(len(index_t))]
        kki = 0
        for  ind in index:
            for k, idx in enumerate(ind):
                if idx < 0:
                    continue
                xy[kki].append([int(k*scale), int(ind[k])])
            kki = kki + 1
        kki = 0
        for ind in index_t:
            for k, idx in enumerate(ind):
                if idx > high_w:
                    continue
                xy_t[kki].append([int(k*scale), int(ind[k])])
            kki = kki + 1
        kki = 0
        for ind in index_l:
            for k, idx in enumerate(ind):
                if idx > high_w:
                    continue
                xy_l[kki].append([int(ind[k]), int(k*scale)])
            kki = kki + 1
        kki = 0
        for ind in index_r:
            for k, idx in enumerate(ind):
                if idx < 0:
                    continue
                xy_r[kki].append([int(ind[k]), int(k*scale)])
            kki = kki + 1
        label_b = inter_xy(img, xy, labelpath)
        label_t = inter_xy(img, xy_t, labelpath)
        label_l = inter_xy_x(img, xy_l, labelpath)
        label_r = inter_xy_x(img, xy_r, labelpath)


        files = labelpath.replace("labels_cls_v2", "combinelabels_cls_v2")
        out_dir = files.rsplit("/", 1)[0]
        print out_dir
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        with open(files,'w+') as f:
            for ll in label_t:
                f.write(str(ll[0])+' '+str(0)+' '+str(ll[1])+' '+str(ll[2])+ ' '+str(index_cls[ll[0]])+'\n')
            #    cv2.circle(img, (int(ll[1]), int(ll[2])), 2, (0, 255, 255), 2)
            for ll in label_b:
                f.write(str(ll[0])+' '+str(1)+' '+str(ll[1])+' '+str(ll[2])+' '+str(index_cls[ll[0]])+'\n')
           #     cv2.circle(img, (int(ll[1]), int(ll[2])), 2, (0, 0, 255), 2)
            for ll in label_l:
                f.write(str(ll[0])+' '+str(2)+' '+str(ll[1])+' '+str(ll[2])+' '+str(index_cls[ll[0]])+'\n')
          #      cv2.circle(img, (int(ll[1]), int(ll[2])), 2, (0, 255, 0), 2)
            for ll in label_r:
                f.write(str(ll[0])+' '+str(3)+' '+str(ll[1])+' '+str(ll[2])+' '+str(index_cls[ll[0]])+'\n')
         #       cv2.circle(img, (int(ll[1]), int(ll[2])), 2, (255, 0, 0), 2)
            f.close()
        #i_num = i_num +1
        #cv2.imwrite("image/"+str(i_num)+".jpg", img)
#        img = cv2.resize(img, (1920/2, 1080/2))
#        cv2.imshow("resxx", img)
#        k = cv2.waitKey(0)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值