OpenCV Functions to Get Started into Computer Vision

Last Updated : 16 Jul, 2026

Computer Vision is a field of artificial intelligence that enables computers to analyze, interpret and extract meaningful information from images and videos. It is widely used in applications such as self-driving cars, facial recognition, medical image analysis, robotics and surveillance systems.

  • OpenCV (Open Source Computer Vision Library) is an open-source library that provides a wide range of functions for computer vision, image processing and machine learning.
  • It supports real-time image and video analysis and is compatible with multiple programming languages, including Python, C++ and Java.

Reading Images

To read the images cv2.imread() method is used. This method loads an image from the specified file. If the image cannot be loaded due to an invalid path, missing file or unsupported format, cv2.imread() returns None.

Image Used

Read image opencv python

Example: Python OpenCV Reading Images

Python
import cv2
from google.colab.patches import cv2_imshow

# Read the image
img = cv2.imread("geeks.png", cv2.IMREAD_COLOR)

cv2_imshow(img)

Output:

Read image using Python Opencv

Saving Images

cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.

Example: Python OpenCV Saving Images

Python
import cv2
from google.colab.patches import cv2_imshow

# Path of the input image
image_path = "geeks14.png"   

img = cv2.imread(image_path)

# Check if the image was loaded successfully
if img is None:
    print("Error: Could not load the image.")
else:
    filename = "savedImage.jpg"

    cv2.imwrite(filename, img)
    
    saved_img = cv2.imread(filename)
    cv2_imshow(saved_img)

Output:

show image using Python Opencv

Resizing Image

Image resizing refers to the scaling of images. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as more is the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model. It also helps in zooming in images. Many times we need to resize the image i.e. either shrink it or scale up to meet the size requirements.

OpenCV provides us with several interpolation methods for resizing an image. Choice of Interpolation Method for Resizing -

  • cv2.INTER_AREA: This is used when we need to shrink an image.
  • cv2.INTER_CUBIC: This is slow but more efficient.
  • cv2.INTER_LINEAR: This is primarily used when zooming is required. This is the default interpolation technique in OpenCV.

Example: Python OpenCV Image Resizing

Python
import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread("geeks.png", 1)
# Loading the image

half = cv2.resize(image, (0, 0), fx = 0.1, fy = 0.1)
bigger = cv2.resize(image, (1050, 1610))

stretch_near = cv2.resize(image, (780, 540),
            interpolation = cv2.INTER_NEAREST)


Titles =["Original", "Half", "Bigger", "Interpolation Nearest"]
images =[image, half, bigger, stretch_near]
count = 4

for i in range(count):
    plt.subplot(2, 3, i + 1)
    plt.title(Titles[i])
    plt.imshow(images[i])

plt.show()

Output:

Python OpenCV Image Resizing

Color Spaces

Color spaces are a way to represent the color channels present in the image that gives the image that particular hue. There are several different color spaces and each has its own significance. Some of the popular color spaces are RGB (Red, Green, Blue), CMYK (Cyan, Magenta, Yellow, Black), HSV (Hue, Saturation, Value), etc.

 cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV.

Example: Python OpenCV Color Spaces

Python
import cv2
from google.colab.patches import cv2_imshow

src = cv2.imread("geeks.png")

# Convert the image to grayscale
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

cv2_imshow(gray)

Output:

python opencv color spaces

Rotating Image

cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in three different ways.

Example: Python OpenCV Rotate Image

Python
import cv2
from google.colab.patches import cv2_imshow

src = cv2.imread("geeks14.png")

# Rotate the image by 90 degrees clockwise
image = cv2.rotate(src, cv2.ROTATE_90_CLOCKWISE)

cv2_imshow(image)

Output:

Python OpenCV Rotate Image

The above functions restrict us to rotate the image in the multiple of 90 degrees only. We can also rotate the image to any angle by defining the rotation matrix listing rotation point, degree of rotation and the scaling factor.

Example: Python OpenCV Rotate Image by any Angle

Python
import cv2
from google.colab.patches import cv2_imshow

img = cv2.imread("geeks14.png")

rows, cols = img.shape[:2]

# Create rotation matrix
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45, 1)

# Rotate the image
rotated = cv2.warpAffine(img, M, (cols, rows))

cv2_imshow(rotated)

Output:

Python OpenCV Rotate Image

Image Translation

Translation refers to the rectilinear shift of an object i.e. an image from one location to another. If we know the amount of shift in horizontal and the vertical direction, say (tx, ty) then we can make a transformation matrix. Now, we can use the cv2.wrapAffine() function to implement the translations. This function requires a 2×3 array. The numpy array should be of float type.

Example: Python OpenCV Image Translation

Python
import cv2
import numpy as np

image = cv2.imread('geeks.png')

height, width = image.shape[:2]

quarter_height, quarter_width = height / 4, width / 4

T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])

# We use warpAffine to transform
img_translation = cv2.warpAffine(image, T, (width, height))

from google.colab.patches import cv2_imshow

cv2_imshow(img_translation)

Output:

Python OpenCV Image Translation

Edge Detection

The process of image detection involves detecting sharp edges in the image. This edge detection is essential in the context of image recognition or object localization/detection. There are several algorithms for detecting edges due to its wide applicability. We’ll be using one such algorithm known as Canny Edge Detection. 

Example: Python OpenCV Canny Edge Detection

Python
import cv2
from google.colab.patches import cv2_imshow

img = cv2.imread("geeks14.png")

# Perform Canny edge detection
edges = cv2.Canny(img, 100, 200)

cv2_imshow(edges)

Output:

Python OpenCV Canny Edge Detection

You can download the complete code from here.

Comment

Explore