python - 如何使用python中的平移矩阵将小图像的特定坐标放置到大图像的特定坐标
问题描述
我正在学习OpenCV,我正在寻找python中的代码,它获取小图像的输入坐标并将其映射到大图像的坐标,以便将小图像插入到大图像中,并且可以像这样进行变换旋转。我想使用翻译矩阵作为输入来做到这一点。例如,如果矩阵是:
([75, 120][210,320],
[30, 90][190,305],
[56, 102][250,474],
[110, 98][330,520])
这意味着小图像中 (75, 120) 处的像素应映射到大图像中 (210, 320) 处的像素,小图像中 (30, 90) 处的像素应映射到大图像中 (190, 305) 处的像素...我搜索了很多,但我没有得到我的问题的正确答案。我怎么解决这个问题?
解决方案
我不知道将像素映射到任何地方的像素的矩阵运算,并且由于图像通常由二维数组表示,因此没有一种通用的方法可以使这些像素指向相同的数据。
但鉴于这些图像由 NumPy 数组表示,您可以使用高级索引将任何像素从一个数组复制到另一个数组:
# smallimage is a NumPy array
# bigimage is a NumPy array
### Indices ###
# I formatted it so the matching indices
# between the 2 images line up in a column
bigD1 = [210, 190, 250, 330] # dimension 0
bigD2 = [320, 305, 474, 520] # dimension 1
smallD1 = [75, 30, 56, 110]
smallD2 = [120, 90, 102, 98]
### copy pixels from small image to big image ###
# on right side of =, advanced indexing copies
# the selected pixels to a new temporary array
# v
bigimage[bigD1, bigD2] = smallimage[smallD1, smallD2]
# ^
# on left side of =, advanced indexing specifies
# where we copy the temporary array's pixels to.
# smallimage is unchanged
# bigimage has edited pixels
推荐阅读
- css - 容器无法正常工作时隐藏的可见性
- reactjs - 如何解决消息 304 未修改
- react-native - 如何在 android react-native 应用程序中添加管理面板以及托管此管理面板的位置
- vb.net - 根据 vb.net 中的进程名称或进程 ID 隐藏应用程序
- javascript - 带有点边界的Javascript六边形头像
- c - 是否有一种标准方法可以保证在编译(翻译)时评估某个(常量)表达式?
- c++ - 获取 Eigen::vector 的标准差
- python - 如何在 python 中使用 selenium 进行两步验证登录
- sql-server - 如何在 Grafana 仪表板中捕获 SQL Server DML
- bittorrent - PEX 消息是否在 2 个种子之间交换