Python :将分割后的图片拼接回原样,通过csv文件对图片进行画框以及生成新的csv文件。

joint_pic.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from PIL import Image
import os
imgname = 0
def pingjie(imgs,weigh_size,high_size):
    print('------------pingjie-------------')
    global imgname
    target = Image.new('RGB', (weigh_size * 4, high_size * 1))  # 拼接前需要写拼接完成后的图片大小 1200*600
    for i in range(len(imgs)):
        a = weigh_size * i  # 图片距离左边的大小
        b = 0  # 图片距离上边的大小
        c = weigh_size * (i + 1)  # 图片距离左边的大小 + 图片自身宽度
        d = high_size  # 图片距离上边的大小 + 图片自身高度
        target.paste(imgs[i], (a, b, c, d))

        print('拼接图片的路径为:', path1 + str(imgname) + '.jpg')
    target.save(path1 + "/" + str(imgname) + '.jpg')
    imgname += 1
def pj():
    print('------------pj-------------')
    # 取1,3是因为每行拼接完整都是最后那个,第一行是0,1命名,第二行是2,3命名,所以取后面那个值
    imglist = [0,1,2,3]
    img = []
    for i in imglist:
        print('完整行的拼接路径为:' + path1 + "/" + str(i) + '.jpg')
        img.append(Image.open(path1 + "/" + str(i) + '.jpg'))
    target = Image.new('RGB', (weigh_size * 4, high_size * 4))  # 拼接前需要写拼接完成后的图片大小 1200*1200
    for i in range(len(img)):
        a = 0  # 图片距离左边的大小
        b = high_size * i  # 图片距离上边的大小
        c = weigh_size * 4  # 图片距离左边的大小 + 图片自身宽度
        d = high_size * (i + 1)  # 图片距离上边的大小 + 图片自身高度
        target.paste(img[i], (a, b, c, d))
        global imgname
        target.save(path1 + "/" + 'pingjie' + '.jpg')
if __name__ == '__main__':
    weigh_size = 1840  # 图片的宽高都为600像素
    high_size = 1228
    filepath = './ceshi'  # 存放要拼接图片的目录
    path1 = './INTER_nearest'  # 拼接后图片的存放目录
    img_list = os.listdir(filepath)
    print(img_list)
    for i in range(4):  # 有两行,所以需要循环两次
        images = []       # 每一次拼接只能一行一行拼接,不能在第一行拼接完后再在其基础上拼接第二行的图片,矩阵不允许这样操作
        for j in range(4):  # 每行有两张图片,所以也要循环两次
            a = i*4 + j
            images.append(Image.open(filepath + "/" + img_list[a]))
        print('第 {} 行拼接完成'.format(i))
        pingjie(images, weigh_size,high_size)
    pj()

 

 joint_pic.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from PIL import Image
import os
imgname = 0
def pingjie(imgs,weigh_size,high_size):
    print('------------pingjie-------------')
    global imgname
    target = Image.new('RGB', (weigh_size * 4, high_size * 1))  # 拼接前需要写拼接完成后的图片大小 1200*600
    for i in range(len(imgs)):
        a = weigh_size * i  # 图片距离左边的大小
        b = 0  # 图片距离上边的大小
        c = weigh_size * (i + 1)  # 图片距离左边的大小 + 图片自身宽度
        d = high_size  # 图片距离上边的大小 + 图片自身高度
        target.paste(imgs[i], (a, b, c, d))
        print('拼接图片的路径为:', path1 + str(imgname) + '.jpg')
    target.save(path1 + "/" + str(imgname) + '.jpg')
    imgname += 1
def pj(k):
    print('------------pj-------------')
    # 取1,3是因为每行拼接完整都是最后那个,第一行是0,1命名,第二行是2,3命名,所以取后面那个值
    imglist = os.listdir(path1)
    #imglist = [0,1,2,3]
    print(imglist)
    j = int(4*k)
    img = []
    for i in range(4):
        print('完整行的拼接路径为:%s' % (path1 + "/" + imglist[i+j] + '.jpg'))
        img.append(Image.open(path1 + "/" + str(i+j) + '.jpg'))
    target = Image.new('RGB', (weigh_size * 4, high_size * 4))  # 拼接前需要写拼接完成后的图片大小 1200*1200
    for i in range(4):
        a = 0  # 图片距离左边的大小
        b = high_size * i  # 图片距离上边的大小
        c = weigh_size * 4  # 图片距离左边的大小 + 图片自身宽度
        d = high_size * (i + 1)  # 图片距离上边的大小 + 图片自身高度
        target.paste(img[i], (a, b, c, d))
        global imgname
        with open('somefile.txt', 'r') as f:
            content = f.read().splitlines()
        target.save(path2 + "/" + content[k] + '.jpg')
