Opening multiple color windows to capture using OpenCV in Python

OpenCV is an open source computer vision library that works with many programming languages and provides a vast scope to understand the subject of computer vision.In this example we will use OpenCV to open the camera of the system and capture the video in two different colors.
Approach: With the libraries available in OpenCV-Python below we will open two different windows, one window will show live camera result in coloured form and the another will display the same in grayscale (black and white).The code is as below
Libraries Used:

  • cv2
  • numpy

cv2 library installs automatically when openCV is installed.In order to install numpy use the following command at the cmd/linux terminal:
pip install numpy

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required modules
import cv2
import numpy as np
  
# capturing from the first camera attached
cap = cv2.VideoCapture(0)
  
# will continue to capture until 'q' key is pressed
while True:
    ret, frame = cap.read()
  
    # Capturing in grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      
    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
  
    # Program will terminate when 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# Releasing all the resources
cap.release()
cv2.destroyAllWindows()

chevron_right


Output:

Run the above code on your own system so that you can install the required libraries and more



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.