1、读入
img = cv2.imread(‘a.jpg’)
以BGR的格式读入图像,matoplib是以RGB的方式读入的
2、图像的显示
#设置可以更改窗口的大小
cv2.namedWindow(‘present img’,cv2.WINDOW_NORMAL)
#以BGR的方式显示图像
cv2.imshow(‘present img’,img)
#让图像窗口停留
cv2.waitKey()
#清理掉内存中无用的缓存的图片
cv2.destroyAllWindows()
3、图像灰度化处理
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
4、建立一个与img大小一样但是像素值全为0的图像
dst = np.zeros_like(img)
5、图像卷积运算
#定义卷积核
kernel = [[-1,0,0],[0,1,0],[0,0,0]]
kernel = np.array(kernel)
#其中gray表示灰度图像
#-1表示的是图像的深度,也就是保存像素值的二进制位数
#kernel指的是卷积核
output = cv2.filter2D(gray,-1,kernel)
6、
#OpenCv通过更改图像像素值,来为图像添加特效
import cv2
import numpy as np
img2 = cv2.imread('1.jpg')
row ,cols = img2.shape[:2]
dst = np.zeros((row,cols,3),dtype = 'uint8')
for i in range(row):
for j in range(col):
R= 0.393*img2[i,j][2] + 0.769*img2[i,j][1] + 0.189*img2[i,j][0]
G= 0.349*img2[i,j][2] + 0.686*img2[i,j][1] + 0.168*img2[i,j][0]
B = 0.272*img2[i,j][2] + 0.534*img2[i,j][1] + 0.131*img2[i,j][0]
dst[i,j][2] = min(R,255)
dst[i,j][1] = min(G,255)
dst[i,j][0] = min(B,255)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()