def cut_image(image):
    width, height = image.size
    item_width = int(width / 4)
    item_height = int(height / 4)
    box_list = []
    # (left, upper, right, lower)
    for i in range(0,4):
     for j in range(0,4):
         #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
         box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)
         box_list.append(box)
    image_list = [image.crop(box) for box in box_list]
    return image_list
#保存
def save_images(image_list,pic_file):
    index = 1
    with open('somefile.txt', 'r') as f:
        content = f.read().splitlines()
    for image in image_list:
        image.save("./ceshi/" + pic_file + "_" + content[index] + ".jpg")
        index += 1
if __name__ == '__main__':
    org_img_path = "./INTER_cubic"
    # filepath = r"./DSC00001.jpg_2.jpg"
    for pic_file in os.listdir(org_img_path):
        print(pic_file)
        file_path = os.path.join(org_img_path, pic_file)
        image = Image.open(file_path)
        image_list = cut_image(image)
        save_images(image_list, pic_file)
    print("图片分割完毕")
    weigh_size = 1840  # 图片的宽高都为600像素
    high_size = 1228
    after_spli_img_path = './ceshi'  # 存放要拼接图片的目录
    path1 = './INTER_nearest'  # 拼接后图片的存放目录
    path2 = "./INTER_LANCZOS4"
    img_list = os.listdir(after_spli_img_path)
    for k in range(int(len(img_list)/16)):
        print(img_list)
        for i in range(4):  # 有两行,所以需要循环两次
            images = []       # 每一次拼接只能一行一行拼接,不能在第一行拼接完后再在其基础上拼接第二行的图片,矩阵不允许这样操作
            for j in range(4):  # 每行有两张图片,所以也要循环两次
                a = k*16 + i*4 + j
                images.append(Image.open(after_spli_img_path + "/" + img_list[a]))
            print('第 {} 行拼接完成'.format(i))
            pingjie(images, weigh_size,high_size)
        pj(k)

 

 通过csv文件对图片画框。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import os
