2

I checked out gradient descent using python and numpy but it didn't solve my problem.

I'm trying to get familiar with image-processing and I want to generate a few test arrays to mess around with in Python.

Is there a method (like np.arange) to create a m x n array where the inner entries form some type of gradient?

I did an example of a naive method for generating the desired output.

Excuse my generality of the term gradient, I'm using it in it's simple meaning as smooth transition in color.

#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt

#Set up parameters
m = 15
n = 10
A_placeholder = np.zeros((m,n))
V_m = np.arange(0,m).astype(np.float32)
V_n = np.arange(0,n).astype(np.float32)

#Iterate through combinations
for i in range(m):
    m_i = V_m[i]
    for j in range(n):
        n_j = V_n[j]
        A_placeholder[i,j] = m_i * n_j #Some combination

#Relabel
A_gradient = A_placeholder
A_placeholder = None

#Print data
print A_gradient
#[[   0.    0.    0.    0.    0.    0.    0.    0.    0.    0.]
 [   0.    1.    2.    3.    4.    5.    6.    7.    8.    9.]
 [   0.    2.    4.    6.    8.   10.   12.   14.   16.   18.]
 [   0.    3.    6.    9.   12.   15.   18.   21.   24.   27.]
 [   0.    4.    8.   12.   16.   20.   24.   28.   32.   36.]
 [   0.    5.   10.   15.   20.   25.   30.   35.   40.   45.]
 [   0.    6.   12.   18.   24.   30.   36.   42.   48.   54.]
 [   0.    7.   14.   21.   28.   35.   42.   49.   56.   63.]
 [   0.    8.   16.   24.   32.   40.   48.   56.   64.   72.]
 [   0.    9.   18.   27.   36.   45.   54.   63.   72.   81.]
 [   0.   10.   20.   30.   40.   50.   60.   70.   80.   90.]
 [   0.   11.   22.   33.   44.   55.   66.   77.   88.   99.]
 [   0.   12.   24.   36.   48.   60.   72.   84.   96.  108.]
 [   0.   13.   26.   39.   52.   65.   78.   91.  104.  117.]
 [   0.   14.   28.   42.   56.   70.   84.   98.  112.  126.]]

#Show Image
plt.imshow(A_gradient)
plt.show()

enter image description here

I've tried np.gradient but it didn't give me the desired output.

#print np.gradient(np.array([V_m,V_n]))
#Traceback (most recent call last):
#    File "Untitled.py", line 19, in <module>
#        print np.gradient(np.array([V_m,V_n]))
#    File "/Users/Mu/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1458, in gradient
#        out[slice1] = (y[slice2] - y[slice3])
#ValueError: operands could not be broadcast together with shapes (10,) (15,) 

1 Answer 1

2
A_placeholder[i,j] = m_i * n_j

Any operation like that can be expressed in numpy using broadcasting

A = np.arange(m)[:, None] * np.arange(n)[None, :]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.