相对于RGB空间,HSV空间能够非常直观的表达色彩的明暗,色调,以及鲜艳程度,方便进行颜色之间的对比.
H - 色调(主波长)。
S - 饱和度(纯度/颜色的阴影)。
V值(强度)。
显示HSV图像:
import cv2 #opencv读取的格式是BGR
img=cv2.imread('D:\cat.jpg')
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv", hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
ret, dst = cv2.threshold(src, thresh, maxval, type)
参数说明:
src: 输入图,只能输入单通道图像,通常来说为灰度图
dst: 输出图
thresh: 想要设定阈值
maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值,一般为255
type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV
cv2.THRESH_BINARY:超过阈值部分取maxval(最大值),否则取0
cv2.THRESH_BINARY_INV:是THRESH_BINARY的反转,超过阈值部分取0),否则取最大值
cv2.THRESH_TRUNC:大于阈值部分设为阈值,否则不变
cv2.THRESH_TOZERO:大于阈值部分不改变,否则设为0
cv2.THRESH_TOZERO_INV:是THRESH_TOZERO的反转,小于阈值部分不改变,否则设为0
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
img=cv2.imread('D:\cat.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#五种不同的阈值操作
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, 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()
均值滤波:简单的平均卷积操作,(3,3)为卷积核的大小
blur = cv2.blur(img, (3, 3))
方框滤波:基本和均值一样,当normalize=True为归一化,效果和均值滤波一样,当normalize=False越界后取最大值255
box = cv2.boxFilter(img,-1,(3,3), normalize=True)
高斯滤波:高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的。
aussian = cv2.GaussianBlur(img, (5, 5), 1)
中值滤波:相当于用中值代替,基本思想是用像素点邻域灰度值的中值来代替该像素点的灰度值
median = cv2.medianBlur(img, 5)
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
#原图
img = cv2.imread('D:\zaoying.jpg')
#均值滤波
blur = cv2.blur(img, (3, 3))#(3,3)为卷积核的大小
#方框滤波
box = cv2.boxFilter(img,-1,(3,3), normalize=True)#normalize=True)即归一化后效果和均值滤波一样
#高斯滤波
aussian = cv2.GaussianBlur(img, (5, 5), 1)
#中值滤波
median = cv2.medianBlur(img, 5)
#图像拼接
res = np.hstack((aussian,median))
#展示高斯滤波和中值滤波
cv2.imshow('aussian vs median', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
对比效果:
进行腐蚀操作的图像一般都是二值的,腐蚀操作的原理是利用一个内核对图像进行卷积(扫描),内核中有一个点被定义为锚点,然后提取内核覆盖区域的像素最小值(黑色)来替换锚点位置的像素值,所以扫描过后黑色变多。
import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
img = cv2.imread('D:\Benwei.png')
cv2.imshow('img', img)
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel:卷积核的大小
iterations:迭代次数
原图:
效果:
和腐蚀一样,也是相对于白色来说,膨胀就是像素值高的(白色)变多了(膨胀了),和腐蚀相反,膨胀是提取内核区域的最大值(白色)来替换锚点位置的像素值。
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
dige_dilate = cv2.dilate(img,kernel,iterations = 1)
cv2.imshow('dige_dilate', dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
开运算和闭运算其实就是把前面的腐蚀和膨胀总结在一起,只不过执行的顺序不一样,开运算是先腐蚀后膨胀,闭运算是先膨胀后腐蚀。开运算为cv2.MORPH_OPEN,闭运算为cv2.MORPH_CLOSE。
开运算:
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('opening', opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
闭运算:
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('closing', closing)
cv2.waitKey(0)
cv2.destroyAllWindows()
梯度运算实际上是图像膨胀减去图像腐蚀后的结果,最终我们得到的是一个类似于图像轮廓的图形。
import cv2 #opencv读取的格式是BGR
import numpy as np
pie = cv2.imread('D:\pie.png')
kernel = np.ones((7,7),np.uint8)
dilate = cv2.dilate(pie,kernel,iterations = 5)
erosion = cv2.erode(pie,kernel,iterations = 5)
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('gradient', gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
礼帽相当于用原始输入减去开运算得到的结果,黑帽是闭运算减去原始输入的到的结果。
礼帽:
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()
黑帽:
import cv2 #opencv读取的格式是BGR
import numpy as np
img = cv2.imread('D:\Benwei.png')
kernel = np.ones((3,3),np.uint8)
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat )
cv2.waitKey(0)
cv2.destroyAllWindows()