import cv2 as cv
# 读一个图像,返回它的矩阵. img = cv.imread(filename="文件路径")
img = cv.imread(filename="./cat.jpg")
# 显示图像矩阵.cv.imshow(winname="窗口名", mat="图像矩阵")
cv.imshow(winname="test", mat=img)
cv.waitKey(0)
注:按d键退出
def readvideo():
# 0代表调用本地摄像头
capture = cv.VideoCapture(0)
while True:
isTrue, frame = capture.read()
cv.imshow(winname="camera from pc", mat=frame)
if cv.waitKey(20) & 0xFF==ord(‘d‘):
break
capture.release()
cv.destroyAllWindows()
readvideo()
注:按d键退出
def readvideo2():
# 指定路径即可
capture = cv.VideoCapture(‘./video.mp4‘)
while True:
isTrue, frame = capture.read()
cv.imshow(winname="video from pc", mat=frame)
if cv.waitKey(20) & 0xFF==ord(‘d‘):
break
capture.release()
cv.destroyAllWindows()
readvideo2()
注:这个rescalevideo,适用于图片,视频,摄像头
import cv2 as cv
def rescalevideo(frame, size=0.5):
width = frame.shape[1] * size
width = int(width)
heigh = frame.shape[0] * size
heigh = int(heigh)
dimensions = (width, heigh)
return cv.resize(src=frame, dsize=dimensions,
interpolation=cv.INTER_AREA
)
def readvideo2():
# 指定路径即可
capture = cv.VideoCapture(‘./video.mp4‘)
while True:
isTrue, frame = capture.read()
newframe = rescalevideo(frame)
cv.imshow(winname="video from pc", mat=frame)
cv.imshow(winname="low size video", mat=newframe)
if cv.waitKey(20) & 0xFF==ord(‘d‘):
break
capture.release()
cv.destroyAllWindows()
readvideo2()
import cv2 as cv
import numpy as np
blank = np.zeros(shape=(200, 200), dtype="uint8")
cv.imshow("blank", blank)
cv.waitKey(0)
import cv2 as cv
import numpy as np
# 创建一个黑色的图像
# 这里要增加3,表示rgb三层通道
blank = np.zeros(shape=(200, 200, 3), dtype="uint8")
cv.imshow("blank0", blank)
cv.rectangle(blank, pt1=(0,0),pt2=(100,100), color=(0,255,0), thickness=3)
cv.imshow("blank1", blank)
cv.waitKey(0)
原文:https://www.cnblogs.com/Coder-Photographer/p/14352377.html