- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python利用ROI进行图像合成的问题小结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
之前使用seamlessclone来合成图片,但发现在两张图片的交集部分会出现一些小问题…… 。
需求:
假设现在有一张图片(模板)中存在两个空格可以用来填照片(如下图所示):
图中,蓝色的圆圈和黄色的圆圈为需要替换的内容,其余部分可以视为一张png图片,且通过ps可知蓝圆和黄圆的具体坐标,需要将下方的两张图片合成到上方的位置中:
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)
|
不出意外的话应该就可以得到下面的这张图片啦!~ 。
然后再把模板的那张png图片盖到最上面——可以利用上文中mask的思路,也可以放到ps里面合成~这里一方面我需要在ps中进行后续的一些操作,另一方面也需要观察图片边缘的处理效果,因而选择了后者.
和模板里的位置完美对齐!~ 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 。
normal_clone 。
monochrome_transfer 。
normal_clone和mixed_clone的区别主要看的是两个圆的交界处,但这两种方法的边缘都会有一个过渡的处理,不太适合套模板的时候用…… 。
到此这篇关于python利用roi进行图像合成的文章就介绍到这了,更多相关python图像合成内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/weixin_43837555/article/details/118500289 。
最后此篇关于Python利用ROI进行图像合成的问题小结的文章就讲到这里了,如果你想了解更多关于Python利用ROI进行图像合成的问题小结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我喜欢 smartcase,也喜欢 * 和 # 搜索命令。但我更希望 * 和 # 搜索命令区分大小写,而/和 ?搜索命令遵循 smartcase 启发式。 是否有隐藏在某个地方我还没有找到的设置?我宁
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
从以下网站,我找到了执行java AD身份验证的代码。 http://java2db.com/jndi-ldap-programming/solution-to-sslhandshakeexcepti
似乎 melt 会使用 id 列和堆叠的测量变量 reshape 您的数据框,然后通过转换让您执行聚合。 ddply,从 plyr 包看起来非常相似..你给它一个数据框,几个用于分组的列变量和一个聚合
我的问题是关于 memcached。 Facebook 使用 memcached 作为其结构化数据的缓存,以减少用户的延迟。他们在 Linux 上使用 UDP 优化了 memcached 的性能。 h
在 Camel route ,我正在使用 exec 组件通过 grep 进行 curl ,但使用 ${HOSTNAME} 的 grep 无法正常工作,下面是我的 Camel 路线。请在这方面寻求帮助。
我正在尝试执行相当复杂的查询,在其中我可以排除与特定条件集匹配的项目。这是一个 super 简化的模型来解释我的困境: class Thing(models.Model) user = mod
我正在尝试执行相当复杂的查询,我可以在其中排除符合特定条件集的项目。这里有一个 super 简化的模型来解释我的困境: class Thing(models.Model) user = mod
我发现了很多嵌入/内容项目的旧方法,并且我遵循了在这里找到的最新方法(我假设):https://blog.angular-university.io/angular-ng-content/ 我正在尝试
我正在寻找如何使用 fastify-nextjs 启动 fastify-cli 的建议 我曾尝试将代码简单地添加到建议的位置,但它不起作用。 'use strict' const path = req
我正在尝试将振幅 js 与 React 和 Gatsby 集成。做 gatsby developer 时一切看起来都不错,因为它发生在浏览器中,但是当我尝试 gatsby build 时,我收到以下错
我试图避免过度执行空值检查,但同时我想在需要使代码健壮的时候进行空值检查。但有时我觉得它开始变得如此防御,因为我没有实现 API。然后我避免了一些空检查,但是当我开始单元测试时,它开始总是等待运行时异
尝试进行包含一些 NOT 的 Kibana 搜索,但获得包含 NOT 的结果,因此猜测我的语法不正确: "chocolate" AND "milk" AND NOT "cow" AND NOT "tr
我正在使用开源代码共享包在 iOS 中进行 facebook 集成,但收到错误“FT_Load_Glyph failed: glyph 65535: error 6”。我在另一台 mac 机器上尝试了
我正在尝试估计一个标准的 tobit 模型,该模型被审查为零。 变量是 因变量 : 幸福 自变量 : 城市(芝加哥,纽约), 性别(男,女), 就业(0=失业,1=就业), 工作类型(失业,蓝色,白色
我有一个像这样的项目布局 样本/ 一种/ 源/ 主要的/ java / java 资源/ .jpg 乙/ 源/ 主要的/ java / B.java 资源/ B.jpg 构建.gradle 设置.gr
如何循环遍历数组中的多个属性以及如何使用map函数将数组中的多个属性显示到网页 import React, { Component } from 'react'; import './App.css'
我有一个 JavaScript 函数,它进行 AJAX 调用以返回一些数据,该调用是在选择列表更改事件上触发的。 我尝试了多种方法来在等待时显示加载程序,因为它当前暂停了选择列表,从客户的 Angul
可能以前问过,但找不到。 我正在用以下形式写很多语句: if (bar.getFoo() != null) { this.foo = bar.getFoo(); } 我想到了三元运算符,但我认
我有一个表单,在将其发送到 PHP 之前我正在执行一些验证 JavaScript,验证后的 JavaScript 函数会发布用户在 中输入的文本。页面底部的标签;然而,此消息显示短暂,然后消失...
我是一名优秀的程序员,十分优秀!