图像基本操作

图片的读取

1 import cv2
2 
3 img = cv2.imread('geeks.png', cv2.IMREAD_GRAYSCALE)     # 读取灰度图片
4 cv2.imshow('my test', img)
5 cv2.waitKey(0)      # 等待,0表示按下任意键终止
6 cv2.destroyAllWindows()
7 print(img.shape)
8 print(img.size)     # 查看像素点个数
View Code

 

视频的读取

 1 import cv2
 2 
 3 vc = cv2.VideoCapture('psychological.mp4')
 4 # 先判断是否能打开,如果能则循环读取每一帧数据,之后对每一张图片进行一些处理
 5 if vc.isOpened():
 6     open, frame = vc.read()
 7 else:
 8     open = False
 9 while open:
10     ret, frame = vc.read()
11     if frame is None:
12         break
13     if ret:
14         # 如果用一些老的训练好的模型,建议使用兼容老模型的bgr
15         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
16         cv2.imshow('result', gray)
17         if cv2.waitKey(100) % 0xFF == 27:
18             break
19 vc.release()
20 cv2.destroyAllWindows()
View Code

 

读取感兴趣的窗口

1 import cv2
2 
3 img = cv2.imread('geeks.png')
4 img2 = img[20:200, 20:200]      # 截取Y、X轴的范围
5 cv2.imshow('test', img2)
6 cv2.waitKey(0)
7 cv2.destroyAllWindows()
View Code

 

通道分离与合并

 1 import cv2
 2 img = cv2.imread('geeks.png')
 3 b, g, r = cv2.split(img)    # 通道分开
 4 img1 = cv2.merge((b, g, r))     # 通道合并
 5 cv2.imshow('test1', img1)
 6 cop_img = img1.copy()
 7 cop_img[:, :, [0, 1]] = 0   # 只留下红色通道
 8 cv2.imshow('test1_only_r', cop_img)
 9 cv2.waitKey(0)
10 cv2.destroyAllWindows()
View Code

 

边界填充

 1 import cv2
 2 import matplotlib.pyplot as plt
 3 
 4 top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
 5 img = cv2.imread('geeks.png')
 6 replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
 7 reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT)
 8 reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
 9 wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
10 constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_CONSTANT, value=0)
11 
12 plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
13 plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
14 plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
15 plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
16 plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
17 plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
18 plt.show()
View Code

 

图像数值计算与融合

 1 import cv2
 2 
 3 # 对图像的像素点直接进行相加减
 4 # img = cv2.imread('default.png')
 5 # img_add = img + 10
 6 # cv2.imshow('test', img)
 7 # cv2.waitKey(0)
 8 # cv2.destroyAllWindows()
 9 
10 img_geeks = cv2.imread('geeks.png')
11 img_default = cv2.imread('default.png')
12 print('img_geeks_size', img_geeks.shape)
13 print('img_default_size', img_default.shape)
14 img_geeks_resize = cv2.resize(img_geeks, (382, 420))
15 print('img_geeks_resize', img_geeks_resize.shape)
16 # 将两张相同大小且类型相同的图像融合,第一张图淡化到30%,第二张图90%,注意两图大小保持一致
17 res = cv2.addWeighted(img_geeks_resize, 0.3, img_default, 0.9, 0)
18 cv2.imshow('420*382', res)
19 cv2.waitKey(0)
20 cv2.destroyAllWindows()
View Code

 

posted @ 2021-09-12 18:50  Ocean允许访问  阅读(35)  评论(0)    收藏  举报