图像篡改数据增强(1)——随机copymove

前提:包含原始图像和掩膜图像
任务:将一幅图像的随机区域,复制移动其他其余,并在对应的标签生成篡改后的标签

import numpy as np
import os
import cv2
import random
import glob

def rand_bbox(sie,cons_W,cons_H):
    H, W= size
    x = np.random.randint(0, W)
    y = np.random.randint(0, H)
    # w = np.random.randint(5, W//2)  # 最低10个像素的长宽,最高不超过图像边界
    # h = np.random.randint(5, H//2)

    # while(x+cons_W>=W):
    #     x = np.random.randint(0, cons_W)
    #     print("x out:",x)
    #     if cons_W==0:
    #         x=0
    #         break
    # while(y+cons_H>=H):
    #     y = np.random.randint(0, cons_H)
    #     print("y out:",y)
    #     if cons_H==0:
    #         y=0
    #         break
    # print(x,y)
    if x+cons_W>=W:
        x=W-cons_W
        print("x out")
    if y+cons_H>=H:
        y=W-cons_H
        print("y out")

    return x, y


def copy_move(img, img2, mask1, mask2):
    # H, W, C = img.shape
    mask_new = np.zeros(img.shape[:2], dtype="uint8")
    contours, hierarchy = cv2.findContours(mask2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    print(len(contours))
    for num in range(len(contours)):
        try:
            if len(contours) > 8:
                return img, mask1
            if len(contours) ==0:
                return img, mask1

            # cv2.drawContours(img2, contours, num, (0, 0, 255), 2)
            # print(contours)
            # approx = cv2.approxPolyDP(contours[0], 20, True)
            # cv2.drawContours(img2, [approx], 0, (0, 255, 0), 5)
            # cns = contours[0]
            X, Y, W, H = cv2.boundingRect(contours[num])
            X=X
            Y=Y
            W=W
            H=H
            # print(X,Y,W,H)
            # img2 = cv2.rectangle(img2, (X-10, Y-10), (X + W+10, Y + H+10), (255, 255, 0), 3)

            bbx1, bby1 = rand_bbox(img.shape[:2],W,H)
            img[bby1: bby1 +H, bbx1: bbx1 + W, :] = img2[Y:Y+H, X:X+W, :]

            mask_temp = np.zeros(img.shape[:2], dtype="uint8")
            mask_temp = cv2.rectangle(mask_temp, (bbx1, bby1), (bbx1 + W, bby1 + H), (255, 255, 255), -1)

            mask_new = cv2.add(mask_temp, mask_new)
        except:
            print("error")
            continue


    mask_new = cv2.add(mask_new, mask1)

    # cv2.imwrite('img.png', img)
    # cv2.imwrite('mas.png', mask_new)
    return img, mask_new

image_path = r"TTI_resize_dataset\train\imgs"
mask_path = r"TTI_resize_dataset\train\masks"

save_img = r"rdcpmask2\imgs"
save_mask = r"rdcpmask2\masks"


if not os.path.exists(save_img):
    os.makedirs(save_img)
if not os.path.exists(save_mask):
    os.makedirs(save_mask)


image_tamper_path=glob.glob(os.path.join(image_path, 't*'))
# print(image_tamper_path)


imgs_name = os.listdir(image_path)

for i in range(300):
    random.shuffle(imgs_name)
    random.shuffle(image_tamper_path)

    img1_name = imgs_name[0]
    img1_path = os.path.join(image_path, img1_name)

    img2_path = image_tamper_path[0]
    img2_name =img2_path.split('\\')[-1]

    mask1_name = img1_name.replace("jpg","png")
    mask1_path = os.path.join(mask_path, mask1_name)


    mask2_name = img2_name.replace("jpg","png")
    mask2_path = img2_path.replace('imgs','masks')
    mask2_path = mask2_path.replace('jpg','png')


    image1 = cv2.imread(img1_path)
    image2 = cv2.imread(img2_path)
    mask1 = cv2.imread(mask1_path,0)
    mask2 = cv2.imread(mask2_path,0)

    mask1[mask1 >= 50] = 255
    mask1[mask1 < 50] = 0
    mask2[mask2 >= 50] = 255
    mask2[mask2 < 50] = 0

    img, mask = copy_move(image1, image2, mask1, mask2)


    cv2.imwrite(os.path.join(save_img, 'rd_'+ str(i) + '_' + img1_name),img)
    cv2.imwrite(os.path.join(save_mask, 'rd_'+ str(i) + '_' + mask1_name),mask)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值