sublime 编辑器
之前用的是自带的编辑器,师兄推荐可以先用这sublime。之前没用过,网上先下载一个安装包:sublime安装包下载
激活方法
我没激活我的代码也能运行,但是激活后好像能快一丢丢。我猜测是我的代码少。大的工程可能就运行不了了。
那个配置图里的代码不太清楚,把下面这个复制进去,就可以按f5直接运行程序了。工程什么的现在还没用到,用到在看。
[ {"keys": ["f5"], "command": "run_existing_window_command", "args": {"id": "repl_python_run", "file": "config/Python/Main.sublime-menu" } } ]
测试一下有没有安装好。
print('hello word!')
这是输出结果
这是运行结果。把之前的代码文件打开,运行了一下也没有问题。
下面开始正文,图像噪声添加。
有一个问题
只有第一次打开文件时,f5会解释运行程序,后面把程序修改之后,再按f5程序执行结果还是打开文件时的程序的运行结果。关闭时会提示是否保存修改,保存后再打开文件此时运行才能看到修改效果。
有点烦,搜了半天不知道哪的问题,破解的问题?应该是没破解?和project有关的是灰色的。
和project无关,应该是破解出问题,按Ctrl+B创建时,窗口会直接闪退。
卸载重按一次。
Ctrl+B不闪退了,可以运行了,但是f5不运行修改后的程序问题任然存在。
不行,搞不定。不知道哪出问题了。。。。。。。
图像噪声添加
import matplotlib.pyplot as plt
import cv2
import numpy as np
import random
def sp_noise(image,prob):
'''
添加椒盐噪声
prob:噪声比例
'''
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
def gasuss_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
mean : 均值
var : 方差
'''
image = np.array(image/255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out*255)
return out
# r ‘\’ 为转义字符,前面加 r 能避免转义保持原义
# python中单引号与双引号没有区别,唯一区别在于,双引号括住的字符串中使用单引号不需要转义,单引号括住的字符中双引号不需要转义。
#图片路径
img_path = r"E:\Laboratory documents\DIP_study_note\image\picture.jpg"
#img_path = input("请输入地址:")
#读取图片
image = cv2.imread(img_path)
#print("image shape:",end = ' ')
#print(image.shape)
print('image shape'+ str(image.shape))
#名称数组
#数组使用的方括号不是大括号
name_label=['original image','image_reverse']
'''
#imshow()负责对图像进行处理,并显示其格式,但是不能显示。
plt.imshow(image)
plt.title(name_label[0])
#展示图片
plt.show()
'''
'''
#将图片RGB三通道分离开来
B,G,R = cv2.split(image)
#将三个通道合并起来
image_merge = cv2.merge([R,G,B])
plt.imshow(image_merge)
plt.title("image_merge")
plt.show()
'''
#也可以利用反向提取,
#将存放图片的矩阵三通道反转。这个是看网上的。
image_reverse = image[:,:,::-1]
'''
plt.imshow(image_reverse)
plt.title(name_label[1])
#注意汉语拼音与英语拼音,不能识别汉字,所以不要使用汉字
plt.xlabel('x_axis')
plt.ylabel('y_axis')
#保存图片,
#必须要放在show()前面,show()之后会自动释放图表内存
# './'使用的是程序当前路径
plt.savefig('E:\Laboratory documents\DIP_study_note\image\plt_figure\image_reverse.png')
#plt.show()
#将彩色图像转为灰度图像
#RGBtoGray(image)
'''
# 添加椒盐噪声,噪声比例为 0.02
out1 = sp_noise(image_reverse, prob=0.3)
# 添加高斯噪声,均值为0,方差为0.01
out2 = gasuss_noise(image_reverse, mean=0, var=0.01)
# 显示图像
titles = ['Original Image', 'Add Salt and Pepper noise','Add Gaussian noise']
images = [image_reverse, out1, out2]
print("Add Salt and Pepper noise"+str(out1.shape))
print("Add Salt and Pepper noise"+str(out2.shape))
plt.figure(figsize = (20, 15))
for i in range(3):
plt.subplot(1,3,i+1)
plt.imshow(images[i])
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
上图左一为原图,中间为加了椒盐噪声右边为加了高斯噪声的图片。明显看到加噪声图片画质变得模糊。
加噪声关键在创建噪声模板,
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
...
out = np.clip(out, low_clip, 1.0)
用到的几个函数介绍
np.clip()函数
numpy.clip(a, a_min, a_max, out=None)
裁剪(限制)数组中的值。
给定一个间隔,该间隔之外的值将被裁剪到间隔边缘。 例如,如果指定间隔[0,1],则小于0的值将变为0,而大于1的值将变为1。
a : 输入的数组
a_min: 限定的最小值 也可以是数组 如果为数组时 shape必须和a一样
a_max:限定的最大值 也可以是数组 shape和a一样
out:剪裁后的数组存入的数组
np.random.normal()函数
numpy.random.normal(loc=0,scale=1e-2,size=shape)
- 参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布,
- 参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线 越高瘦。
- 参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
np.zeros()函数
np.zeros()函数返回一个元素全为0且给定形状和类型的数组:
zeros(shape, dtype=float, order=‘C’)
- shape:形状
- dtype:数据类型,可选参数,默认numpy.float64
- order:可选参数,c代表与c语言类似,行优先;F代表列优先
np.array()函数
numpy.array(object, dtype=None)
object:创建的数组的对象,可以为单个值,列表,元胞等。
dtype:创建数组中的数据类型。
返回值:给定对象的数组。