zoukankan      html  css  js  c++  java
  • 在python3下使用OpenCV 抓取摄像头图像并实时显示3色直方图

    以下代码为在Python3环境下利用OpenCV 抓取摄像头的实时图像, 通过OpenCV的 calHist函数计算直方图, 并显示在3个不同窗口中.

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    import time

    cap  = cv2.VideoCapture(0)
    for i in range(0, 19):
         print(cap.get(i))

    while(1):
         ret, frame = cap.read()
         # color = ('b', 'g', 'r')
         color = ((255,0,0), (0,255,0), (0,0,255))
        

        for i, col in enumerate(color):
             hist = cv2.calcHist([frame], [i], None, [256], [0, 256])
             minVal, maxVal,minPos,maxPos = cv2.minMaxLoc(hist)
             # print(minVal, maxVal,minPos,maxPos)
             histImage = np.zeros([256,256,3], np.uint8)
             for x in range(256):
                 cv2.line(histImage, (x,256), (x, 256-(hist[x]/maxVal)*250), col)
             cv2.imshow("Hist{}".format(i), histImage)
        
         cv2.imshow("Capture", frame)
         # time.sleep(0.01)

        key = cv2.waitKey(1)
         if key & 0xff == ord('q') or key == 27:
             print(frame.shape,ret)
             break
    cap.release()
    cv2.destroyAllWindows()


    image

    cnblogs Tags: ,
  • 相关阅读:
    iOS 中架构模式的浅显理解
    Block 在 ARC 下的拷贝
    Repo 的使用小结
    博客园新语言代码高亮以及OpenLiveWriter插件开发(一)
    GridLayout 使用
    PopupWindow 使用
    NDK笔记(二)-在Android Studio中使用ndk-build
    NDK 笔记(一)
    Java 多线程编程
    搭建Apache Web服务器
  • 原文地址:https://www.cnblogs.com/gxgl314/p/9273500.html
Copyright © 2011-2022 走看看