Search code examples
pythonvispy

How to convert visuals image to polar using Vispy


I am trying to convert my visual image to Polar form using Vispy

Something similar to below images..

Original Image generated using below code of Vispy

scene.visuals.Image(self.img_data,interpolation=interpolation,parent = self.viewbox.scene, cmap=self.cmap, method='subdivide', clim=(-65,40)) 

Image generated using Vispy

Required Polar Image: Required Polar Image

I did try to implement polarTransform using PolarTransform Example from Vispy but couldn't succeed.

Can anyone please guide my on how to do polarTransform of above Image to Polar using Vispy.

Thanks

Reply to @djhoese: Image generated by Vispy before PolarTransform Image generated by Vispy before PolarTransform

Image generated by Vispy after PolarTransform Image generated by Vispy after PolarTransform

Code for PolarTransform:

self.img.transform = PolarTransform()

Solution

  • ImageVisual and PolarTransform do not play well together without some nudging.

    VisPy has two methods of drawing, subdivide and impostor. I'll concentrate on subdivide here. This won't work with impostor.

    First, create the ImageVisual like that:

    img = visuals.ImageVisual(image, 
                              grid=(1, N),           
                              method='subdivide')
    

    For N use a reasonable high number (eg 360). Playing with that number you'll immediately see how the polar resolution is affected.

    Further you need to setup some specific transform chain:

    transform = (
                 # move to final location and scale to your liking
                 STTransform(scale=(scx,scy), translate=(xoff,yoff))
    
                 # 0
                 # just plain simple polar transform
                 *PolarTransform()
    
                 # 1
                 # pre scale image to work with polar transform
                 # PolarTransform does not work without this
                 # scale vertex coordinates to 2*pi
                 * STTransform(scale=(2 * np.pi / img.size[0], 1.0))
    
                 # 2
                 # origin switch via translate.y, fix translate.x
                 * STTransform(translate=(img.size[0] * (ori0 % 2) * 0.5,                                                                   
                                          -img.size[1] * (ori0 % 2)))
    
                 # 3
                 # location change via translate.x
                 * STTransform(translate=(img.size[0] * (-loc0 - 0.25), 0.0))
    
                 # 4
                 # direction switch via inverting scale.x
                 * STTransform(scale=(-dir0, 1.0))
    
                )
    # set transform
    img.transform = transform
    
    • dir0 - Direction cw/ccw (takes values -1/1, respectively)
    • loc0 - Location of Zero (value between 0 and 2 * np.pi, counter clockwise)
    • ori0 - Side which will be transformed to center of polar image (takes values 0, 1 for top or bottom

    The bottom four STTransform can surely be simplified. They are split apart to show the different changes and how they have to be applied.

    An example will be added to the VisPy examples section later.