补充 cv2.waitkey()
cv2.waitKey(1)
#参数是1,表示延时1ms切换到下一帧图像,对于视频而言;
#参数为0,如cv2.waitKey(0)只显示当前帧图像,相当于视频暂停,;
#参数过大如cv2.waitKey(1000),会因为延时过久而卡顿感觉到卡顿。
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
# Fixing random state for reproducibility
np.random.seed(19680801)
grid = np.random.rand(4, 4)
fig, axes = plt.subplots(3, 6, figsize=(12, 6),
subplot_kw={'xticks': [], 'yticks': []})
fig.subplots_adjust(hspace=0.3, wspace=0.05)
for ax, interp_method in zip(axes.flat, methods):
ax.imshow(grid, interpolation=interp_method, cmap='viridis')
ax.set_title(interp_method)
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('messi4.jpg')
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121);plt.imshow(img) # expects distorted color
plt.subplot(122);plt.imshow(img2) # expect true color
plt.show()
cv2.imshow('bgr image',img) # expects true color
cv2.imshow('rgb image',img2) # expects distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()
matplot: R G B
opencv: B G R
是两种显示模式
#!/usr/bin/env python
import cv2
import numpy as np
import matplotlib.pyplot as plt
cap = cv2.VideoCapture(1)
#ret, frame = cap.read()
#print(ret)
while True:
ret, frame = cap.read()
if ret == True:
cv2.imshow("frame",frame)
else:
print("cam error")
break
if cv2.waitKey(1) & 0xff == ord('q'):
break #这里的break只是跳出改循环,如果有嵌套的话,上层循环是会继续进行
cap.release()
cv2.destroyAllWindows()
创建滑块条
#!/usr/bin/env python
import cv2
import numpy as np
def nothing(x):
pass
img = np.zeros((400,300,3),np.uint8)
cv2.namedWindow("dennis")
cv2.createTrackbar("R", "dennis", 0, 255, nothing)
cv2.createTrackbar("G", "dennis", 0, 255, nothing)
cv2.createTrackbar("B", "dennis", 0, 255, nothing)
r, g, b = 0, 0, 0
while True:
cv2.imshow("dennis", img)
if cv2.waitKey(1) & 0xFF == 27:
break
r = cv2.getTrackbarPos('R','dennis')
g = cv2.getTrackbarPos('G','dennis')
b = cv2.getTrackbarPos('B','dennis')
print(b)
img[:, :, :] =[b, g, r]
cv2.imshow("dennis", img)
cv2.destroyAllWindows()