实例所用的函数可在另一篇文章查询: https://www.cnblogs.com/Zhouce/p/17867164.html
1、图像读取
1 import cv2 # 引入opencv库
2 import numpy as np # 引入numpy库
3 fn = "car.png"
4
5 # img_color = cv2.imread(fn,1) # 上下两种方式都是读取彩色图像的flag
6 img_color = cv2.imread(fn,cv2.IMREAD_COLOR)
7
8 # img_gray = cv2.imread(fn,0) # 上下两种方式都是读取灰色图像的flag
9 img_gray = cv2.imread(fn,cv2.IMREAD_GRAYSCALE)
10
11 # 获取图像大小
12 sp1 = img_color.shape
13 sp2 = img_gray.shape
14 print("img_color.shape = ",sp1) # 彩色图像三通道 : 高度(图像行数)、宽度(图像列数)、通道数
15 print("img_gray.shape = ",sp2) # 灰色图像两通道 : 高度(图像行数)、宽度(图像列数)
16
17 # # 创建窗口
18 # cv2.namedWindow('img_color')
19 # cv2.namedWindow('img_color')
20
21 # 显示图像
22 cv2.imshow('img_color',img_color)
23 cv2.imshow('img_gray',img_gray)
24
25 cv2.waitKey(0)
2、保存图像
1 import cv2
2
3 fn = "car.png"
4 img_gray = cv2.imread(fn,0)
5 sp1 = img_gray.shape
6
7 cv2.imshow('image',img_gray) # 显示灰色图像
8 cv2.imwrite("car_gray.png",img_gray) # 在当前文件夹下保存图像
9
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()
3、缩放图像
# 第一种缩放图像的方法
1 import cv2
2 import numpy as np
3
4 fn = 'car.png'
5
6 img = cv2.imread(fn)
7
8 #指定目标图像的宽高
9 #图像不失真
10
11 # img_down_0 = cv2.resize(img,(150,150)) #
12 img_up_0 = cv2.resize(img,(500,500))
13
14 # cv2.imshow('carimg',img_down_0)
15 cv2.imshow('carimg',img_up_0)
16
17 # 图像失真
18 img_distortion = cv2.resize(img, (200, 300))
19 cv2.imshow('img_distortion', img_distortion)
20 cv2.waitKey(0)
21 cv2.destroyAllWindows()
# 第二种缩放图像的方法
1 import cv2
2 import numpy as np
3
4 fn = 'car.png'
5
6 img = cv2.imread(fn)
7
8 # 指定图像的比例因子(需要指出的是dsize必须要传参,None)
9 # 图像不失真
10 img_down_1 = cv2.resize(img,None,fx=0.75,fy=0.75)
11 img_up_1 = cv2.resize(img,None,fx=1.2,fy=1.2)
12 cv2.imshow('img_down-No distortion1', img_down_1)
13 cv2.imshow('img_up-No distortion1', img_up_1)
14 # 图像失真
15 img_distortion1 = cv2.resize(img,None,fx=0.75,fy=1.2)
16 cv2.imshow('img distortion1',img_distortion1)
17 cv2.waitKey(0)
18 cv2.destroyAllWindows()
1 import cv2
2 import numpy as np
3
4 fn = 'car.png'
5
6 img = cv2.imread(fn)
7
8 # 指定图像缩放方法
9 img_scale0 = cv2.resize(img, (150, 150), interpolation=cv2.INTER_AREA)
10 img_scale1 = cv2.resize(img, (450, 450), interpolation=cv2.INTER_CUBIC)
11 img_scale2 = cv2.resize(img, (450, 450), interpolation=cv2.INTER_LINEAR)
12 img_scale3 = cv2.resize(img, (150, 150), interpolation=cv2.INTER_NEAREST)
13
14 cv2.imshow('img_scale0',img_scale0)
15 cv2.imshow('img_scale1',img_scale1)
16 cv2.imshow('img_scale2',img_scale2)
17 cv2.imshow('img_scale3',img_scale3)
18
19 cv2.waitKey(0)
20 cv2.destroyAllWindows()
21 # 可选值包括cv2.INTER_NEAREST、cv2.INTER_LINEAR双线性插值法(默认)、cv2.INTER_AREA、cv2.INTER_CUBIC、cv2.INTER_LANCZOS4等
4、图像裁剪
1 import cv2
2
3 fn = "car.png"
4 car = cv2.imread(fn)
5 print(car.shape)
6
7 # 裁剪图像
8 cropped_car = car[25:190,50:300] # image[start_row : end_row , start_rol : end_rol]
9 cv2.imshow('original',car)
10 cv2.imshow('cropped',cropped_car)
11 cv2.waitKey(0)
12 cv2.destroyAllWindows()
5、色彩空间转换
1 # 颜色空间转换 cv2.cvtColor(img,flags)
2
3 import cv2
4 import matplotlib.pyplot as plt
5
6 img = cv2.imread("car.png")
7 # 利用plot函数画出图像并放置在一个窗口中显示
8 plt.subplot(3,3,1)
9 plt.imshow(img)
10 plt.axis('off')
11 plt.title('BGR')
12
13 # BGR变换RGB
14 img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
15 plt.subplot(3,3,2)
16 plt.imshow(img_RGB)
17 plt.axis('off')
18 plt.title('RGB')
19
20 # 原图变换灰度图
21 img_GRAY = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
22 plt.subplot(3, 3, 3)
23 plt.imshow(img_GRAY)
24 plt.axis('off')
25 plt.title('GRAY')
26
27 # 变换HSV空间
28 img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
29 plt.subplot(3, 3, 4)
30 plt.imshow(img_HSV)
31 plt.axis('off')
32 plt.title('HSV')
33
34 # 变换YCrCb空间
35 img_YcrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
36 plt.subplot(3, 3, 5)
37 plt.imshow(img_YcrCb)
38 plt.axis('off')
39 plt.title('YcrCb')
40
41 # 变换HLS空间
42 img_HLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
43 plt.subplot(3, 3, 6)
44 plt.imshow(img_HLS)
45 plt.axis('off')
46 plt.title('HLS')
47
48 # 变换XYZ空间
49 img_XYZ = cv2.cvtColor(img, cv2.COLOR_BGR2XYZ)
50 plt.subplot(3, 3, 7)
51 plt.imshow(img_XYZ)
52 plt.axis('off')
53 plt.title('XYZ')
54
55 # 变换LAB空间
56 img_LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
57 plt.subplot(3, 3, 8)
58 plt.imshow(img_LAB)
59 plt.axis('off')
60 plt.title('LAB')
61
62 # 变换YUV空间
63 img_YUV = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
64 plt.subplot(3, 3, 9)
65 plt.imshow(img_YUV)
66 plt.axis('off')
67 plt.title('YUV')
68 plt.show()
(2)
1 import cv2
2
3 img = cv2.imread('car.png')
4
5 img_BGR = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
6 img_HSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
7
8 cv2.imshow('origin_img',img)
9 cv2.imshow('BGR_img',img_BGR)
10 cv2.imshow('HSV_img',img_HSV)
11
12 cv2.waitKey(0)
13 cv2.destroyAllWindows()