注释:本文翻译自OpenCV3.0.0 document->OpenCV-Python Tutorials
import numpy as np
import cv2
#以灰度模式加载图像
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0) #等待任意键按下
cv2.destroyAllWindows() #销毁所有窗口
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('messigray.png',img)
import numpy as np
import cv2
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0) & 0xFF
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # 隐藏x和y坐标上的刻度值
plt.show()
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# 逐帧捕获
ret, frame = cap.read()
# 图像灰度化
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 显示结果
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 最后释放捕获
cap.release()
cv2.destroyAllWindows()
cap.read()返回一个bool(True / False)。 如果frame正确读取,则为True。 因此,您可以通过检查该返回值来查看是否到达视频的结尾。
import numpy as np
import cv2
cap = cv2.VideoCapture('vtest.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
注意:确保安装了正确的ffmpeg或gstreamer版本。 有时候,由于ffmpeg / gstreamer的安装错误,使用视频捕获技术很麻烦。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
# 创建一个黑色图像
img = np.zeros((512,512,3), np.uint8)
#画一个对角蓝线,厚度为5像素
cv2.line(img,(0,0),(511,511),(255,0,0),5)
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv2.circle(img,(447,63), 63, (0,0,255), -1)
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
注意:如果第三个参数是False,绘制的图像将是一条折现连接所有的点,而不是一个封闭的形状。
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
import cv2
import numpy as np
# 鼠标回调函数
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
# 创建一个黑色图像,并绑定窗口和鼠标回调函数
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
#根据鼠标点击事件花绿色的矩形或者画红色的圆
#用m键切换模式,默认是画矩形,按下ESC键退出
import cv2
import numpy as np
drawing=False #true if mouse is pressed
mode=True #if true, draw a rectangle. Press 'm' to toggle to curve
ix,iy=-1,-1
#mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing==True
ix,iy=x,y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),50,(0,0,255),-1)
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),50,(0,0,255),-1)
#Next we have to bind this mouse callback function to OpenCV Window.
#In the main loop,we should set a keyboard binding for key'm' to toggle between
#rectangle and circle
img=np.zeros((512,512,3),np.uint8);
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xFF
if k==ord('m'):
mode=not mode
elif k==27:
break
cv2.destroyAllWindows()
结果:
import cv2
import numpy as np
def nothing(x):
pass
#create a black image,a window
img=np.zeros((300,512,3),np.uint8)
cv2.namedWindow('image')
#create trackbars for color change
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
#create trackbar for ON/OFF functonality
switch='0:OFF\n1:ON'
cv2.createTrackbar(switch,'image',0,1,nothing)
#按下ESC键程序退出
while(1):
cv2.imshow('image',img)
k=cv2.waitKey(1)&0xFF
if k==27:
break
#get current positions of four trackbars
r=cv2.getTrackbarPos('R','image')
g=cv2.getTrackbarPos('G','image')
b=cv2.getTrackbarPos('B','image')
s=cv2.getTrackbarPos(switch,'image')
if s==0:
img[:]=0
else:
img[:]=[b,g,r]
cv2.destroyAllWindows()