Capture the camera image with OpenCV and display a 3-color histogram in real time under Python3

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.