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()
No description has been provided for this image

We got a weird image colors... This is because OpenCV uses image reading convention of BGR and matplotlib uses RGB.

The fix is easy:

In [5]:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=figsize)
plt.imshow(img)
plt.title("Lenna RGB")
plt.show()
No description has been provided for this image
In [6]:
# some image info:
print(type(img))
print(img.shape)
img
<class 'numpy.ndarray'>
(512, 512, 3)
Out[6]:
array([[[226, 137, 125],
        [226, 137, 125],
        [223, 137, 133],
        ...,
        [230, 148, 122],
        [221, 130, 110],
        [200,  99,  90]],

       [[226, 137, 125],
        [226, 137, 125],
        [223, 137, 133],
        ...,
        [230, 148, 122],
        [221, 130, 110],
        [200,  99,  90]],

       [[226, 137, 125],
        [226, 137, 125],
        [223, 137, 133],
        ...,
        [230, 148, 122],
        [221, 130, 110],
        [200,  99,  90]],

       ...,

       [[ 84,  18,  60],
        [ 84,  18,  60],
        [ 92,  27,  58],
        ...,
        [173,  73,  84],
        [172,  68,  76],
        [177,  62,  79]],

       [[ 82,  22,  57],
        [ 82,  22,  57],
        [ 96,  32,  62],
        ...,
        [179,  70,  79],
        [181,  71,  81],
        [185,  74,  81]],

       [[ 82,  22,  57],
        [ 82,  22,  57],
        [ 96,  32,  62],
        ...,
        [179,  70,  79],
        [181,  71,  81],
        [185,  74,  81]]], dtype=uint8)
In [7]:
# show only red channel
plt.figure(figsize=figsize)
plt.imshow(img[:, :, 0])
plt.title("Lenna red channel")
plt.show()
No description has been provided for this image
In [8]:
# gray color-mapping
fig, ax_arr = plt.subplots(1, 2, figsize=figsize)

ax_arr[0].imshow(img[:, :, 0], cmap="gray")
ax_arr[0].set_title("Auto-adjusted pixel\n scale intensity")

ax_arr[1].imshow(
    img[:, :, 0], cmap="gray", vmin=0, vmax=255
)  # 255 is the max of uint8 type number (== 2**8 -1)
ax_arr[1].set_title("Absolute pixel\n scale intensity")
plt.show()
No description has been provided for this image

More advanced functions

In [9]:
# image blurring
img_blurred = cv2.GaussianBlur(
    img, (15, 15), 7
)  # use a 15x15 Gaussian kernel with standard deviation 7
plt.figure(figsize=figsize)
plt.imshow(img_blurred)
plt.title("Lenna blurred")
plt.show()
No description has been provided for this image
In [10]:
# edge detection
img_canny = cv2.Canny(img, 180, 200)  # end args are the lower & upper TH of hysteresis
plt.figure(figsize=figsize)
plt.imshow(img_canny, cmap="gray")
plt.title("Lenna edges")
plt.show()
No description has been provided for this image
In [11]:
# detect circles

img = cv2.imread("opencv_logo.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

circles = cv2.HoughCircles(img_gray, cv2.HOUGH_GRADIENT, 0.1, 50, param1=50, param2=38)

for x, y, r in circles[0, :]:
    # draw the outer circle
    cv2.circle(img, (int(x), int(y)), int(r), (0, 0, 0), 2)
    # draw the center of the circle
    cv2.circle(img, (int(x), int(y)), 2, (0, 0, 255), 3)

plt.figure(figsize=figsize)
plt.imshow(img)
plt.title("finding circles")
plt.show()
No description has been provided for this image
In [ ]: