02_opencv_python_图像处理进阶
发布时间:2020-12-20 10:38:49 所属栏目:Python 来源:网络整理
导读:1??灰度图 import cv2 # opencv读取的格式是BGR import numpy as np import matplotlib.pyplot as plt # Matplotlib是RGB % matplotlib inline img =cv2.imread( ‘ cat.jpg ‘ )img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)img_gray.shape ? cv2.imsho
1??灰度图import cv2 # opencv读取的格式是BGR import numpy as np import matplotlib.pyplot as plt # Matplotlib是RGB %matplotlib inline img=cv2.imread(‘cat.jpg‘) img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img_gray.shape ? cv2.imshow("img_gray",img_gray) ? ? ? 2??HSV
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.imshow("hsv",hsv) cv2.waitKey(0) cv2.destroyAllWindows() ? ? 3??图像阈值?
参考上篇博客中的?基于颜色提出目标 # 1.将RGB转换成HSV色彩空间 hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # 2.定义数组,说明你要提取(过滤)的颜色目标 # 三通道,所以是三个参数 # 红色 lower_hsv_r = np.array([156,43,46]) upper_hsv_r = np.array([180,255,255]) # 3.进行过滤,提取,得到二值图像 mask_red = cv2.inRange(hsv,lower_hsv_r,upper_hsv_r) # 通道数是 1 3.1??ret,dst = cv2.threshold(src,thresh,maxval,type)
ret,thresh1 = cv2.threshold(img_gray,127,255,cv2.THRESH_BINARY) ret,thresh2 = cv2.threshold(img_gray,cv2.THRESH_BINARY_INV) ret,thresh3 = cv2.threshold(img_gray,cv2.THRESH_TRUNC) ret,thresh4 = cv2.threshold(img_gray,cv2.THRESH_TOZERO) ret,thresh5 = cv2.threshold(img_gray,cv2.THRESH_TOZERO_INV) titles = [‘Original Image‘,‘BINARY‘,‘BINARY_INV‘,‘TRUNC‘,‘TOZERO‘,‘TOZERO_INV‘] images = [img,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() ? ? 4??图像平滑(利用各种卷积核)img = cv2.imread(‘lenaNoise.png‘) # 椒盐噪音 cv2.imshow(‘img‘,img) cv2.waitKey(0) cv2.destroyAllWindows() # 均值滤波 # 简单的平均卷积操作 blur = cv2.blur(img,(3,3)) cv2.imshow(‘blur‘,blur) cv2.waitKey(0) cv2.destroyAllWindows() # 方框滤波 # 基本和均值一样,可以选择归一化 box = cv2.boxFilter(img,-1,3),normalize=True) cv2.imshow(‘box‘,box) cv2.waitKey(0) cv2.destroyAllWindows() # 高斯滤波 # 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的 aussian = cv2.GaussianBlur(img,(5,5),1) cv2.imshow(‘aussian‘,aussian) cv2.waitKey(0) cv2.destroyAllWindows() # 中值滤波 # 相当于用中值代替 median = cv2.medianBlur(img,5) # 中值滤波 cv2.imshow(‘median‘,median) cv2.waitKey(0) cv2.destroyAllWindows() # 展示所有的 res = np.hstack((blur,aussian,median)) #print (res) cv2.imshow(‘median vs average‘,res) cv2.waitKey(0) cv2.destroyAllWindows() ? ? 5??形态学-腐蚀操作(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |