gpt4 book ai didi

Python利用ROI进行图像合成的问题小结

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 29 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python利用ROI进行图像合成的问题小结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

需求:

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

Python利用ROI进行图像合成的问题小结

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

Python利用ROI进行图像合成的问题小结

roi合成圆形区域 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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后懒得该回去了,不加应该也没什么问题~ 。

?
1
2
3
4
5
6
7
8
9
10
11
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)

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

Python利用ROI进行图像合成的问题小结

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

Python利用ROI进行图像合成的问题小结

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

roi合成矩形区域 。

?
1
2
3
4
5
6
7
8
9
10
11
12
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方法,但尝试了三种模式效果均不理想:

?
1
2
3
4
5
6
7
8
9
10
11
12
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 。

Python利用ROI进行图像合成的问题小结

normal_clone 。

Python利用ROI进行图像合成的问题小结

monochrome_transfer 。

Python利用ROI进行图像合成的问题小结

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

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

原文链接:https://blog.csdn.net/weixin_43837555/article/details/118500289 。

最后此篇关于Python利用ROI进行图像合成的问题小结的文章就讲到这里了,如果你想了解更多关于Python利用ROI进行图像合成的问题小结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com