Search code examples
pythonimageimage-processingpwm

Phase shifted fringe pattern


I have created a fringe pattern for a structured light test like this one (Binary pattern) using a code snippet for generating a pwm signal in python

import numpy as np
import cv2

amp=float(input('fringe amplitude:'))
t=float(input('time period:'))
c=int(input('cycle time:'))
dt=1
xpix=np.arange(0,c*t,dt)
ypix=np.zeros_like(xpix)
n=t/dt

i=0
while i*dt< c*t:
    if (i % n)/n < amp/100.0:
        ypix[i]=1
    i=i+1

A =255*np.tile(ypix,(1140,1))
print(A)
i= input('enter value:')
cv2.imwrite('C:\\Users\\newimg'+str(i)+'.bmp', A)

I want to phase shift these by 1/3 the time period of each signal , to create two new shifted images. Is there a way to edit this code to do it or a better /easier option to use?


Solution

  • Some things about your code are not entirely clear to me, however if you want to shift the pattern you should simply add a variable defining by how much to shift it, and then offset your axis by that variable, like so:

    import numpy as np
    import cv2
    
    amp=float(input('fringe amplitude:'))
    t=float(input('time period:'))
    c=int(input('cycle time:'))
    
    # this is the shift variable
    shift = int(input('shift amount'))
    
    dt=1
    xpix=np.arange(0,c*t,dt)
    ypix=np.zeros_like(xpix)
    n=t/dt
    
    i=0
    while i*dt< c*t:
        # Here we offset i by the shift variable.
        # Because we are summing, we are moving everything left by $shift pixels
        # If you want to move right simply insert a negative value in shift
        if ((i + shift) % n)/n < amp/100.0:
            ypix[i]=1
        i=i+1
    
    A =255*np.tile(ypix,(1140,1))
    print(A)
    i= input('enter value:')
    cv2.imwrite('newimg'+str(i)+'.bmp', A)