0% found this document useful (0 votes)
41 views25 pages

OpenCV Video Processing Techniques

Uploaded by

Raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Video Saving,
  • Video Processing Libraries,
  • Video Quality,
  • Video Processing Methods,
  • Video Processing Applications,
  • Color Conversion,
  • Boolean Values,
  • Video Processing Frameworks,
  • Computer Vision,
  • Video Output
0% found this document useful (0 votes)
41 views25 pages

OpenCV Video Processing Techniques

Uploaded by

Raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Video Saving,
  • Video Processing Libraries,
  • Video Quality,
  • Video Processing Methods,
  • Video Processing Applications,
  • Color Conversion,
  • Boolean Values,
  • Video Processing Frameworks,
  • Computer Vision,
  • Video Output

Video Operation using OpenCV

Lecture 5
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")

• cap is an object to read the video


Code
import cv2
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")
print("cap",cap)

while True:
ret,frame = [Link]()

[Link]("frame", frame)
[Link]()

[Link]()
[Link]()

ret ;is boolean value either there is frame or not


Resize the Video Size
Resize Code
import cv2
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")
print("cap",cap)

while True:
ret,frame = [Link]()
frame = [Link](frame,(700,450))
[Link]("frame", frame)
[Link]()

[Link]()
[Link]()
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")
Code
import cv2
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")
print("cap",cap)
while True:
ret,frame = [Link]()
frame = [Link](frame,(700,450))
[Link]("frame", frame)
k = [Link]()
if k == ord("q"):
break
[Link](25)
[Link]()
ret,frame = [Link]()
• Ret is boolean value
• Frame is any image
End the Video by press q
import cv2
• k = [Link]() cap = [Link]("F:\First Semester
2024\Computer Vision\lec5.mp4")
• if k == ord("q"): print("cap",cap)

• break while True:


ret,frame = [Link]()
frame = [Link](frame,(700,450))
[Link]("frame", frame)
k = [Link]()
if k == ord("q"):
break
[Link]()
[Link]()
64 bit error Mask
import cv2
cap = [Link]("F:\First Semester 2024\Computer Vision\lec5.mp4")
print("cap",cap)

while True:
ret,frame = [Link]()
frame = [Link](frame,(700,450))
[Link]("frame", frame)
k = [Link]()
if k == ord("q") & 0xFF:
break

[Link]()
[Link]()
To convert the frame into Grayscale
To convert the frame into Grayscale
import cv2
cap = [Link]("F:\First Semester 2024\Computer Vision\
lec5.mp4")
print("cap",cap)

while True:
ret,frame = [Link]()
frame = [Link](frame,(700,450))
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
[Link]("frame", frame)
[Link]("gray", gray)
k = [Link]()
if k == ord("q") & 0xFF:
break

[Link]()
[Link]()
[Link](100)
• Play Back Speed (SLOW MO)
Read Video From WebCam & Save it to
Memory
• cap = [Link](0)
• For webcam camera 0 for external camera 1
Read Video From WebCam
import cv2
#Now capture video from webcam and save it to memeory
cap = [Link](0,cv2.CAP_DSHOW)
print(cap)
while [Link]():
ret,frame = [Link]()
if ret == True:
frame =[Link](frame,(700,450))
gray = [Link](frame,cv2.COLOR_BGR2GRAY)
[Link]('frame',frame)
[Link]("gray", gray)
if [Link](1) & 0xFF == ord('q'): #Press to exit
break

[Link]()
[Link]()
Save it to Memory
Save it to Memory (Continued)
import cv2
#Now capture video from webcam and save it to memeory
cap = [Link](0,cv2.CAP_DSHOW)

#DIVX, XVID, MJPG, X264, WMV1, WMV2


fourcc = cv2.VideoWriter_fourcc(*"XVID") # "XVID"
# It contain four parameter, name, codec, fps, resolution
output = [Link]("F:\First Semester 2024\Computer Vision\[Link]", fourcc,20.0,
(640,480))
print(cap)
while [Link]():
ret,frame = [Link]()
if ret == True:

gray = [Link](frame,cv2.COLOR_BGR2GRAY)
[Link]('frame',frame)
[Link]("gray", gray)
[Link](frame)
if [Link](1) & 0xFF == ord('q'): #Press to exit
break

[Link]()
[Link]()
[Link]()
import cv2
To save the video in grayscale
#Now capture video from webcam and save it to memeory
cap = [Link](0,cv2.CAP_DSHOW)

#DIVX, XVID, MJPG, X264, WMV1, WMV2


fourcc = cv2.VideoWriter_fourcc(*"XVID") # "XVID"
# It contain four parameter, name, codec, fps, resolution
output = [Link]("F:\First Semester 2024\Computer Vision\[Link]", fourcc,20.0,
(640,480),0)
print(cap)
while [Link]():
ret,frame = [Link]()
if ret == True:

gray = [Link](frame,cv2.COLOR_BGR2GRAY)
[Link]('frame',frame)
[Link]("gray", gray)
[Link](gray)
if [Link](1) & 0xFF == ord('q'): #Press to exit
break

[Link]()
[Link]()
[Link]()
To flip the frame
import cv2
#Now capture video from webcam and save it to memeory
cap = [Link](0,cv2.CAP_DSHOW)

#DIVX, XVID, MJPG, X264, WMV1, WMV2


fourcc = cv2.VideoWriter_fourcc(*"XVID") # "XVID"
# It contain four parameter, name, codec, fps, resolution
output = [Link]("F:\First Semester 2024\Computer Vision\[Link]", fourcc,20.0,
(640,480),0)
print(cap)
while [Link]():
ret,frame = [Link]()
if ret == True:

gray = [Link](frame,cv2.COLOR_BGR2GRAY)
frame = [Link](frame,0)
[Link]('frame',frame)
[Link]("gray", gray)
[Link](gray)
if [Link](1) & 0xFF == ord('q'): #Press to exit
break

[Link]()
[Link]()
[Link]()
Gray Frame Flip

Common questions

Powered by AI

To convert a video frame to grayscale using OpenCV, you use the function cv2.cvtColor with the color conversion code cv2.COLOR_BGR2GRAY. This is typically done within a loop processing each frame: ret, frame = cap.read(), gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), cv2.imshow('gray', gray).

