OpenCV Guide 👁️📷

Computer Vision with Python

1. What is OpenCV?

OpenCV (Open Source Computer Vision Library) is used for image processing, video analysis, and real-time vision applications.

2. Installation

pip install opencv-python

3. Read Image

import cv2

img = cv2.imread("image.jpg")
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Convert to Grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

5. Resize Image

resized = cv2.resize(img, (300, 300))

6. Draw Shapes

cv2.rectangle(img, (50,50), (200,200), (255,0,0), 2)

7. Camera Capture

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    cv2.imshow("Camera", frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()

8. Face Detection

face = cv2.CascadeClassifier('haarcascade_frontalface.xml')

faces = face.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

9. Real Use Cases