2016-07-28 35 views
0

我试图从我的摄像头的灰度实时显示直方图,问题是直方图没有更新,我的凸轮停止,直到我关闭直方图的窗口。我怎样才能解决这个问题?我想在同一时间显示来自我的网络摄像头和其直方图的灰度img,可以做到这一点吗?我的凸轮实时直方图

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

cap = cv2.VideoCapture(0) 

while(True): 
    ret, frame = cap.read() 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    cv2.imshow('Janela', frame) 
    cv2.imshow('Outra', gray) 
    plt.hist(gray.ravel(), 256, [0, 256]) 
    plt.show() 

    if (cv2.waitKey(1) & 0xFF == 27): 
     break 

cap.release() 
cv2.destroyAllWindows() 
+0

'plt.show()'打开一个不像'cv2.imshow'需要'cv2.waitkey'来阻止的窗口。因此使用不同的功能显示直方图是您的最佳选择。 – B8vrede

回答

2

我一直在同一个任务中工作了一段时间。过了一段时间,我有一段很好的代码。它在一个窗口中显示摄像机图像,在另一个窗口中显示直方图。由于我对查找颜色感兴趣,因此我正在处理每帧的“色调”通道。

# import the necessary packages 
import cv2 
import numpy as np 

#Create window to display image 
cv2.namedWindow('colorhist', cv2.CV_WINDOW_AUTOSIZE) 

#Set hist parameters 
hist_height = 64 
hist_width = 256 
nbins = 32 
bin_width = hist_width/nbins 

camera_id = 0 # type fo webcam [0 built-in | 1 external] 
cameraWidth = 320 
cameraHeight = 240 

if camera_id == 0: 
    cameraId = "PC webcam" 
elif camera_id == 1: 
    cameraId = "External webcam" 

camera = cv2.VideoCapture(camera_id) 

# set camera image to 320 x 240 pixels 
camera.set(3,cameraWidth) 
camera.set(4,cameraHeight) 

cameraInfo = "Image size (%d,%d)" % (camera.get(3),camera.get(4)) 

# initialize mask matrix 
mask = np.zeros((cameraHeight,cameraWidth), np.uint8) 

# draw a circle in mask matrix 
cv2.circle(mask,(cameraWidth/2,cameraHeight/2), 50, 255, -1) 

#Create an empty image for the histogram 
h = np.zeros((hist_height,hist_width)) 

#Create array for the bins 
bins = np.arange(nbins,dtype=np.int32).reshape(nbins,1) 

while True: 
    # grab the current frame 
    (grabbed, frame) = camera.read() 

    if not grabbed: 
     "Camera could not be started." 
     break 

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 
    #Calculate and normalise the histogram 
    hist_hue = cv2.calcHist([hsv],[0],mask,[nbins],[0,256]) 
    cv2.normalize(hist_hue,hist_hue,hist_height,cv2.NORM_MINMAX) 
    hist=np.int32(np.around(hist_hue)) 
    pts = np.column_stack((bins,hist)) 

    #Loop through each bin and plot the rectangle in white 
    for x,y in enumerate(hist): 
     cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height),(255),-1) 

    #Flip upside down 
    h=np.flipud(h) 

    #Show the histogram 
    cv2.imshow('Color Histogram',h) 
    h = np.zeros((hist_height,hist_width)) 

    frame = cv2.bitwise_and(frame,frame,mask = mask) 
    cv2.putText(frame, cameraInfo, (10, 20), 
    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 

    cv2.imshow(cameraId, frame)    
    key = cv2.waitKey(1) & 0xFF 

    # if the `q` key is pressed, break from the loop 
    if key == ord("q"): 
     break 

camera.release() 
cv2.destroyAllWindows() 

的代码是基于JohnLinux代码(How do I plot a 32-Bin Histogram for a Grayscale Image in Python using OpenCV)和其它代码的行来自我从阿德里安的ROSENBROCK的网站https://www.pyimagesearch.com/教训。

+0

正是我想要做的!我只需要将一些变量转换为int,因为它提供了错误类型。 ''cv2.circle(mask,(cameraWidth/2,cameraHeight/2),50,255,-1) TypeError:预期的整数参数,得到float'' –