Opening multiple color windows using OpenCV Python
In this tutorial, we will learn opening multiple color windows using OpenCV Python. Python has various tools and libraries for image and video recording and processing. One of the very popular amongst them is OpenCV. With OpenCV library, the code gets to access the computer’s web camera which means we can record videos and images and process them as per our need. This often helps to maintain the traffic data.
In the code below, we will capture a video using different frames through OpenCV in Python. We have followed the following steps:-
- import the cv2 library
- cv2.VideoCapture() gets a video capture object for the camera
- Then we will start reading the frames in an infinite loop using the read() method using the object created ‘cap’
- We will capture the image in grayscale using the COLOR_BGR2GRAY filter
- Then the image will be shown in two windows – one will be colored and other black and white
- We would break the loop when the key ‘q’ is pressed.
Below is the code for it:
Multiple color windows using OpenCV in Python
import cv2 cap= cv2.VideoCapture(0) while True: # capture frame by frame ret,frame=cap.read() #our operations on the frame come here gray= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',frame)# displaying the original colored frame cv2.imshow('gray',gray)# displaying the filtered frame black n white #this loops break when the key 'q' is pressed if cv2.waitKey(10) & 0xFF == ord('q'): break #when everything done, release the capture cap.release() cv2.destroyAllWindows()
The code above opens up your first camera and splits the screen into two windows – one in which the original video will be happening, the other with the gray filter applied. This code might not run on an online IDE. Below is the screenshot of an instance of the video recording:
Also read: Detect speed of a car with OpenCV in Python