Python Workshop: OpenCV

Open In Colab

Introduction

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. [https://opencv.org/about/]

opencv logo

Basic usage

In [1]:
import cv2  # opencv for python package
import matplotlib.pyplot as plt

figsize = (10, 10)
In [2]:
# to run in google colab
import sys

if "google.colab" in sys.modules:

    def download_from_web(url):
        import requests

        response = requests.get(url)
        if response.status_code == 200:
            with open(url.split("/")[-1], "wb") as file:
                file.write(response.content)
        else:
            raise Exception(f"Failed to download the image. Status code: {response.status_code}")

    download_from_web("https://github.com/YoniChechik/AI_is_Math/raw/master/c_01_intro_to_CV_and_Python/Lenna.png")
    download_from_web(
        "https://github.com/YoniChechik/AI_is_Math/raw/master/c_01_intro_to_CV_and_Python/opencv_logo.png"
    )

This is how to read and plot an image with opencv

In [3]:
img = cv2.imread("Lenna.png")
In [4]:
plt.figure(figsize=figsize)
plt.imshow(img)
plt.title("Lenna orig")
plt.show()