1 import cv2 as cv
2 import numpy as np
3 from matplotlib import pyplot as plt
4
5 if __name__ == '__main__':
6
7 #读取图片
8 img = cv.imread('image/apple.jpg')
9
10 #保存图片 cv.imwrite('路径',img)
11
12 '''显示图片
13 cv.imshow('apple',img)
14 img3 = cv.imread('路径',0) #灰度显示
15 cv.waitKey(0)
16 cv.destroyAllWindows()
17 '''
18 # plt显示图片按BGR读取的,需转换成RGB模式
19 img2 = cv.cvtColor(img,cv.COLOR_BGR2RGB)
20 plt.imshow(img2)
21 plt.show()
22
23 #折线绘图
24 x = np.arange(2,20)
25 y = 2*x+np.random.randint(5,20,18)
26 plt.plot(x,y,'*-',color='r') #折线
27 plt.show()
28 x = np.linspace(0,1,100)
29 y1 = np.power(x,0.5)
30 y2 = np.power(x,1.5)
31 plt.plot(x,y1,label='0.5')
32 plt.plot(x,y2,label='1.5')
33 plt.legend() #将每条折线区分开来
34 plt.xlabel('x')
35 plt.ylabel('y') #两条坐标轴命名
36 plt.grid() #显示表格
37 plt.xlim([0,1])
38 plt.ylim([0,1]) #设置坐标轴显示范围
39 plt.show()
40
41 #直方图统计绘图
42 a = np.random.randint(0,101,1000)
43 plt.hist(a,rwidth=0.9,color='g') #显示的是0-10,10-20等区间内数值的个数
44 bins = np.arange(-0.5,101,1)
45 plt.hist(a,bins) #显示0,1,2,3,,,的个数
46 plt.show()
47
48 #显示图像函数
49 def show(img):
50 #灰度2维,彩色3维
51 if img.ndim == 2:
52 plt.imshow(img,cmap='gray')
53 else:
54 plt.imshow(cv.cvtColor(img,cv.COLOR_BGR2RGB))