Plotting images relative to each other in Matplotlib

I am working on a robot that has a downward facing camera. To show how my localization filter works, I would like to plot grass patterns on a chart using a robot path. This means that I have to place the rotated images in the graph. Is it possible to depict the image relative to the origin or each other with rotation?
I want to avoid using ndimage.rotate as it rotates the image data, not the image in the axes.

I tried using:

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()

      

to no avail, the image was placed on top of the previous one.

+3


source to share


1 answer


Here's an example with two copies of the image, different rotations and translations, one after the other. Lots of extreme tweaks and Z-ordering and acceptance of an understandable function and all is left to do. I've Frankensteined your code and 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

+1


source







All Articles