cutAndPastPicture 切割部分同时覆盖背景原图和分割图

此篇博客介绍了一个使用Python的方法,通过随机选择背景图片并粘贴切割原图,同时更新背景和分割图的特定像素。作者展示了如何读取和操作图像文件,并利用OpenCV进行颜色替换和图像保存。
import cv2
import  numpy as np
import os
import os.path as osp
import random
from random import choice
#复制原图!!!切割部分同时覆盖背景原图和分割图
img_dirs = "/切割原图文件夹/data/select_segcolors/none"
backimg_dirs = "/背景图文件夹/data/1/images_copy"
gtsem_dirs = "/分割图文件夹/data/gt_semantic_copy"
#saveDirs = img_dirs.replace("limit_lever", "generator_img/1")
#os.makedirs(saveDirs, exist_ok=True)
#savegtsem_Dirs = "/data/gtsem_test"
#os.makedirs(savegtsem_Dirs, exist_ok=True)
###随机选择3张背景原图粘贴想要的切割原图
pathDir = os.listdir("/背景图文件夹/data/1/selectimg")
sample = random.sample(pathDir, 3)
index = 0
for backImg in sample:
    for name in os.listdir(img_dirs):
        backImg_path = osp.join(backimg_dirs, backImg)
        #print("backImg_path:", backImg_path)
        img_back = cv2.imread(backImg_path)
        img_back = cv2.cvtColor(img_back, cv2.COLOR_BGR2RGB)
        #rows, cols, channels = img_back.shape
        #print("backImg  height:", rows, "weight:", cols)
        (path, filename) = os.path.split(backImg_path)
        file_name, extension = os.path.splitext(filename)
        #print(filename)
        gtsem = (file_name + ".png")
        gtsem_path = osp.join(gtsem_dirs, gtsem)
        print("gtsem_path:", gtsem_path)
        gtsem_img = cv2.imread(gtsem_path)
        gtsem_img = cv2.cvtColor(gtsem_img, cv2.COLOR_BGR2RGB)
        #rows, cols, channels = gtsem_img.shape
        #print("gtsem_img  height:", rows, "weight:", cols)
        img_path = osp.join(img_dirs, name)
        #print("img_path:", img_path)
        img = cv2.imread(img_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        w, h = img.shape[0:2]
        # os.path.split()返回文件的路径和文件名
        (filepath, tempfilename) = os.path.split(img_path)
        #print(tempfilename)
        # os.path.splitext()将文件名和扩展名分开
        fname, fename = os.path.splitext(tempfilename)
        #print(fname)
        #print("img  height:", w, "weight:", h)
        # img=cv2.erode(img,None,iterations=1)
        # img=cv2.dilate(img,None,iterations=1)
        center=[0,0]#在新背景图片中的位置
        ###在切割原图中遍历像素值从而覆盖背景原图与分割图
        for i in range(w):
            for j in range(h):
                if img[i][j][0] < 10 and img[i][j][1] < 10 and img[i][j][2] < 10:#0代表黑色的点
                    continue
                else:
                    #对于背景原图此处替换切割原图颜色
                    img_back[center[0]+i, center[1]+j] = img[i, j]
                    #对于分割图此处替换切割图处分割像素值
                    gtsem_img[i][j][0] = 255
                    gtsem_img[i][j][1] = 255
                    gtsem_img[i][j][2] = 0
        savepath = osp.join(backimg_dirs, fname + backImg)
        cv2.imwrite(savepath, cv2.cvtColor(img_back, cv2.COLOR_BGR2RGB))
        savegtsem_path = osp.join(gtsem_dirs, fname + gtsem)
        cv2.imwrite(savegtsem_path, cv2.cvtColor(gtsem_img, cv2.COLOR_BGR2RGB))
        print("save ", fname + backImg, "to ", savepath)
        print("save ", fname + gtsem, "to ", savegtsem_path)
        # randomlist.remove(one)
print("congratulations--------*done*")
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值