小图贴大图(检测的数据增强)

参考:https://blog.csdn.net/u011321546/article/details/79565355

1.单张图测试:

# -*- coding=GBK -*-
'''
empty classroom pictures
jushou imgs:

'''

import cv2
import numpy as np
from matplotlib import pyplot as plt

#截取图片中的指定区域或在指定区域添加某一图片
def jie_image(src1):
    src2 = src1[5:89, 500:630]#截取第5行到89行的第500列到630列的区域

    cv2.imshow("截取", src2)
    src1[105:189, 300:430] = src2#指定位置填充,大小要一样才能填充
    cv2.imshow("合成", src1)

def tietu():
    empty = cv2.imread("/data/JUSHOU/align/real_empty_class/imgs/20180509_r_00767.jpg")
    # cv.imshow("empty", empty)
    # img_rgb = cv2.cvtColor(empty, cv2.COLOR_BGR2RGB)
    w, h, c = empty.shape


    handsup = cv2.imread("/data/JUSHOU/align/crop_jushou_generate/yiyang2-image-00170-img_000005.jpg")
    w1,h1,c1=handsup.shape
    k=int(w1/12)
    handsup2=handsup[k:w1-k,k:h1-k,:]
    w2, h2, c2 = handsup2.shape

    x_s=100
    y_e=820
    print(empty.shape)
    empty[y_e-w2:y_e,x_s:x_s+h2]=handsup2
    # empty = cv2.cvtColor(empty, cv2.COLOR_BGR2RGB)
    plt.imshow(empty)
    # plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis

    cv2.imwrite('./result_imgs/1.jpg', empty)
    plt.show()



# tietu()

2.大批量生成(每张大图贴6张小图,生成1000张结束),并生成每张图的检测groudtruth:

import os
import json
def Image_synthesis():
    output = open('./jushou.json', 'w')

    empty_imgs_dir='/data/JUSHOU/align/real_empty_class/imgs'
    jushou_imgs_dir='/data/JUSHOU/align/crop_jushou_generate'
    empty_img_list=open('./emty_imgs_list.txt','w')
    empty_list=[]
    for path,dir,filenames in os.walk(empty_imgs_dir):
        for filename in filenames:
            empty_path=os.path.join(path,filename)
            empty_img_list.write(empty_path+'\n')
            empty_list.append(empty_path)

    jushou_img_list = open('./jushou_imgs_list.txt', 'w')
    jushou_list=[]
    for path, dir, filenames in os.walk(jushou_imgs_dir):
        for filename in filenames:
            jushou_path = os.path.join(path, filename)
            jushou_img_list.write(jushou_path + '\n')
            jushou_list.append(jushou_path)

    it=iter(empty_list)
    sub_img=iter(jushou_list)

    count=0

    for i in range(1000):
        img_gt=[]
        try:
            count+=1
            print(count)
            # if count>20:
            #     break
            # print(next(it))
            name1=next(it)
            print(name1)
            empty = cv2.imread(name1)

            h,w,c=empty.shape
            XS = [0+100,int(w/3+100),int(w*2/3)+100,0+100,int(w/3)+100,int(w*2/3)+100]
            YE = [int(h/2),int(h/2),int(h/2),h-100,h-100,h-100]
            for j in range(6):
                # print('#' * 30)
                try:
                    name2 = next(sub_img)
                    print(name2)
                    handsup = cv2.imread(name2)

                except StopIteration:
                    sub_img = iter(jushou_list)
                    handsup = cv2.imread(next(sub_img))
                w1, h1, c1 = handsup.shape
                if w > 2000:
                    handsup = cv2.resize(handsup, (w1 * 2, h1 * 2), interpolation=cv2.INTER_CUBIC)

                w1, h1, c1 = handsup.shape
                if h1>400:
                    handsup= cv2.resize(handsup, (int(w1/2), int(h1/2)), interpolation=cv2.INTER_CUBIC)

                if h1<160:
                    handsup = cv2.resize(handsup, (w1 *2 , h1 * 2), interpolation=cv2.INTER_CUBIC)
                    if w>2000:
                        handsup = cv2.resize(handsup, (w1 * 3, h1 * 3), interpolation=cv2.INTER_CUBIC)

                w1, h1, c1 = handsup.shape
                k = int(w1 / 12)
                handsup2 = handsup[k:w1 - k, k:h1 - k, :]
                # print(handsup2.shape)
                w2, h2, c2 = handsup2.shape

                x_s = XS[j]
                y_e = YE[j]



                y1,y2,x1,x2=(y_e - w2,y_e, x_s,x_s + h2)
                # print(handsup2.shape)
                # print((y_e - w2, y_e, x_s, x_s + h2))
                empty[y_e - w2:y_e, x_s:x_s + h2] = handsup2

                img_gt.append(
                    [x1,
                     y1,
                     x2,
                     y2])
                instances = []
                for gt in img_gt:
                    bbox = gt[:4]
                    cls = 1
                    instances.append({
                        'is_ignored': False,
                        'bbox': bbox,
                        'label': cls})
                data = {
                    'filename': 'result_imgs/%d.jpg'%i,
                    'image_height': h,
                    'image_width': w,
                    'instances': instances
                }

            if len(instances) == 0:
                count -= 1
                continue
            output.write(json.dumps(data, ensure_ascii=False) + '\n')

            cv2.imwrite('./result_imgs/%d.jpg'%i, empty)
            # plt.imshow(empty)
            # plt.show()

        except StopIteration:
            it = iter(empty_list)
            continue


Image_synthesis()

猜你喜欢

转载自blog.csdn.net/qq_33547191/article/details/90745368
今日推荐