To capture and flip a video frame vertically using OpenCV, read each frame in a loop, then apply cv2.flip with 0 as the second argument to flip the frame vertically. Example code: ret, frame = cap.read(), frame = cv2.flip(frame, 0), cv2.imshow('frame', frame).

In OpenCV, to terminate a video processing loop when a specific key is pressed, you use cv2.waitKey() in combination with a comparison against the desired key's ASCII value. For example, if cv2.waitKey(1) & 0xFF == ord('q') would break the loop when the 'q' key is pressed .

The function 'cap.release()' is used to release the video capture object, freeing the resources used by it. 'cv2.destroyAllWindows()' is called to close all OpenCV windows that were opened during the operation. These functions ensure the proper cleanup after processing the video content .

When creating a video writer in OpenCV using cv2.VideoWriter, you need to specify four parameters: the output file name, the four-character code of codec used to compress the frames (e.g., 'XVID'), the frames per second (fps) rate, and the resolution (width and height) of the video frames .

To slow down the playback speed of a video in OpenCV, you can increase the delay between frames in the cv2.waitKey function. The function takes milliseconds as an argument, so increasing this delay reduces the frame rate and subsequently slows down the playback speed. For instance, using cv2.waitKey(100) will significantly slow down the video .

The use of '& 0xFF' in key press event handling with OpenCV ensures compatibility across different operating systems by masking all but the least significant byte. This operation is used to handle discrepancies between how different systems (e.g., 32-bit vs 64-bit) treat the return value of cv2.waitKey().

The function 'cv2.VideoCapture(0)' initializes the video capturing from the first camera device (usually the default webcam on a system). By specifying '0', it instructs OpenCV to use the first camera available on the system; '1' would refer to the next available camera if connected .

To resize a video frame in OpenCV, you can use the cv2.resize method within a while loop that reads the video frames. The code is as follows: import cv2, cap = cv2.VideoCapture('path_to_video'), while True: ret, frame = cap.read(), frame = cv2.resize(frame, (700, 450)), cv2.imshow('frame', frame), cv2.waitKey(), cap.release(), cv2.destroyAllWindows().

To save a video in grayscale using OpenCV, first convert each frame to grayscale using cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), then write these frames using the cv2.VideoWriter object. Make sure the VideoWriter's color flag is set to 0: output = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480), 0).

You might also like