之前使用seamlessclone来合成图片,但发现在两张图片的交集部分会出现一些小问题……

需求:

假设现在有一张图片(模板)中存在两个空格可以用来填照片(如下图所示):

图中,蓝色的圆圈和黄色的圆圈为需要替换的内容,其余部分可以视为一张png图片,且通过ps可知蓝圆黄圆的具体坐标,需要将下方的两张图片合成到上方的位置中:

roi合成圆形区域

def input_circle_img(img, file_path, img_part_name, x, y, r):
    for file in os.listdir(file_path):
        if img_part_name in file:
            path = file_path + "\\" + file
            src = cv_imread(path)
            src = cv.resize(src, (r * 2 + 4, r * 2 + 4))
            h, w, ch = src.shape
            mask = np.zeros(src.shape[:2], dtype=np.uint8)
            mask = cv.circle(mask, (r + 1, r + 1), r, (255, 255, 255), -1)
            imgroi = img[(y - r):(y + r), (x - r):(x + r)]
            mask = mask/255.0
            a =  mask[..., none]
            for row in range(imgroi.shape[0]):
                for col in range(imgroi.shape[1]):
                    if a[row, col]:
                        imgroi[row, col] = src[row, col]
参数 说明
img 模板图片对象,即上文中的第一幅图片
file_path 需要替换的图片所在的文件路径,即上文中的1_测试.jpg和2_测试.jpg所在的文件夹路径
img_part_name 即需要替换的图片的(部分)文件名,比如我想换的是“1_测试.jpg”,则此参数可以为“1_”也可以为全名~(需要注意的是:填写的字符串尽量为文件夹中唯一的标识符,例如填“_测试”则可能导致想要的文件被其它图片所覆盖)
x 图片中心在模板中的横向位置(与模板左侧的距离)
y 图片中心在模板中的纵向位置(与模板上侧的距离)
r 图片出于模板中的实际半径

之所以+4是因为之前利用seamlessclone时边缘会收到原模板的影响,改成roi后懒得该回去了,不加应该也没什么问题~

def export_comp_img(path):
    print("[start] export_comp_img ...")
    for file_path in os.listdir(path):
        file_path = path + "\\" + file_path
        # 创建画布方法,就是利用np.zeros,与本文无关就不放啦~
        img = create_img(2400, 3600)
        input_circle_img(img, file_path, "2_", 1862, 800, 440)
        input_circle_img(img, file_path, "1_", 1247, 558, 315)
        # input_rect_img(img, file_path, "3_", (0, 2202), (2400, 2944))
        # 保存图片方法,就是利用imencode,与本文无关就不放啦~
        save_img(img, file_path)

不出意外的话应该就可以得到下面的这张图片啦!~

然后再把模板的那张png图片盖到最上面——可以利用上文中mask的思路,也可以放到ps里面合成~这里一方面我需要在ps中进行后续的一些操作,另一方面也需要观察图片边缘的处理效果,因而选择了后者。

和模板里的位置完美对齐!~
ps:如果是除圆以外的不规则图形的话,可以通过改变mask实现——最粗暴的便是加载一张mask图片~
而若是单纯的矩形选区的话则无视mask即可~
至此完结!~下面是一些无关紧要的补充……

roi合成矩形区域

def input_rect_img(img, file_path, img_part_name, start_point, end_point):
    for file in os.listdir(file_path):
        if img_part_name in file:
            path = file_path + "\\" + file
            src = cv_imread(path)
            h = end_point[1] - start_point[1]
            w = end_point[0] - start_point[0]
            src = cv.resize(src, (w, h))
            imgroi = img[start_point[1]:(start_point[1] + h),start_point[0]:(start_point[0] + w)]
            for row in range(imgroi.shape[0]):
                for col in range(imgroi.shape[1]):
                    imgroi[row, col] = src[row, col]

seamlessclone合成圆形区域

值得一提的是,一开始我用的是seamlessclone方法,但尝试了三种模式效果均不理想:

def input_circle_img_seamlessclone(img, file_path, img_part_name, x, y, r):
    for file in os.listdir(file_path):
        if img_part_name in file:
            path = file_path + "\\" + file
            src = cv_imread(path)
            src = cv.resize(src, (r * 2 + 4, r * 2 + 4))
            h, w, ch = src.shape
            mask = np.zeros(src.shape[:2], dtype=np.uint8)
            mask = cv.circle(mask, (r + 1, r + 1), r, (255, 255, 255), -1)
            center = (x, y)
            output = cv.seamlessclone(src, img, mask, center, cv.mixed_clone)
            return output

mixed_clone

normal_clone

monochrome_transfer

normal_clonemixed_clone的区别主要看的是两个圆的交界处,但这两种方法的边缘都会有一个过渡的处理,不太适合套模板的时候用……

到此这篇关于python利用roi进行图像合成的文章就介绍到这了,更多相关python图像合成内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!