EXPERIMENT – 2 22IR005
BASIC CONCEPTS IN OPENCV IMAGE AND VIDEO HANDLING
Aim:
1. To code the following tasks in OpenCV
a) To read and display an image by using OpenCV
b) To resize an image using OpenCV
2. To capture video from Camera, play a video from a file and save a video file.
Software/ Package Used:
1. Visual Studio Code
2. Libraries used:
a) NumPy
b) opencv-python
c) matplotlib
d) scipy
Programs:
1. Read and Display image using OpenCV:
#import the cv2 library
import cv2
#the function cv2.imread() is used to read an image
img_color = cv2.imread('bike.jpg',cv2.IMREAD_COLOR)
img_grayscale = cv2.imread('bike.jpg',cv2.IMREAD_GRAYSCALE)
img_unchanged = cv2.imread('bike.jpg',cv2.IMREAD_UNCHANGED)
#img_color = cv2.imread('1.jpg',1)
#img_grayscale = cv2.imread('1.jpg',0)
#img_unchanged = cv2.imread('1.jpg',-1)
#The function cv2.imshow() is used to dsip;ay an image in a window.
#Displays an image inside a window
cv2.imshow('color image',img_color)
cv2.imshow('grayscale image',img_grayscale)
cv2.imshow('unchanged image',img_unchanged)
#waitkey() waits for a key press to close the window and 0 specifies indefinite loop
dim = img_color.shape
print(dim)
cv2.waitKey(0)
#cv2.destroyAllWindows() simply destroys all the windows we created
cv2.destroyAllWindows()
#the function cv2.imwrite() is used to write an image.
cv2.imwrite('grayscale.jpg',img_grayscale)
Output :
(148, 204, 3)
Input Image:
Output Image:
• Color: • Grayscale:
• Unchanged:
2. RGB Recognition :
import cv2
image = cv2.imread('opencv.jpg')
cv2.imshow('Original_Image', image)
b,g,r = cv2.split(image)
cv2.imshow('model blue color', b)
cv2.imshow('model green color', g)
cv2.imshow('model red color', r)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :
Original image : Blue image : Green image : Red image :
3. Resize :
import cv2
image = cv2.imread('opencv.jpg')
resized_image = cv2.resize(image,(150,150))
cv2.imshow('resized_image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output :
Input image :
Output image :
4. Video Handling :
Uding WebCam :
import cv2
#open the default camera
cam = cv2.VideoCapture(0)
#get the default frame width and height
frame_width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
#define the codec and create Videowriter object
fourcc = cv2.Videowriter_fourcc('mp4v')
out = cv2.Videowriter('output.mp4', fourcc, 20.0, (frame_width, frame_height))
while True :
ret, frame = cam.read()
#Write the frame to the output file
out.write(frame)
#Display the captured frame
cv2.imshow('Camera', frame)
#Press 'q' to exit the loop
if cv2.waitKey(1) == ord('q'):
break
#Release the capture and writer objects
cam.release()
out.release()
cv2.destroyAllWindows()
Output :
Using WebCam :
Result:
The basics of OpenCV Image and Video Handling were learnt using OpenCV-
python in Visual Studio Code.
Post Lab Questions:
1. List out the image and video file formats supported in OpenCV. What are the
formats supported in OpenCV for writing an image into the computer?
OpenCV primarily supports image formats like JPEG (.jpg), PNG, TIFF, BMP, and can
also read formats like GIF, WebP, JPEG 2000, and various PNM (Portable Network Map)
variations.
Commonly supported image formats are:
JPEG (.jpg)
PNG
TIFF
BMP
GIF
WebP
JPEG 2000 (.jp2)
Various PNM formats (like PBM)
2. Differentiate the functions used to read the video inputs from a built-in
camera, an external USB camera and from a video file that is saved in your
local drive
cv2.VideoCapture(0) Reads from built-in or external USB camera
cv2.VideoCapture('filename or filepath') Reads from the specified file in
the given filepath
3. How to find the size of an image?
import cv2
# read image
img = cv2.imread('cat.jpg')
# get dimensions of image
dimensions = img.shape
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
print('Image Dimension: ',dimensions)
print('Image Height: ',height)
print('Image Width: ',width)
Output:
Image Dimension: (230, 220, 3)
Image Height: 230
Image Width: 220
4. What are the different interpolation methods available in cv2.resize
function?
The cv2.resize function in OpenCV has a number of interpolation methods. They
are:
Nearest:neighbor interpolation: cv2.INTER_NEAREST
Bilinear interpolation: cv2.INTER_LINEAR
Bicubic interpolation: cv2.INTER_CUBIC
Lanczos interpolation: cv2.INTER_LANCZOS4
Area interpolation: cv2.INTER_AREA
Interpolation is a method for constructing new data points based on a set of known
data points. The cv2.resize function uses the interpolation parameter to calculate
pixel values for a new image from the original one. The choice of interpolation
method depends on the situation, as each method has its own merits and
challenges. For example, bicubic interpolation is slower but produces higher-
quality results for upscaling than bilinear interpolation
5. What is the significance of the waitKey() function in OpenCV?
waitKey() is used to pause the execution of window created by the function
cv2.imshow(). When waitKey(0) is used, the execution is paused. Otherwise
waitkey(n) can be used, where n is the time in milliseconds, the window will be
displayed for.
6. What is the difference in Reading an image using OpenCV and Matplotlib?
OpenCV reads images in BGR (Blue, Green, Red) format by default. It uses the
cv2.imread() function to read images. Matplotlib reads images in RGB (Red, Green,
Blue) format by default. It uses the matplotlib.pyplot.imread() function to read
images. If you read an image with OpenCV and display it using Matplotlib, the
colors will be incorrect. To correct this, you need to convert the image from BGR
to RGB using cv2.cvtColor(image, cv2.COLOR_BGR2RGB).