OpenCV Python Tutorial

OpenCV Python Tutorial

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV provides a vast array of tools to help developers incorporate computer vision functionalities into their applications.

In this brief tutorial, we'll introduce you to some basic concepts and functionalities provided by OpenCV in Python.

1. Installation:

To start using OpenCV in Python, first, you need to install it. You can do so using pip:

pip install opencv-python 

2. Reading, Writing, and Displaying an Image:

import cv2 # Reading an image image = cv2.imread('path_to_image.jpg') # Displaying the image in a window cv2.imshow('Image', image) cv2.waitKey(0) cv2.destroyAllWindows() # Writing/saving the image cv2.imwrite('path_to_save_image.jpg', image) 

3. Converting Image to Grayscale:

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale Image', gray_image) cv2.waitKey(0) cv2.destroyAllWindows() 

4. Capturing Video from Webcam:

cap = cv2.VideoCapture(0) # '0' refers to the default camera while True: ret, frame = cap.read() # Read a frame cv2.imshow('Webcam Feed', frame) if cv2.waitKey(1) & 0xFF == ord('q'): # Quit on pressing 'q' break cap.release() cv2.destroyAllWindows() 

5. Basic Image Processing - Gaussian Blur:

blurred_image = cv2.GaussianBlur(image, (15, 15), 0) cv2.imshow('Blurred Image', blurred_image) cv2.waitKey(0) cv2.destroyAllWindows() 

6. Drawing Shapes and Writing Text:

# Drawing a rectangle cv2.rectangle(image, (50, 50), (200, 200), (0, 255, 0), 2) # Writing text cv2.putText(image, 'OpenCV', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.imshow('Shapes and Text', image) cv2.waitKey(0) cv2.destroyAllWindows() 

This tutorial only scratches the surface of what's possible with OpenCV. The library offers numerous functionalities, including object detection, face recognition, image stitching, and many more.


More Tags

jenkins-2 virtualization pg-dump vector logout deep-linking return indexoutofboundsexception heap-analytics istanbul

More Programming Guides

Other Guides

More Programming Examples