opencv - 如何通过变换矩阵在python中进行图像变换操作(旋转、缩放、平移)
问题描述
我想在没有任何其他外部库(PIL,CV2)函数调用的情况下对图像执行矩阵变换操作,如旋转、缩放、平移。
我正在尝试使用基本的矩阵变换方法。但我面临一些问题,比如旋转有孔,图像被裁剪。
我想沿着中心执行上述所有操作。这里需要一些指导。
旋转逻辑:
newImage = np.zeros((2*row, 2*column, 3), dtype=np.uint8)
radAngle = math.radians(int(input("angle: ")))
c, s = math.cos(radAngle), math.sin(radAngle)
mid_coords = np.floor(0.5*np.array(image.shape))
for i in range(0, row-1):
for j in range(0, column-1):
x2 = mid_coords[0] + (i-mid_coords[0])*c - (j-mid_coords[1])*s
y2 = mid_coords[1] + (i-mid_coords[0])*s + (j-mid_coords[1])*c
newImage[int(math.ceil(x2)), int(math.ceil(y2))] = image[i, j]
解决方案
旋转图像中出现孔的原因是您将原始图像的每个像素分配给旋转图像中的计算像素位置。因此,旋转图像中的一些像素被遗漏了。你应该反过来做。对于旋转图像中的每个像素,从原始图像中找到它的值。请参考这个答案。
推荐阅读
- java - 当 SingleValueEvent 或 Transaction 的嵌套 ValueEventListener 失败时,如何在 Firebase RealtimeDB for Android 中回滚到以前的状态?
- angular - Angular 5 业力测试显示“app-addbook”不是已知元素
- sql - 如何找到一张表
- marklogic - XDMP-NOEXECUTE:文档不是可执行的 mimetype
- r - 如何舍入 geom_text 中包含在其他文本中的值?
- asp.net - 注入依赖层应用程序/域/存储库
- apache-spark - spark-submit 命令无法连接到 bluemix 分析引擎服务的 spark 节点
- omnet++ - 将所有复合模块门连接到 Omnet++ 中的子模块
- javascript - 在本地项目中加载 json 文件而不使用 webserver
- ios - 单击重置按钮后,UIbutton 不会重置为默认值