Python 生成 -1~1 之间的随机数矩阵

来源:互联网 发布:淘宝网平板电脑手机 编辑:程序博客网 时间:2024/06/08 01:21

1. 使用函数 np.random.random


        由于 np.random.random()  默认生成 0~1 之间的小数,因此需要转换一下


             如生成 3*3 的 -1~1 之间的随机数矩阵

             

             -1 + 2*np.random.random((3,3))


# -*- coding:utf-8 -*-import matplotlib.pyplot as pltimport pylabimport cv2import numpy as npimg = plt.imread("1.png")                        #在这里读取图片#plt.imshow(img)                                     #显示读取的图片#pylab.show()print "start processing..."for i in range(1,200):#    fil = np.random.randint(0, 10, size=[3, 3])    fil = -1 + 2*np.random.random((3,3))    res = cv2.filter2D(img,-1,fil)                      #使用opencv的卷积函数#    plt.imshow(res)                                     #显示卷积后的图片    pic_name = str(i) + ".png"#    plt.imsave(pic_name, res)#    plt.imsave("res.jpg",res)#    pylab.show()print "complete!"





原创粉丝点击