边界填充
cv2.BORDER_REPLICATE) #复制法:复制最边缘像素 aaa|abcde|eee
cv2.BORDER_REFLECT) #反射法:对图像的两边进行复制 cba|abcde|edc
cv2.BORDER_REFLECT_101) #反射法:以最边缘像素为轴,对称复制 dcb|abcde|dcb
cv2.BORDER_WRAP) #外包装法:用另一边的像素填充 cde|abcde|abc
cv2.BORDER_CONSTANT,value=0) #常量法:填充常数
例:
img=cv2.imread('dawn-sun-mountain-landscape-preview.jpg')
img1=cv2.imread('california-sunset-dusk-sky-preview.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGRA2GRAY)
b,g,r=cv2.split(img)
img=cv2.merge((r,g,b))
t_s,b_s,l_s,r_s = 100,100,100,100
replicate = cv2.copyMakeBorder(img,t_s,b_s,l_s,r_s,cv2.BORDER_REPLICATE) #复制法:复制最边缘像素 aaa|abcde|eee
reflect = cv2.copyMakeBorder(img,t_s,b_s,l_s,r_s,cv2.BORDER_REFLECT) #反射法:对图像的两边进行复制 cba|abcde|edc
reflect101 = cv2.copyMakeBorder(img,t_s,b_s,l_s,r_s,cv2.BORDER_REFLECT_101) #反射法:以最边缘像素为轴,对称复制 dcb|abcde|dcb
wrap = cv2.copyMakeBorder(img,t_s,b_s,l_s,r_s,cv2.BORDER_WRAP) #外包装法:用另一边的像素填充 cde|abcde|abc
constant = cv2.copyMakeBorder(img,t_s,b_s,l_s,r_s,cv2.BORDER_CONSTANT,value=0) #常量法:填充常数
a=['original','replicate','reflect','reflect101','wrap','constant']
b=[img,replicate,reflect,reflect101,wrap,constant]
for i in range(6):
plt.subplot(231+i),plt.imshow(b[i],'gray'),plt.title(a[i])
plt.show()
#改变图像大小
img1=cv2.resize(图像数据,(宽,高))
img=cv2.resize(图像数据,(0,0),fx=宽放大倍数,fy=高放大倍数)
#将图像按权重叠加
img0=cv2.addWeighted(图像数据,权重,图像数据,权重,gamma(亮度))
#图像连接
img2 = np.hstack([图像数据,图像数据,图像数据])
例:
img1=cv2.resize(img1,(728,485))
img0=cv2.addWeighted(img,0.5,img1,0.5,0)
img=cv2.resize(img,(0,0),fx=0.5,fy=0.5)
img0=cv2.resize(img0,(0,0),fx=0.5,fy=0.5)
img1=cv2.resize(img1,(0,0),fx=0.5,fy=0.5)
img2 = np.hstack([img,img0,img1])
cv2.imshow('',img2)
cv2.waitKey(0)
图像阈值
#ret,dst = cv2.threshold(输入图(只能输入单通道图像,通常为灰度图),阈值,当像素超过阈值(或小于阈值,由type决定)所赋予的值,二值化操作类型)
#cv2.THRESH_BINARY 大于阈值取所给值,否则取0
#cv2.THRESH_BINARY_INV 与cv2.THRESH_BINARY相反
#cv2.THRESH_TRUNC 大于阈值取MAX,否则不变
#cv2.THRESH_TOZERO 大于阈值不变,否则取0
#cv2.THRESH_TOZERO_INV 与THRESH_TOZERO_INV相反
例:
ret,a = cv2.threshold(gray,100,220,cv2.THRESH_BINARY)
ret,b = cv2.threshold(gray,100,220,cv2.THRESH_BINARY_INV)
ret,c = cv2.threshold(gray,100,220,cv2.THRESH_TRUNC)
ret,d = cv2.threshold(gray,100,220,cv2.THRESH_TOZERO)
ret,e = cv2.threshold(gray,100,220,cv2.THRESH_TOZERO_INV)
m=['ORIGINAL','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
n=[gray,a,b,c,d,e]
for i in range(6):
plt.subplot(231+i),plt.imshow(n[i],'gray'),plt.title(m[i])
plt.xticks([]),plt.yticks([])
plt.show()
图像平滑处理
#均值滤波,简单的平均卷积操作
blur=cv2.blur(图像数据,区域(3,3))
#方框滤波,可以选择归一化,归一化基本和均值一样,不归一化数值容易越界
box=cv2.boxFilter(图像数据,颜色通道(-1:取全部),区域(3,3),normalize=是否归一化(True))
#高斯滤波,卷积核里的数值满足高斯分布,更重视中间的
aussian=cv2.GaussianBlur(图像数据,区域(5,5),1)
#中值滤波,相当于用中值代替
median=cv2.medianBlur(图像数据,区域(3))
形态学-腐蚀操作
yuan=cv2.imread("1.jpg")
kernel=np.ones((5,5),np.uint8)
erosion1=cv2.erode(图像数据,kernel,iterations=执行次数)
例:
yuan=cv2.imread("1.jpg")
kernel=np.ones((5,5),np.uint8)
erosion1=cv2.erode(yuan,kernel,iterations=1)
erosion2=cv2.erode(yuan,kernel,iterations=2)
erosion3=cv2.erode(yuan,kernel,iterations=3)
da=np.hstack([yuan,erosion1,erosion2,erosion3])
cv2.imshow('',da)
cv2.waitKey(0)
形态学-膨胀操作
kernel=np.ones((5,5),np.uint8)
dilate=cv2.dilate(图像数据,kernel,iterations=执行次数)
例:
kernel=np.ones((5,5),np.uint8)
dilate1=cv2.dilate(erosion3,kernel,iterations=1)
dilate2=cv2.dilate(erosion3,kernel,iterations=2)
dilate3=cv2.dilate(erosion3,kernel,iterations=3)
da1=np.hstack([dilate1,dilate2,dilate3,yuan])
cv2.imshow('',da1)
cv2.waitKey(0)
开运算与闭运算
kernel=np.ones((5,5),np.uint8)
#开运算:先腐蚀后膨胀
open1=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
#闭运算:先膨胀后腐蚀
close1=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
a=np.hstack([open1,close1])
cv2.imshow('',a)
cv2.waitKey(0)