OpenCV(Basics)-Understanding of How Images and Videos will work in OpenCV
OpenCV is an open-source computer vision library. It deals with images and video data. Before moving into the coding part let's understand the images and videos.
Image
Color Image is a 3-dimensional matrix or Tensor why 3D means because it has 3 channels(R, G, B). When it comes to grayscale images it has only 1 channel.
Every Image has pixels. The pixel range is between 0 to 255. If the particular pixel in the image has a value of 0 then it looks black if it has 255 then it is white.
I said the color image has a 3D matrix so all 3 matrices are of the same shape. Look at the below image you can understand it clearly.

Let's understand the basic operations of OpenCV on images.
import cv2 as cv
import numpy as npimg = cv.imread('yuvi_dhoni.jpg')
cv.imshow(img) #Use this If you are coding in your local machine or
cv2_imshow(img)# If you are coding in Google collab
Output:-

In the above code what we are doing was initially we are reading the image by using an image file and then we are printing the image.
One thing to remember if you are using google collab then you have to use cv2_imshow or if you are using your local machine(Jupyter, Pycharm, Vscode) then use cv.imshow
The below code will print the grayscale image. You just need to pass 0 as 2nd parameter in imread function.
gray_img = cv.imread('yuvi_dhoni.jpg',0)
cv.imshow(gray_img)
Output:-

cv.imshow('yuvi_dhoni_picture',img)
cv.waitKey(0)
cv.destroyAllWindows()
you can pass window names also while printing images. So it will print images with window names. In the above code, ‘yuvi_dhoni_picture’ is the window name.
You can create as many windows as possible but you have to give different window names.
cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event.
cv2.destroyAllWindows() simply destroy all the windows we created. If you want to destroy any specific window then give the name of that window.
print("Color Image Shape : ", img.shape)print("Gray Scale Image Shape :",gray_img.shape)
Output :-
Color Image Shape : (167, 301, 3)
Gray Scale Image Shape : (167, 301)
I told you at the beginning of the article the number of channels in Color(3 channels) and grayscale(1 channel) images.
Videos
Video is a sequence of frames or a combination of images.
cap = cv.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame',gray)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
VideoCapture(0) will capture the video from 1st camera in our machine, if you pass 1 to that then it will take the 2nd camera or you can pass the video file also.
The read object will return frame as well as return = bool(True or False). If the frame is read correctly then True or else false.
Thanks for reading.