python - Histogram of my cam in real time -


im trying show histogram in real time grayscale of webcam, problem histogram not being update , cam stop until close histogram's window. how can fix this? want show grayscale img webcam , histogram in same time, possible that?

import numpy np import cv2 matplotlib import pyplot 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() 

i have been working while in same task. after time, have piece of code works good. displays camera image in window , histogram in other. since i'm interested in finding colors, i'm working "hue" channel of each frame.

# import necessary packages import cv2 import numpy np  #create window 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 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 circle in mask matrix cv2.circle(mask,(camerawidth/2,cameraheight/2), 50, 255, -1)  #create empty image histogram h = np.zeros((hist_height,hist_width))  #create array bins bins = np.arange(nbins,dtype=np.int32).reshape(nbins,1)  while true:    # grab current frame     (grabbed, frame) = camera.read()     if not grabbed:       "camera not started."       break     hsv = cv2.cvtcolor(frame, cv2.color_bgr2hsv)    #calculate , normalise 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 , plot rectangle in white    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 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 `q` key pressed, break loop    if key == ord("q"):       break  camera.release() cv2.destroyallwindows() 

the code based on code johnlinux (how plot 32-bin histogram grayscale image in python using opencv) , other lines of code come have learnt adrian rosenbrock's site https://www.pyimagesearch.com/.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -