openCV学习--安装相应的包和体验opencv
安装:
安装命令 检查是否安装成功命令
pip install opencv-python import cv2
pip install tensorflow import tensorflow
pip install keras import keras
注:
tensorflow 目前不支持3.7 (19.10.5)
计算机视觉基础:
OpenCV 图像处理:
1 import cv2 2 import matplotlib.pyplot as plt 3 4 #读取图片 5 image = cv2.imread("d:/images/image.jpg") 6 print("图片的宽度 width {} pixels".format(image.shape[1])) 7 print("图片的高度 height {} pixels".format(image.shape[0])) 8 print("图片的通道数 num {}".format(image.shape[2])) #因为是彩色图片所以是3通道 9 10 #下面用plt 显示图片 11 if 0: 12 plt.imshow(image) 13 plt.axis('off') #关闭坐标轴 14 plt.show() 15 if 0: 16 # 注:此时图片显示是和原图不一样的,因为 17 #opencv 处理的时候是按 bgr 来处理的,而图片一般都是rgb 18 #可以通过如下代码解决 19 pass 20 new_image =cv2.cvtColor(image,cv2.COLOR_BGR2RGB) #将bgr格式 换成我们通常的rgb格式 21 22 plt.imshow(new_image) 23 plt.axis('off') #关闭坐标轴 24 plt.show() 25 26 cv2.imwrite("d:/images/new_image.png",image) #保存image 此时和原图一样 27 cv2.imwrite("d:/images/new_image.png",new_image) #保存image 此时和原图不一样 28 29 #注意:读入和写入 opencv 都是按照bgr 的方式,而图片本身是rgb 格式的。
OpenCV 图像处理过程中RGB转化的理解:
总结:在OpenCV处理的过程中操作的数组的三个值分别为 B G R , 处理过程显示是按 R G B 显示的.
操作图像的像素值 ,以及分割图片:
1 import cv2 2 import matplotlib.pyplot as plt 3 4 #读入图片 5 image = cv2.imread("d:/images/image.jpg") 6 (h,w,c) = image.shape #height width count 7 8 #读取图片中任意一点的像素值 9 (b,g,r) = image[0,0] # 10 # print(image[0,0]) 11 12 #修改像素的值 13 if 0: 14 for i in range(100): 15 for j in range(100): 16 image[i,j] = (0,0,255) 17 18 plt.imshow(image) #虽然此时显示的图片左上角变为了 蓝色,但是实际图片仍然是红色 19 plt.show() 20 21 22 cv2.imwrite("d:/images/new_image02.png",image) 23 24 #读取图片左上角的部分 25 cx ,cy = (w//2,h//2) 26 def show(image): 27 plt.imshow(image) 28 plt.axis('off') 29 plt.show() 30 if 0: 31 lf_top = image[:cy,:cx] 32 lf_btm = image[cy:,:cx] 33 rt_top = image[:cy,cx:] 34 rt_btm = image[cy:,cx:] 35 show(lf_top) 36 show(lf_btm) 37 show(rt_top) 38 show(rt_btm) 39 pass 40 41 #修改左上部分为 红色! 42 if 1: 43 image[:cy,:cx] =(0,0,255) #修改的是r 44 show(image) 45 cv2.imwrite("d:/images/new_image_03.jpg",image)
openCV 处理图片框架:(此时图片显示完全一致)
1 import cv2 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 def show(image): 6 ''' 7 显示图片 8 ''' 9 plt.imshow(image) 10 plt.axis('off') #隐藏坐标轴 11 plt.show() 12 pass 13 14 def imread(path): 15 image = cv2.imread(path) 16 image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)#将bgr转为rgb 17 return image 18 pass 19 def imwrite(path,image): 20 image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR) 21 cv2.imwrite(path,image) 22 23 image = imread("d:/images/image.jpg") 24 h,w,c = image.shape 25 cx,cy = (w//2,h//2) 26 image[:cy,:cx] = [0,0,255] 27 show(image) 28 imwrite("d:/images/new_02.png",image)
图片的平移(Translation):
1 import cv2 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 def show(image): 6 ''' 7 显示图片 8 ''' 9 plt.imshow(image) 10 plt.axis('off') #隐藏坐标轴 11 plt.show() 12 pass 13 14 def imread(path): 15 image = cv2.imread(path) 16 image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)#将bgr转为rgb 17 return image 18 pass 19 def imwrite(path,image): 20 image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR) 21 cv2.imwrite(path,image) 22 23 image = imread("d:/images/image.jpg") 24 M = np.float32([[1,0,250],[0,1,500]]) #水平250 垂直500 25 # print(M) 26 #平移图片 27 shifted = cv2.warpAffine(image,M,(image.shape[1],image.shape[0])) 28 show(shifted)
图片的旋转(Rotation):
1 import cv2 2 import matplotlib.pyplot as plt 3 import numpy as np 4 5 def show(image): 6 ''' 7 显示图片 8 ''' 9 plt.imshow(image) 10 plt.axis('off') #隐藏坐标轴 11 plt.show() 12 pass 13 14 def imread(path): 15 image = cv2.imread(path) 16 image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)#将bgr转为rgb 17 return image 18 pass 19 def imwrite(path,image): 20 image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR) 21 cv2.imwrite(path,image) 22 23 image = imread("d:/images/image.jpg") 24 h,w = image.shape[:2] 25 cx,cy = w//2,h//2 26 27 #旋转图片(可以进行缩放) 28 # getRotationMatrix2D 参数:旋转中心,角度(默认逆时针),缩放因子 29 M = cv2.getRotationMatrix2D((cx,cy),45,1) 30 31 rotated = cv2.warpAffine(image,M,(w,h)) 32 show(rotated)
图片的resize:
1 from imutils import * #这是封装到一个模块imutils 2 """ 3 imutils 中的内容 : 4 import cv2 5 import matplotlib.pyplot as plt 6 import numpy as np 7 8 def show(image): 9 ''' 10 显示图片 11 ''' 12 plt.imshow(image) 13 plt.axis('off') #隐藏坐标轴 14 plt.show() 15 16 def imread(path): 17 image = cv2.imread(path) 18 image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)#将bgr转为rgb 19 return image 20 21 22 def imwrite(path,image): 23 image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR) 24 cv2.imwrite(path,image) 25 """ 26 27 image = imread("d:/images/image.jpg") 28 show(image) 29 print(image.shape) 30 31 width = 150 32 height = 150 33 new_iamge = cv2.resize(image,(width,height)) 34 show(new_iamge)