Opencv基于python的基本操作(一)
一.OpenCV在win7系统的安装
测试代码 (读入图片并展示):
import cv2 as cv src= cv.imread("D:\opencv-test\EBK7%HI(P%)(FV1(`J034D3.png") cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) cv.imshow("input image",src) cv.waitKey(0) cv.destroyAllWindows()
二.图片的加载和保存
代码分析:
''' 图像加载与保存 ''' import numpy as np import cv2 as cv def video_demo(): #开视频 capture = cv.VideoCapture(r'D:\opencv-test\007、Tensorflow案例实战视频课程07 逻辑回归框架.mp4') #开摄像头 # capture = cv.VideoCapture(0) while(True): ret,frame= capture.read() # frame = cv.flip(frame,1) cv.imshow("video",frame) c = cv.waitKey(50) #ESC退出 if c == 27: break def get_image_info(image): print(type(image)) print(image.shape) print(image.size) print(image.dtype) pixel_data = np.array(image) print(pixel_data) src= cv.imread("D:\opencv-test\EBK7%HI(P%)(FV1(`J034D3.png") cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) # 展示图片 cv.imshow("input image",src) # get_image_info(src) #展示视频 # video_demo() cv.waitKey(0) #写入保存 # cv.imwrite(r'D:\opencv-test\result.png',src) cv.destroyAllWindows()
三.numpy修改图片像素等操作
代码实现:
'numpy修改图片像素等操作' import numpy as np import cv2 as cv import time def get_image_info(image): print(image.shape) height = image.shape[0] width= image.shape[1] channels = image.shape[2] print("height:%s,width:%s,channels:%s"%(height,width,channels)) #取反操作 for i in range(height): for j in range(width): for k in range(channels): m = image[i,j,k] image[i,j,k]=255-m cv.imshow('new image',image) #取反函数 def inverse(image): inv = cv.bitwise_not(image) cv.imshow('inv',inv) #创建新像片 def create_image(): #多通道 # img = np.zeros([400,400,3],np.uint8) # img[:,:,0] = np.ones([400,400])*255 # img[:,:,2] = np.ones([400,400])*255 #单通道 # img = np.zeros([400, 400,1], np.uint8) # img[:,:,0]=np.ones([400,400])*127 img = np.ones([400, 400, 1], np.uint8) img = img*127 cv.imshow('black',img) src= cv.imread(r"D:\opencv-test\9807657b93fbe2067c1f394af8fd674.png") # cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) t1 = time.time() # cv.imshow("input image",src) inverse(src) t2= time.time() print(t2-t1) # get_image_info(src) # create_image() cv.waitKey(0) cv.destroyAllWindows()
四.颜色空间:
HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model)。
这个模型中颜色的参数分别是:色调(H),饱和度(S),亮度(V)。
除了HSV,还有其他色彩空间:
代码如下(选取一个颜色HSV范围,视频中的该颜色变为白,其他颜色为黑):
import cv2 as cv import numpy as np def extract(): capture = cv.VideoCapture(r'D:\opencv-test\007、Tensorflow案例实战视频课程07 逻辑回归框架.mp4') while True: ret,frame=capture.read() if not ret: break hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV) lower_hsv = np.array([37,43,46]) upper_hsv = np.array([77,255,255]) mask = cv.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
#黑白视频 cv.imshow('mask',mask)
#本色视频 cv.imshow('video',frame) c = cv.waitKey(40) if c ==27: break def color_space_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) cv.imshow("gray",gray) hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV) cv.imshow('hsv',hsv) yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV) cv.imshow('yuv',yuv) ycrcb= cv.cvtColor(image, cv.COLOR_BGR2YCrCb) cv.imshow('YCRCB', ycrcb) src= cv.imread("D:\opencv-test\9807657b93fbe2067c1f394af8fd674.png") # cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) # cv.imshow("input image",src) # color_space_demo(src) extract() #三元色分割 # b,g,r =cv.split(src) # cv.imshow('blue',b) # cv.imshow('green',g) # cv.imshow('red',r) #三原色合并 # src = cv.merge([b,g,r]) #更改其中一个元色 # src[:,:,0]=0 # cv.imshow('changed image',src) cv.waitKey(0) cv.destroyAllWindows()