发布时间:2024-01-13 15:30
import cv2
import numpy as np
def createWindow():
#读取图片
img=cv2.imread('images/1 (1).jpg')
#创建窗口 flags=WINDOW_NORMAL表示可以改变窗口大小
cv2.namedWindow(winname='window',flags=cv2.WINDOW_NORMAL)
#缩放窗口的大小
cv2.resizeWindow(winname='window',width=300,height=200)
#显示窗口
cv2.imshow('window',img)
#获取鼠标或者键值
key=cv2.waitKey(0)
if (key&0XFF==ord('Q')):
cv2.destroyAllWindows()
#销毁所有的窗口
if __name__ == '__main__':
print('PyCharm')
createWindow()
import cv2
import numpy as np
def createWindow():
#读取图片
img=cv2.imread('images/1 (1).jpg')
#创建窗口 flags=WINDOW_NORMAL表示可以改变窗口大小
cv2.namedWindow(winname='window',flags=cv2.WINDOW_NORMAL)
#缩放窗口的大小
cv2.resizeWindow(winname='window',width=300,height=200)
while True:
#显示窗口
cv2.imshow('window',img)
#获取鼠标或者键值
key=cv2.waitKey(0)
if (key&0XFF==ord('Q')):
break
elif (key&0xFF==ord('s')):
# 保存图片
# name-保存的文件名 img-保存的图片
cv2.imwrite('save_pic.png', img)
break
cv2.destroyAllWindows()
#销毁所有的窗口
if __name__ == '__main__':
print('PyCharm')
createWindow()
import os
import cv2
import numpy as np
def CollectVideo():
#创建窗口
cv2.namedWindow(winname='window',flags=cv2.WINDOW_AUTOSIZE)
cv2.resizeWindow(winname='window',width=450,height=300)
#打开摄像头
cap=cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
#获取窗口大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 输出文件 多媒体文件格式 视频帧率 分辨率大小
vw = cv2.VideoWriter('video.avi', fourcc, 25, size)
while cap.isOpened():
#从摄像头读取视频帧
OK,frame=cap.read()
if OK:
#显示摄像头画面
cv2.imshow('window',frame)
cv2.resizeWindow(winname='window', width=450, height=300)
#将从摄像头采集的视频帧写入文件
vw.write(frame)
#从鼠标和键盘获取键值,按下ESC键退出
if cv2.waitKey(1)&0xFF==27:
break
#释放资源
cap.release()
vw.release()
#销毁所有的窗口
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
CollectVideo()
#回调函数参数解释
#event:鼠标移动,按下左键;
#(x,y):鼠标坐标
#flags:鼠标键及组合键
import cv2
import numpy as np
#回调函数定义
def mouse_callback(event,x,y,flags,userdata):
print(event,x,y,flags,userdata)
#创建窗口
cv2.namedWindow(winname='mouse',flags=cv2.WINDOW_NORMAL)
cv2.resizeWindow(winname='mouse',width=450,height=300)
#给“mouse”窗口设置回调函数
cv2.setMouseCallback('mouse',mouse_callback,'123')
img=np.zeros(shape=(300,450,3),dtype=np.uint8)
while True:
cv2.imshow('mouse',img)
#按下ESC键退出
if cv2.waitKey(1)&0xFF==27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print('pycharm')
import os
import cv2
import numpy as np
cv2.namedWindow(winname='trackbar',flags=cv2.WINDOW_NORMAL)
cv2.resizeWindow(winname='trackbar',width=450,height=300)
#获取TrackBar的值
def TrackBarValue():
#获取窗口“window”下子窗口“R”的值
value_R= cv2.getTrackbarPos(trackbarname='R', winname='trackbar')
value_G = cv2.getTrackbarPos(trackbarname='G', winname='trackbar')
value_B = cv2.getTrackbarPos(trackbarname='B', winname='trackbar')
return value_R,value_G,value_B
def callback():
pass
#定义TrackBar函数
def TrackBarBGR():
#value-trackbar的值 count-设置的最大值count(最小值为0) OnChange-回调函数
cv2.createTrackbar('R','trackbar', 0, 255, callback)
cv2.createTrackbar('G','trackbar', 0, 255, callback)
cv2.createTrackbar('B','trackbar', 0, 255, callback)
img=np.zeros(shape=(450,300,3),dtype=np.uint8)
#创建trackbar组件
TrackBarBGR()
while True:
#获取trackbar的值
R,G,B=TrackBarValue()
img[:]=[B,G,R]
#获取值之后改变背景的颜色
cv2.imshow('trackbar', img)
#按下ESC键退出
if cv2.waitKey(1)&0xFF==27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')