在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: python, opencv


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM