import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img = cv2.imread('revolte.jpg')
img # 读取的是array 格式
array([[[240, 243, 255],[239, 242, 255],[238, 241, 255],...,def cv_show(name,image):cv2.imshow(name,image)cv2.waitKey(0)cv2.destroyAllWindows()
cv_show('image',img) #定义个函数不用每次都写这么多行
img.shape#常用的属性
(183, 275, 3)
###灰度图的读取
img= cv2.imread('revolte.jpg',cv2.IMREAD_GRAYSCALE)
cv_show('Gray',img)
cv2.imwrite('gray.jpg',img)
True
视频的处理
vc = cv2.VideoCapture('AMZ.mp4')
while open:ret,frame = vc.read()#返回两个值if frame is None:breakif ret == True:gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)cv2.imshow('gray result',gray)if cv2.waitKey(1)&0xFF ==27:break
vc.realse()
cv2.destroyAllWindows() ^
颜色通道的提取
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r)) #合并通道
#只保留B
cur_img = img.copy()
cur_img[:,:,1]=0
cur_img[:,:,2]=0
cv_show('B',cur_img)
边界的填充(padding),有复制法(复制最边缘的像素),反射法,外包装法,常量法
top_size,bottom_size,left_size,right_size=(50,50,50,50)
replicate = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_WRAP)
const =wrap = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_CONSTANT,value = 0)
plt.subplot(221),plt.imshow(img,'gray'),plt.title('Original')
plt.subplot(222),plt.imshow(replicate,'gray'),plt.title('replicate')
plt.subplot(223),plt.imshow(reflect,'gray'),plt.title('replicate101')
plt.subplot(224),plt.imshow(const,'gray'),plt.title('const')
数值计算
img2 = img+10
#可以切片进行计算,numpy计算越界取余
#不同于cv2.add 越界取最大
图像融合
img2 = cv2.imread('mito.jpg')
img2.shape
(159, 317, 3)
img.shape
(159, 317, 3)
img = cv2.resize(img,(317,159))
img = cv2.flip(img, 1)
res = cv2.addWeighted(img,0.6,img2,0.4,0)
plt.imshow(res)