Opencv——基础3

补充 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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值