泊松融合实现无缝融合贴图数据增强

import cv2
import numpy as np
import os
import json
count = 0

def seamless(src,dst,P,center):
    src_mask = np.zeros(src.shape, src.dtype)
    poly = np.array(P, np.int32)
    cv2.fillPoly(src_mask, [poly], (255, 255, 255))
    output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
    return output

backpaths = [os.path.join('background_out/{}'.format(file)) for file in os.listdir('background_out')]
pays = [file for file in os.listdir('/home/lixuan/data/466yuanshi/im')]
fww = open('log.txt','w')
count = 0
for bkpath in backpaths:
    for dir in os.listdir('/home/lixuan/sku_data/sku110k/yolov5_sku/annotations'):
        # dir = 'F04247916_1_20210319T114750Z_20210319T115941Z%5B00-07-51%5D%5B20210323-163755784%5D.json'
        datas = json.load(open('/home/lixuan/sku_data/sku110k/yolov5_sku/annotations/{}'.format(dir)))
        imagepath = datas['imagepath']
        if imagepath.split('466yuanshi/imgs/')[-1] in pays:
            originalimg = cv2.imread(imagepath)
            try:
                imageHeight,imageWidth,_ = originalimg.shape
            except:
                print(dir,'*'*30)
                continue
            P = datas['people']
            if datas.get('pay') != None:
                box = datas['pay']
                lab = 'pay'
            else:
                box = datas['scanning']
                lab = 'scan'
                continue
            P = datas['people']
            imgback = cv2.imread(bkpath)
            imgback = cv2.resize(imgback,(imageWidth,imageHeight))

            img = np.zeros((imageHeight, imageWidth, 3), np.uint8)
            area1 = np.array(P)

            cv2.fillPoly(img, [area1], (255, 255, 255))
            maskindex = np.where(img > 0)

            gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            _, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

            __,contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
            s = contours[0].shape[0]
            cont = contours[0]
            for co in contours:
                if co.shape[0] > s:
                    cont = co
                    s = co.shape[0]

            if True:
                # 外接矩形
                x, y, w, h = cv2.boundingRect(cont)
                x1 = x
                y1 = y
                x2 = x1 + w
                y2 = y1 + h
                x = ((box[0][0] + box[1][0])/2)/imageWidth
                y = ((box[0][1]+box[1][1])/2)/imageHeight
                w = box[1][0] - box[0][0]
                h = box[1][1] - box[0][1]
                out = seamless(originalimg, imgback, P,((x1+x2)//2,(y1+y2)//2))
                cv2.imwrite('/home/lixuan/data/466yuanshi/img/pay_{}.jpg'.format(count),out)
                fw = open('/home/lixuan/data/466yuanshi/lab/pay_{}.txt'.format(count),'w')
                fw.write('1 ' + str(x) + ' ' + str(y) + ' ' + str(w/imageWidth) + ' ' + str(h/imageHeight) + '\n')
                fw.close()
                count += 1
                # out = cv2.rectangle(out, (box[0][0], box[0][1]), (box[1][0], box[1][1]), (0, 255, 0), 3)
                # cv2.namedWindow('img',0)
                # cv2.imshow('img',out)
                # cv2.waitKey(0)
import cv2
import numpy as np
import os

count = 0

backpaths = [os.path.join(r'D:\dataset\backgroud\{}'.format(file)) for file in os.listdir(r'D:\dataset\backgroud')]
count = 0
for bkpath in backpaths:
    for file in os.listdir(r'D:\dataset\images'):
            originalimg = cv2.imread(os.path.join(r'D:\dataset\images',file))
            try:
                imageHeight, imageWidth, _ = originalimg.shape
            except:
                print(dir, '*' * 30)
                continue
            imgback = cv2.imread(bkpath)
            imgback = cv2.resize(imgback, (imageWidth, imageHeight))
            img = np.zeros((imageHeight, imageWidth, 3), np.uint8)
            mask = cv2.imread(r'D:\dataset\labels\{}'.format(file.replace('jpg','png')),0)
            maskindex = np.where(img > 0)

            # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            _, binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)

            contours,_ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            s = contours[0].shape[0]
            cont = contours[0]
            for co in contours:
                if co.shape[0] > s:
                    cont = co
                    s = co.shape[0]

            if True:
                # 外接矩形
                x, y, w, h = cv2.boundingRect(cont)
                x1 = x
                y1 = y
                x2 = x1 + w
                y2 = y1 + h
                output = cv2.seamlessClone(originalimg,imgback,mask,((x1+x2)//2,(y1+y2)//2), cv2.NORMAL_CLONE)
                cv2.imwrite(r'D:\dataset\p\{}.jpg'.format(count), output)
                count += 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值