在Matplotlib中相对于彼此绘制图像

时间:2015-05-05 09:48:56

标签: matplotlib

我正在研究一台有朝下相机的机器人。为了说明我的定位过滤器是如何做的,我想用机器人的路径绘制图形中的草地补丁。这意味着我必须将旋转的图像放在图表中。是否可以通过旋转绘制相对于原点或彼此的图像?
我想避免使用ndimage.rotate,因为它会旋转图像的数据,而不是轴上的图像。

我试图使用:

import import matplotlib.pyplot as mplplt
import matplotlib.transforms as mpltf
import matplotlib.image as mplimg

grass = mplimg.imread("grass0.png")
grass = mplimg.imread("grass1.png")
fig = mplplt.figure()
ax = mplplt.subplot(111)

#Show previous grass image
ax.imshow(grass, extent = (-5, 5, -5, 5))

#Next image
tf = mpltf.Affine2D().rotate_deg(30).translate(1,1)
ax.imshow(grass1, extent = (-5, 5, -5, 5))#, transform = tf)

mplplt.show()

无济于事,图像位于上一张图像的顶部。

1 个答案:

答案 0 :(得分:1)

这是一个示例,包含两个图像副本,不同的旋转和翻译,一个在下一个。大量的限制调整和Z排序,并使一个可理解的功能和所有剩下的事情。我对您的代码和http://matplotlib.org/examples/api/demo_affine_image.html

进行了检查
import matplotlib.pyplot as mplplt
import matplotlib.transforms as mpltf
import matplotlib.image as mplimg

def imshow_affine(ax, z, *kl, **kwargs):
    im = ax.imshow(z, *kl, **kwargs)
    x1, x2, y1, y2 = im.get_extent()
    im._image_skew_coordinate = (x2, y1)
    return im

grass = mplimg.imread("hammer_map.png")
fig = mplplt.figure()
ax = mplplt.subplot(111)

tf = mpltf.Affine2D().rotate_deg(30).translate(-1,-1) + ax.transData
im1 = imshow_affine(ax, grass, interpolation='none',
                        origin='lower',
                        extent=[1, 7, 0, 5], clip_on=False, zorder=2)

im1.set_transform(tf)

tf = mpltf.Affine2D().rotate_deg(190).translate(-2,1) + ax.transData
im2 = imshow_affine(ax, grass, interpolation='none',
                        origin='lower',
                        extent=[-3, 3, -2, 3], clip_on=False, zorder=1)

im2.set_transform(tf)

mplplt.show()

enter image description here