The code below is a real-time image of the OpenCV grab camera in the PYTHON3 environment, and the histogram is calculated by OpenCV's calhist function and displayed in 3 different windows.
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, [[[]], [0, +])
minval, Maxval, Minpos,maxpos = Cv2.minmaxloc (hist)
# Print (Minval, Maxval, Minpos,maxpos)
histimage = Np.zeros ([256,256,3], np.uint8)
for x in range:
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 ()
Cnblogs Tags:python, OpenCV
Capture the camera image with OpenCV and display a 3-color histogram in real time under Python3