OpenCV in Python: Part 1 - Working with Images and Videos

The translation of the material was prepared as part of the " Python Developer. Basic " course .






Welcome! Here is the first article in the OpenCV in Python series, which, as you guessed by the name, is devoted to how to learn how to work comfortably in OpenCV.





, , . . , .





, !





OpenCV

, . OpenCV, . ( Windows): 





pip install opencv-python
      
      



, .





.





import cv2
import argparse
      
      



cv2 – OpenCV. . argparse , . , (https://www.pyimagesearch.com/2018/03/12/python-argparse-command-line-arguments/), .





.





( ) , . 





, .





ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())
      
      



argparse



, , , , , , , . vars()



, dict



.





imread



cv2, image



.





image = cv2.imread(args["image"], cv2.IMREAD_GRAYSCALE)
      
      



, – args[“image”]



, , . , . 





. , , . , . 





imshow. – , – .





cv2.imshow("Image", image)
cv2.waitKey(0)
      
      



cv2.waitKey(0)



, , . 





, image



, .





(h, w) = image.shape[:2]

# display image properties
print("width: {} pixels".format(w))
print("height: {} pixels".format(h))
      
      



OpenCV . , OpenCV , .jpeg .png.





imwrite



, , . , , OpenCV .





cv2.imwrite("folder/newimage.png", image)
      
      







import cv2
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"], cv2.IMREAD_GRAYSCALE)

cv2.imshow("Image", image)
cv2.waitKey(0)

(h, w) = image.shape[:2]

# display image properties
print("width: {} pixels".format(w))
print("height: {} pixels".format(h))

cv2.imwrite("photos/newimage.png", image)
      
      



:





python app.py --image photo_one.jpeg
      
      



: app.py, photo_one.jpeg, .





, :





, , newimage.png.





OpenCV

, , - OpenCV. , , .





.





import cv2
import numpy as np
      
      



VideoCapture



, , , «» .





cap = cv2.VideoCapture(0)
      
      



, , . 0 – - , 1 – ..





, .





while



, .





while True:
    ret, frame = cap.read()
    cv2.imshow('video feed', frame)
      
      



cap.read()



(True/False) . , True.





cv2.imshow()



. .





, -, .





if cv2.waitKey(1) & 0xFF == ord('q'):
    break
      
      



, , .





waitKey(0)



-1, . , , 32- .





0xFF



– 8- 11111111, «q». 255.





, ord(char)



, [1]. 





, , «». , , .





cap.release()
cv2.destroyAllWindows()
      
      



. , , :





import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    cv2.imshow('video feed', frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()
      
      



, - «video feed». , «q».





, ? . .





while : 





gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray feed', gray)
      
      



:





  1. .





  2. .





, /. , .





VideoWriter



. fourcc (https://en.wikipedia.org/wiki/FourCC). FourCC – 4- , . FourCC (http://www.fourcc.org/codecs.php). 





fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
      
      



. (fps) . 





, while.





out.write(frame)
out.release()
cv2.destroyAllWindows()
      
      



VideoWriter



.





:





import cv2
import numpy as np

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    out.write(frame)

    cv2.imshow('video feed', frame)
    cv2.imshow('gray feed', gray)    

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()
      
      



, «output.avi» . , .





– . OpenCV – , . 





, , OpenCV. , , , . 





, .





GitHub





– 1 (https://github.com/sthitaprajna-mishra/opencv_in_python/tree/main/Part%201)









  1. Getting Started with Videos — OpenCV-Python Tutorials 1 documentation (opencv-python-tutroals.readthedocs.io)










"Python Developer. Basic"








All Articles