A Comprehensive Guide to OpenCV in Python.

Pradeep Kumar
1 min readJan 2, 2024

--

Introduction:

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It contains more than 2500 optimized algorithms for image and video analysis.

OpenCV

Installation:

You can install OpenCV using pip:

pip install opencv-python

Reading, Writing and Displaying Images

To read an image, we use the imread() function:

import cv2
img = cv2.imread('image.jpg')

To display an image, we use the imshow() function:

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

To write or save an image, we use the imwrite() function:

cv2.imwrite('output.jpg', img)

Basic Operations on Images:

1. Image Resizing

resized_img = cv2.resize(img, (new_width, new_height))

2. Image Rotation

(h, w) = img.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h))

Image Processing:

1. Edge Detection

edges = cv2.Canny(img, threshold1, threshold2)

2. Image Thresholding

ret,thresh1 = cv2.threshold(img,threshold_value,max_value, cv2.THRESH_BINARY)

Conclusion

OpenCV offers a wide array of functionalities for image processing. It’s a powerful tool for developers and researchers in the field of computer vision.

--

--