一、数值计算
#数值计算
img_flower = cv2.imread('D:/flower.jpg')
img_flower2 = img_flower + 10 #每个像素点上都加10
img_flower[:5,:,0]#只取前五行
array([[255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255], [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
img_flower2[:5,:,0] #像素点的值的范围是0—255
array([[9, 9, 9, ..., 9, 9, 9], [9, 9, 9, ..., 9, 9, 9], [9, 9, 9, ..., 9, 9, 9], [9, 9, 9, ..., 9, 9, 9], [9, 9, 9, ..., 9, 9, 9]], dtype=uint8)
二、图像融合
img_flower.shape #(500,500,3)
img_fish = cv2.imread('D:/fish.jpg')
img_fish.shape #(683,1024,3)
img_fish = cv2.resize(img_fish,(500,500)) #变换图像大小
img_fish.shape #(500,500,3)
result = cv2.addWeighted(img_flower,0.4,img_fish,0.6,gamma = 0) #图片后面的数字代表每张图片的权重,gamma为修正系数,不需要修正设置为0
plt.imshow(result)
res = cv2.resize(img_flower,(0,0),fx = 1,fy = 3) #通过改变宽和高的倍数来调整图像的大小(fx表示宽,fy表示高)
plt.imshow(res)
三、图像阈值
ret,det = cv2.threshold(src,thresh,maxval,type)
1.src:输入图,只能输入单通道图像,通常来说为灰度图
2.dst:输出图
3.thresh:阈值
4.maxval:当像素值超过了阈值(或小于阈值,根据type来决定),所赋予的值
5.type:二值化操作的类型,包含以下5种类型:cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV
6.cv2.THRESH_BINARY 超过阈值部分取maxval(最大值),否则取0
7.cv2.THRESH_BINARY_INV THRESH_BINARY的反转
8.THRESH_TRUNC 大于阈值部分设为阈值,否则不变
9.cv2.THRESH_TOZERO 大于阈值部分不改变,否则设为0
10.cv2.THRESH_TOZERO_INV THRESH_TOZERO的反转
img_grey = cv2.imread('D:/flower.jpg',cv2.IMREAD_GRAYSCALE)
ret,thresh1 = cv2.threshold(img_grey,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img_grey,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img_grey,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img_grey,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img_grey,127,255,cv2.THRESH_TOZERO_INV)
titles = ['original image','binary','binary_inv','trunc','tozero','tozero_inv']
images = [img_grey,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
四、图像平滑处理
对图像数据进行各种滤波操作
原图展示:
import cv2
imgs = cv2.imread('D:/zhouyu.jpg')
cv2.imshow('imgs',imgs)
cv2.waitKey(0)
cv2.destroyAllWindows()
(1)均值滤波
#均值滤波
#简单的平均卷积操作
blur = cv2.blur(imgs,(3,3))
cv2.imshow('blur',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
(2)方框滤波
#方框滤波
#基本和均值一样,可以选择归一化
box = cv2.boxFilter(imgs,-1,(3,3),normalize = True)
cv2.imshow('box',box)
cv2.waitKey(0)
cv2.destroyAllWindows()
#方框滤波
#基本和均值一样,可以选择归一化,容易越界
box = cv2.boxFilter(imgs,-1,(3,3),normalize = False)
cv2.imshow('box',box)
cv2.waitKey(0)
cv2.destroyAllWindows()
(3)高斯滤波
#高斯滤波
#高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的
aussian = cv2.GaussianBlur(imgs,(5,5),1)
cv2.imshow('aussian',aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
(4)中值滤波
#中值滤波
median = cv2.medianBlur(imgs,5)
cv2.imshow('median',median)
cv2.waitKey(0)
cv2.destroyAllWindows()
#展示所有图片
import numpy as np
res = np.hstack((blur,aussian,median))
print(res)
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
五、形态学操作
(一)腐蚀操作
原图展示:
import cv2
img_fushi = cv2.imread('D:/fushi2.jpg')
cv2.imshow('img_fushi',img_fushi)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img_fushi,kernel,iterations = 1)
cv2.imshow('erosion',erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
(二)膨胀操作
import cv2
img_fushi = cv2.imread('D:/fushi2.jpg')
cv2.imshow('img_fushi',img_fushi)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img_fushi,kernel,iterations = 1)
cv2.imshow('erosion',erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel = np.ones((3,3),np.uint8)
img_dilate = cv2.dilate(erosion,kernel,iterations = 2)
cv2.imshow('img_dilate',img_dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
(三)开运算与闭运算
开运算:先腐蚀,再膨胀
毕运算:先膨胀,再腐蚀
#开运算
import cv2
import numpy as np
img = cv2.imread('D:/fushi2.jpg')
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
cv_show('opening',opening)
#闭运算
import cv2
import numpy as np
img = cv2.imread('D:/fushi2.jpg')
kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
cv_show('closing',closing)
六、梯度运算
梯度 = 膨胀 - 腐蚀
import cv2
import numpy as np
img = cv2.imread('D:/fushi2.jpg')
kernel = np.ones((5,5),np.uint8)
dilate = cv2.dilate(img,kernel,iterations = 5) #膨胀操作
erosion = cv2.erode(img,kernel,iterations = 5) #腐蚀操作
res = np.hstack((dilate,erosion))
cv_show('res',res)
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
cv_show('gradient',gradient)