np.stack()用法

import tensorflow as tf
import numpy as np
def sobel_filter():
    fx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).astype(np.float32)
    fy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]).astype(np.float32)

    fx = np.stack((fx, fx), axis=2)
    fy = np.stack((fy, fy), axis=2)
    print(fx)
    print("#############")
    print(fy)
    fx = np.reshape(fx, (3, 3, 2, 1))
    fy = np.reshape(fy, (3, 3, 2, 1))

    # tf_fx = tf.Variable(tf.constant(fx))
    # tf_fy = tf.Variable(tf.constant(fy))

    return fx, fy
x,y=sobel_filter()
print(x)
print("############")
print(y)
  • 结果
[[[-1. -1.]
  [ 0.  0.]
  [ 1.  1.]]

 [[-2. -2.]
  [ 0.  0.]
  [ 2.  2.]]

 [[-1. -1.]
  [ 0.  0.]
  [ 1.  1.]]]
#############
[[[-1. -1.]
  [-2. -2.]
  [-1. -1.]]

 [[ 0.  0.]
  [ 0.  0.]
  [ 0.  0.]]

 [[ 1.  1.]
  [ 2.  2.]
  [ 1.  1.]]]
#######
[[[[-1.]
   [-1.]]

  [[ 0.]
   [ 0.]]

  [[ 1.]
   [ 1.]]]


 [[[-2.]
   [-2.]]

  [[ 0.]
   [ 0.]]

  [[ 2.]
   [ 2.]]]


 [[[-1.]
   [-1.]]

  [[ 0.]
   [ 0.]]

  [[ 1.]
   [ 1.]]]]
############
[[[[-1.]
   [-1.]]

  [[-2.]
   [-2.]]

  [[-1.]
   [-1.]]]


 [[[ 0.]
   [ 0.]]

  [[ 0.]
   [ 0.]]

  [[ 0.]
   [ 0.]]]


 [[[ 1.]
   [ 1.]]

  [[ 2.]
   [ 2.]]

  [[ 1.]
   [ 1.]]]]

你可能感兴趣的:(numpy,基础)