from PIL import Image
import time
def bouding_box(lines,input_pic,font,m,n,out_csv_path):
   for pic_file in os.listdir(input_pic):
      file_path = os.path.join(input_pic,pic_file)
      image = Image.open(file_path)
      img = cv2.imread(file_path)
      width, height = image.size  #获取测试图片的宽和高
      weight_avg = int(width / m) #获取原始图片分割后每张小图的宽
      heigh_avg = int(height / n)  #获取原始图片分割后每张小图的高
      for i , name in enumerate(lines):
         #从csv文件每一行的第一列里获取原始图片的名字以及分割后生成的每一张小图片名字的后半部分
         #具体可参照csv文件里图片的命名方式
         img_name = name.split(",")[0].split("/")[1].split("_")[1].split(".")[0]  # 分割后小图片名字的后半部分
         org_img_name = name.split(",")[0].split("/")[1].split("_")[0] + ".jpg"  # 测试图片的名字
         label_name = name.split(",")[5]  # 获取对应方框的类别名
         score = name.split(",")[6] #获取每一个类别的得分值
         score = float(score)
         text = label_name + ":" + str(round(score,2))  #图片上每个框的类别及得分值

         #当类别名为crack时
         if label_name == "crack":
            if org_img_name == pic_file:
               if img_name == "DS01":  #原始图片分割出来的第一张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin+10, new_ymin-10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS02":  #原始图片分割出来的第二张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3])+ int(weight_avg)
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS03":  #原始图片分割出来的第三张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*2
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*2
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS04":  #原始图片分割出来的第四张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*3
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*3
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS05":  #原始图片分割出来的第五张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS06":  #原始图片分割出来的第六张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS07":  #原始图片分割出来的第七张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS08":   #原始图片分割出来的第八张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS09":  #原始图片分割出来的第九张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)*2
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)*2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS10":  #原始图片分割出来的第十张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS11":  #原始图片分割出来的第十一张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS12":  #原始图片分割出来的第十二张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS13":  #原始图片分割出来的第十三张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS14":  #原始图片分割出来的第十四张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS15":  #原始图片分割出来的第十五张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS16":  #原始图片分割出来的第十六张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)*3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)*3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (0, 255, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (0, 255, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
         #当类别名为line时
         else:
            if org_img_name == pic_file:
               if img_name == "DS01":     #原始图片分割出来的第一张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255,0 , 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS02":  #原始图片分割出来的第二张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS03":  #原始图片分割出来的第三张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS04":  #原始图片分割出来的第四张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4])
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS05":  #原始图片分割出来的第五张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS06":  #原始图片分割出来的第六张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS07":  #原始图片分割出来的第七张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS08":  #原始图片分割出来的第八张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS09":  #原始图片分割出来的第九张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS10":  #原始图片分割出来的第十张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS11":   #原始图片分割出来的第十一张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS12":  #原始图片分割出来的第十二张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS13":  #原始图片分割出来的第十三张小图片
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS14":  #原始图片分割出来的第十四张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS15": #原始图片分割出来的第五张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
               elif img_name == "DS16": #原始图片分割出来的第十六张小图片
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  cv2.rectangle(img, (new_xmin, new_ymin), (new_xmax, new_ymax), (255, 0, 0), 4)
                  cv2.putText(img, text, (new_xmin + 10, new_ymin - 10), font, 2, (255, 0, 0), 1)
                  WriteinCSV(out_csv_path, pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
      #cv2.imwrite("/home/jhy/wave_test/ultimate_img/" + pic_file, img)
      cv2.imwrite("./out_result/" + pic_file, img)
   end_time = time.time()
   print "一共耗时:%ds" % (end_time-start_time)

#写入新的csv文件中
def WriteinCSV(OutputCSVFileName,image_name,xmin,ymin,xmax,ymax,label_name,score):
    WriteCSVFile = open(OutputCSVFileName, 'a')
    one_line_data = [image_name,xmin,ymin,xmax,ymax,label_name,score]
    WriteCSVFile.write(",".join(one_line_data) + "\n")
    WriteCSVFile.close()
if __name__ == "__main__":
   start_time = time.time()
   m = 4
   n = 4
   #with open('/home/jhy/wave_test/reult_forktest_images/result.csv', 'r') as p:
   with open('./result.csv', 'r') as p:
      lines = p.read().splitlines()  #读出csv文件的每一行数据
   #input_pic = "/home/jhy/caffe-ssd/examples/images/ssd_test_image"
   input_pic = "./INTER_cubic"
   out_csv_path = "./out_result/new_result.csv"
   font = cv2.FONT_HERSHEY_DUPLEX  #画框标注时的字体
   bouding_box(lines,input_pic,font,m,n,out_csv_path)

 

 

 单独处理csv文件,把分割后的图片生成的框的坐标还原到原始图片上

Resize_csv.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cv2
import os
from PIL import Image


def WriteinCSV(OutputCSVFileName,image_name,xmin,ymin,xmax,ymax,label_name,score):
    WriteCSVFile = open(OutputCSVFileName, 'a')
    one_line_data = [image_name,xmin,ymin,xmax,ymax,label_name,score]
    WriteCSVFile.write(",".join(one_line_data) + "\n")
    WriteCSVFile.close()

def Resize_csv(lines,image,pic_file,m,n):

      width, height = image.size
      weight_avg = int(width / m)
      heigh_avg = int(height / n)
      for i , name in enumerate(lines):

          img_name = name.split(",")[0].split("/")[1].split("_")[1].split(".")[0]
          org_img_name = name.split(",")[0].split("/")[1].split("_")[0] + ".jpg"
          label_name = name.split(",")[5]
          score = name.split(",")[6]
          #score = float(score)
          if org_img_name == pic_file:
              if img_name == "DS01":
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4])
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax), label_name, str(score))
              elif img_name == "DS02":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4])
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS03":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4])
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS04":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2])
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4])
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS05":
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS06":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS07":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS08":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg)
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg)
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS09":
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS10":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS11":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS12":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 2
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 2
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS13":
                  new_xmin = int(name.split(",")[1])
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3])
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS14":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg)
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg)
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS15":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 2
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 2
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
              elif img_name == "DS16":
                  new_xmin = int(name.split(",")[1]) + int(weight_avg) * 3
                  new_ymin = int(name.split(",")[2]) + int(heigh_avg) * 3
                  new_xmax = int(name.split(",")[3]) + int(weight_avg) * 3
                  new_ymax = int(name.split(",")[4]) + int(heigh_avg) * 3
                  WriteinCSV("new_csv", pic_file, str(new_xmin), str(new_ymin), str(new_xmax), str(new_ymax),
                             label_name, str(score))
if __name__ == "__main__":
    m = 4
    n = 4
    with open('./result.csv', 'r') as p:
    #with open('./result.csv', 'r') as p:
      lines = p.read().splitlines()
    #input_pic = "/home/jhy/caffe-ssd/examples/images/ssd_test_image"
    input_pic = "./INTER_cubic"
    #font = cv2.FONT_HERSHEY_DUPLEX
    for pic_file in os.listdir(input_pic):
        file_path = os.path.join(input_pic, pic_file)
        image = Image.open(file_path)
        #img = cv2.imread(file_path)
        Resize_csv(lines,image,pic_file,m,n)

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值