【OpenCV+TensorFlow】清华博士带你做项目!计算机视觉实战+深度学习,项目可写进简历!(附源码资料)-人工智能/深度学习框架/RNN/池化层/感受_哔哩哔哩_bilibili import cv2 # 读取的格式是BGR import matplotlib.pyplot as plt import numpy as np def cv_show(name, img): cv2.imshow('image', img) # 第一个为窗口名字 cv2.waitKey(0) # 等待时间,毫秒级,0表示任意键终止 cv2.destroyAllWindows() ''' 阈值与平滑处理 ret.dst=cv2.threshold(src,thresh,maxval,type) src:输出图,只能输入单通道图像,通常来说为灰度图 dst:输出图 thresh:阈值 maxval:当像素超过了阈值(或者小于阈值,根据type来决定),所赋予的值 type:二值化操作的类型,包含以下5种类型:cv2.THRESH_BINARY超过阈值部分取maxval(最大值),否则取0; cv2.THRESH_BINARY_INY THRESH_BINARY的反转; cv2.THRESH_TRUNC 大于阈值部分设为阈值,否则不变; cv2.THRESH_TOZERO 大于阈值部分不改变,否则设为0; cv2.THRESH_TOZERO_INV THRESH_TOZERO的反转; ''' # flower = cv2.imread('flower.jpg') # # cv_show('flower', flower) # ret, thresh1 = cv2.threshold(flower, 127, 255, cv2.THRESH_BINARY) # ret, thresh2 = cv2.threshold(flower, 127, 255, cv2.THRESH_BINARY_INV) # ret, thresh3 = cv2.threshold(flower, 127, 255, cv2.THRESH_TRUNC) # ret, thresh4 = cv2.threshold(flower, 127, 255, cv2.THRESH_TOZERO) # ret, thresh5 = cv2.threshold(flower, 127, 255, cv2.THRESH_TOZERO_INV) # # titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV'] # images = [flower, thresh1, thresh2, thresh3, thresh4, thresh5] # for i in range(6): # plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray') # plt.title(titles[i]) # plt.xticks([]), plt.yticks([]) # plt.show() # 图像平滑处理 moon = cv2.imread('timg.jpg') # cv_show('moon', moon) # # 均值滤波 # # 简单的平均卷积操作 # blur = cv2.blur(moon, (3, 3)) # cv_show('blur', blur) # # 方框滤波 # # 基本和均值一样,可以选择归一化 # box = cv2.boxFilter(moon, -1, (3, 3), normalize=True) # normalize为True时和均值滤波一致,而为False时超过255的值设为255 # cv_show('box', box) # # 高斯滤波 # # 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的 # aussian = cv2.GaussianBlur(moon, (5, 5), 1) # cv_show('aussian', aussian) # # 中值滤波 # # 相当于中值代替 # median = cv2.medianBlur(moon, 5) # 中值滤波 # cv_show('median', median) # 展示所有 blur = cv2.blur(moon, (3, 3)) aussian = cv2.GaussianBlur(moon, (5, 5), 1) median = cv2.medianBlur(moon, 5) res = np.vstack((blur, aussian, median)) cv_show('median vs average', res)
只能输入灰度图