# WINDOW_AUTOSIZE不容许修改窗口大小 # WINDOW_NORMAL可以修改窗口大小 # cv2.namedWindow("image", cv2.WINDOW_NORMAL) # cv2.resizeWindow('Window', 2000, 100) # cat = cv2.imread('1.jpeg', cv2.IMREAD_UNCHANGED) # cv2.imshow('Window', cat) # "0"是任意按键,给其他整数,等待,毫秒 # if key == 113: # cv2.destroyAllWindows() #def cv_show( name , img ) : #cat = cv2.imread(img, cv2.IMREAD_UNCHANGED) #cv2.imshow(name, cat) #key = cv2.waitKey(0) #cv_show('cat', '1.jpeg') # 加载图片并保存图片 #cv2.namedWindow("img", cv2.WINDOW_NORMAL) #cv2.resizeWindow('img',800,600) #img=cv2.imread('1.jpeg') #while True: #cv2.imshow('img',img) #key = cv2.waitKey(0) #if key==ord('q'): #break #elif key==ord('s'): #cv2.imwrite('123.png',img) #cv2.destoryAllWindows() #1.打开视频 # cv2.namedWindow("Video", cv2.WINDOW_NORMAL) # cv2.resizeWindow('Video',800,600) # cap=cv2.VideoCapture(0) # while True: # #读取每一帧,返回标记和每一帧的数据,‘True'表示读取到数据,False表示没读取到数据 # ret,frame=cap.read() # if not ret: # break # #返回数据 # cv2.imshow('Video', frame) # key=cv2.waitKey(10) # if key==ord('q'): # break # #释放资源 # cap.release() # cv2.destoryAllWindows() # #2.加载视频 # cv2.namedWindow("Video", cv2.WINDOW_NORMAL) # cv2.resizeWindow('Video',800,600) # #输入视频的地址 # cap=cv2.VideoCapture('11.mp4') # while cap.isOpened(): # #读取每一帧,返回标记和每一帧的数据,‘True'表示读取到数据,False表示没读取到数据 # ret,frame=cap.read() # if not ret: # break # #返回数据 # cv2.imshow('Video', frame) # key=cv2.waitKey(1000//30) # if key==ord('q'): # break # #释放资源 # cap.release() # cv2.destoryAllWindows() # # 3.录制视频 # cap=cv2.VideoCapture(0) # #*'mp4v'等同于解包操作:‘m’,'p','4','v' # fourcc=cv2.VideoWriter_fourcc(*'XVID') # vw=cv2.VideoWriter('output.avi',fourcc,20,(640,480)) # # fourcc=cv2.VideoWriter_fourcc(*'mp4v') # # vw=cv2.VideoWriter('output.mp4',fourcc,20,(640,480)) # while cap.isOpened(): # #读取每一帧,返回标记和每一帧的数据,‘True'表示读取到数据,False表示没读取到数据 # ret,frame=cap.read() # if not ret: # print('can not recive frame,Exiting... ') # break # #返回数据 # vw.write(frame) # cv2.imshow('frame', frame) # key=cv2.waitKey(1000//30) # if key==ord('q'): # break # cap.release() # vw.release() # cv2.destoryAllWindows() # # 4.控制鼠标 # #event:鼠标事件,flags:鼠标组合键 # def mouse_callback(event,x,y,flags,userdata): # print(event,x,y,flags,userdata) # cv2.namedWindow("mouse", cv2.WINDOW_NORMAL) # cv2.resizeWindow('mouse',640,360) # #设置鼠标回调函数 # cv2.setMouseCallback('mouse',mouse_callback,'123') # #显示窗口背景 # #生成全黑的图片 # img=np.zeros((360,640,3),np.uint8) # while True: # cv2.imshow('mouse',img) # key=cv2.waitKey(1) # if key==ord('q'): # break # cv2.destoryAllWindows() # # # TrackBar控件 # # createTrackbar(trackbarname,winname,value,count,onChange) # cv2.namedWindow('trackbar',cv2.WINDOW_NORMAL) # cv2.resizeWindow('trackbar',640,480) # def callback(value): # print(value) # #创建trackBar # cv2.createTrackbar( 'R', 'trackbar', 0, 255 , callback) # cv2.createTrackbar('G','trackbar',0,255,callback) # cv2.createTrackbar('B','trackbar',0,255,callback) # # img=np.zeros((360,640,3),np.uint8) # # while True: # # 获取当前的trackbar值 # r = cv2.getTrackbarPos('R', 'trackbar') # g = cv2.getTrackbarPos('G', 'trackbar') # b = cv2.getTrackbarPos('B', 'trackbar') # #改变图片颜色 # img[:]=[b,g,r] # cv2.imshow('trackbar',img) # key=cv2.waitKey(1) # if key==ord('q'): # break # cv2.destoryAllWindows() # opencv色彩空间 # HSV # HSL # YUV # import cv2 # # 空间颜色转换 # def callback(value): # pass # cv2.namedWindow('color',cv2.WINDOW_NORMAL) # cv2.resizeWindow('color',640,480) # img=cv2.imread('123.png') # # #常见的颜色空间转换2=to # color_spaces=[cv2.COLOR_BGR2RGBA,cv2.COLOR_BGR2BGRA, # cv2.COLOR_BGR2GRAY,cv2.COLOR_BGR2HSV,cv2.COLOR_BGR2YUV] # cv2.createTrackbar('trackbar','color',0,4,callback) # while True: # index=cv2.getTrackbarPos('trackbar','color') # # cvt_img=cv2.cvtColor(img, color_spaces[index]) # cv2.imshow('color',cvt_img) # key=cv2.waitKey(10) # if key==ord('q'): # break # cv2.destoryWindows() #import cv2 # # mat属性 # img=cv2.imread('123.png') # print(img.data) # print(img.dtype) # print(img.size) # print(img.shape) # print(img.itemsize) # print(img.ndim) # print(img.imag) # Mat拷贝 #因为python中图片数据已经包装成ndarray了,所以对Mat深浅拷贝其实就是对ndarray拷贝 # import cv2 # import numpy as np # # img=cv2.imread('123.png') # #浅拷贝 # img2=img.view() # #深拷贝 # img3=img.copy() # img[10:100,10:100]=[0,0,255] # # cv2.imshow('img',np.hstack((img,img2,img3))) # # cv2.waitKey(0) # cv2.destoyWindows() # # 图像分割与融合 # # import cv2 # import numpy as np # img = np.zeros((200,200,3),np.uint8) # #分割 # img = cv2.resize(img, (342, 256)) # (b, g, r) = cv2.split(img) # b[10:100,10,100]=255 # g[10:100,10:100]=255 # #合并通道 # img2=cv2.merge((b,g,r)) # cv2.imshow('img',np.hstack((b,g))) # cv2.imshow('img',np.hstack((img,img2))) # cv2.waitKey(0) # cv2.destoyWindows() # #绘制图形 # import cv2 # import numpy as np # img=np.zeros((480,640,3),np.uint8) # #画线 # #cv2.line(img,(10,20),(300,400),(0,0,255),5,0) # #画矩形 # #cv2.rectangle(img,(100,120),(140,160),(0,0,255),4) # #画圆 # cv2.circle(img,(150,150),100,(0,0,255)) # cv2.imshow('draw',img) # cv2.waitKey(0) # cv2.destoyWindows() # 人脸识别 # import cv2 # import numpy as np # # # 运行之前,检查cascade文件路径是否在相应的目录下 # face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') # # # 读取图像 # img = cv2.imread('young.jpg') # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # # # 检测脸部 # faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), # flags=cv2.CASCADE_SCALE_IMAGE) # print('Detected ', len(faces), " face") # # for (x, y, w, h) in faces: # img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) # roi_gray = gray[y: y + h, x: x + w] # roi_color = img[y: y + h, x: x + w] # # eyes = eye_cascade.detectMultiScale(roi_gray) # for (ex, ey, ew, eh) in eyes: # cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) # # cv2.imshow('img', img) # cv2.waitKey(0) # cv2.destroyAllWindows() # # 绘制多边形 # import cv2 # import numpy as np # img=np.zeros((480,640,3),np.uint8) # pts=np.array([[(10,120),(140,160),(230,400)]],np.int32) # #注意pts是三维的 # cv2.polylines(img,pts,True,(0,0,255),5) # #填充多边形 # cv2.fillPoly(img,pts,(0,0,255),16) # cv2.imshow('draw',img) # cv2.waitKey(0) # cv2.destoyWindows() # # 绘制文本 # import cv2 # import numpy as np # img=np.zeros((480,640,3),np.uint8) # cv2.putText(img,"你好",(100,300),cv2.FONT_HERSHEY_COMPLEX,3,[0,0,255]) # # cv2.imshow('draw',img) # cv2.waitKey(0) # cv2.destoyWindows() # # 图像运算 # import cv2 # import numpy as np # img1=cv2.imread('123.png') # img2=cv2.imread('1212.png') # print(img1.shape) # print(img2.shape) # # cv.add 加法操作要求具有相同的长度和通道数,add规则就是两张图片像素相加,超过255,则值是255. # # img1+=100 ,每个像素和100相加,超出255会被截断,相当于%256 # #减法,subtract(),相减为负,结果为0 # # new_img1=cv2.resize(img1,(img2.shape[:-1][::-1])) # new_img2=img2[:335, :600] # print(new_img2.shape) # new_img=cv2.add(img1,new_img2) # cv2.imshow('draw',new_img) # cv2.waitKey(0) # cv2.destoyWindows() # 图像融合 # 图像融合不是简单的做加法运算,而是通过线性运算,img=img1*w1+img2*w2+bias # import cv2 # import numpy as np # img1=cv2.imread('123.png') # img2=cv2.imread('1212.png') # new_img2=img2[:335, :600] # new_img=cv2.addWeighted(img1,0.3,new_img2,0.4,7) # cv2.imshow('draw',new_img) # cv2.waitKey(0) # cv2.destoyWindows()
Opencv学习笔记(1-24)
最新推荐文章于 2024-06-19 01:10:29 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)