Pass 2D Numpy pixel matrix coordinates to distance function

I am working on an image processing program with OpenCV and numpy. For most pixel operations, I can avoid nested loops using np.vectorize (), but one of the functions that I need to implement is required as a parameter "distance from the center" or basically the coordinate of the point being processed.

Pseudo Exam:

myArr = [[0,1,2]
         [3,4,5]]

def myFunc(val,row,col):
    return [row,col]

f = np.vectorize(myFunc)
myResult = f(myArr,row,col)

I obviously can't get elemX and elemY from a vectorized array, but is there another numpy function that I could use in this situation or do I need to use for loops ?, is there a way to do this with openCV?

The function with which I must place each pixel is:, f(i, j) = 1/(1 + d(i, j)/L)d (i, j) is the Euclidean distance of the point from the center of the image.

+4
2

, ( , ):

    import numpy as np

myArr = np.array([[0,1,2], [3,4,5]])

nx, ny = myArr.shape
x = np.arange(nx) - (nx-1)/2.  # x an y so they are distance from center, assuming array is "nx" long (as opposed to 1. which is the other common choice)
y = np.arange(ny) - (ny-1)/2.
X, Y = np.meshgrid(x, y)
d = np.sqrt(X**2 + Y**2)

#  d = 
#  [[ 1.11803399  1.11803399]
#   [ 0.5         0.5       ]
#   [ 1.11803399  1.11803399]]

f(i, j):

f = 1/(1 + d/L)


, np.vectorize() . , , , :

, . for.

, (, f , , L ), numpy.vectorize(), .

+1

np.vectorize , , `

# This compute distance between all points of MyArray and the center

dist_vector= np.sqrt(np.sum(np.power(center-MyArray,2),axis=1))


# F will contain the target value for each point

F = 1./(1 + 1. * dist_vector/L)
+1

All Articles