Greyscale image transformation as a function g(x,y) = f(a*x,b*y)

Viewed 105

I have the following code. I'm reading a greyscale image into f and simply need to treat it as a function f(x,y) and transform it into g(x,y)=f(-x,y) and g(x,y)=f(kx,ky). In general, I need to be able to transform the image as g(x,y)=f(ax,bx). It should be very straightforward, but I have tried various approaches (all ultimately wrong so not shown) but can't properly access the pixel values to multiply them - there is always some kind of error I can't solve.

My question is: how do I transform f in the mathematical way shown (commented as #####... in the code) correctly?

Note: f becomes a 3D numpy array when "filename" is read.

Thank you for any assistance.

import matplotlib.pyplot as plt
import numpy as np

rows = 2
cols = 2
axes=[]
fig=plt.figure()

f = np.array(plt.imread(r"filename", format = None)

b = 3
g1 = b*f
##### g2 = f(-x,y)
##### g3 = f(k*x,k*y)
 
masterfx =[f, g1, g2, g3]

for i in range(rows*cols):
    axes.append( fig.add_subplot(rows, cols, i+1) )
    subplot_title=("Image "+str(i+1))
    axes[-1].set_title(subplot_title)  
    plt.imshow(masterfx[i])
fig.tight_layout()    
plt.show()
1 Answers

I was able to "solve" the problem, I think. But, if this is right, it's certainly not very elegant: (the multiple versions of f as f0, f1, f2, allow different results to be displayed upon showing the 4 images - otherwise, all functions looked the same if just using f - somehow effecting each other from right to left.

import matplotlib.pyplot as plt
import numpy as np

rows = 2
cols = 2
axes=[]
fig=plt.figure()

b = 3
K = 5

f0 = np.array(plt.imread(r"filename", format = None))

x, y, z = f0.shape

f1 = np.array(plt.imread(r"filename", format = None))
f2 = np.array(plt.imread(r"filename", format = None))
f3 = np.array(plt.imread(r"filename", format = None))

g1 = b*f1
g2 = f2
g3 = f3

for i in range(x):
    g2[i,:,:] = -1*g2[i,:,:]
    
for i in range(x):
    for j in range(y):
        g3[i,j,:] = K*g2[i,j,:]

masterfx =[f0, g1, g2, g3]

for i in range(rows*cols):
    axes.append( fig.add_subplot(rows, cols, i+1) )
    subplot_title=("Image "+str(i+1))
    axes[-1].set_title(subplot_title)  
    plt.imshow(masterfx[i])
fig.tight_layout()    
plt.show()
Related