CompileArtisan

OpenCV

Table of Contents

Greatlearning

import numpy as np
import cv2

1. Writing an Image

  • cv2.imwrite() is used to write a numpy array into an image.
  • One thing to note is that OpenCV uses BGR color format.
  • For example, here’s how you make a pixel of a \(3 \times 3\) pixel red:

    img = np.ones((3, 3, 3), dtype="uint8")
    print(img)
    img[1, 2] = (0, 0, 255) # (B, G, R)
    cv2.imwrite('color_img.png', img) 
    
      [[[1 1 1]
        [1 1 1]
        [1 1 1]]
    
       [[1 1 1]
        [1 1 1]
        [1 1 1]]
    
       [[1 1 1]
        [1 1 1]
        [1 1 1]]]
    

2. Reading an Image

  • cv2.imread() takes in the name of a file as an argument (and an optional flag), and returns the equivalent NumPy array.
  • For example:
image_read = cv2.imread('color_img.png')
print(image_read)
[[[  1   1   1]
  [  1   1   1]
  [  1   1   1]]

 [[  1   1   1]
  [  1   1   1]
  [  0   0 255]]

 [[  1   1   1]
  [  1   1   1]
  [  1   1   1]]]

2.1. Flags

OpenCV provides 3 flags which you can use (1, 0 or -1).

Flag Value What it does
cv2.imread('color_img.png', cv2.IMREAD_COLOR) 1 returns 3D NumPy array (doesn’t contain transparency). This is also the default value.
cv2.imread('color_img.png', cv2.IMREAD_GRAYSCALE) 0 returns 2D NumPy array (doesn’t contain transparency or color either).
cv2.imread('color_img.png', cv2.IMREAD_UNCHANGED) -1 returns 3D or 4D NumPy array (contains transparency if original image had it)

Here’s a quick code to convert an image into black and white.

cv2.imwrite(
    'black_and_white.png',
    cv2.imread('color_img.png', cv2.IMREAD_GRAYSCALE)
)

The function name is cv2.imread() and IMREAD_COLOR , IMREAD_GRAYSCALE and IMREAD_UNCHANGED are global variables declared in the cv2 package. By convention, variables written in all caps mean they’re constants. One more thing to note is that the variable names are very well descriptive (functionName_value).

3. Displaying an Image - cv2.imgshow()

cv2.imshow() creates a window and displays the array as an image.

cv2.imshow('Window Name Lol', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.waitKey(0) waits for keyboard input and returns the ASCII value of the key pressed.

  • 0 = wait forever
  • 1000 = wait 1000ms (1 second)

4. Resizing an Image

4.1. By Specifying a new dimension

img = cv2.imread("color_img.png")
resized = cv2.resize(img, (1000, 500))
cv2.imwrite("resized.png", resized)

resized.png

4.2. By Scaling

scaled = cv2.resize(img, (0, 0), fx=100, fy=100)
cv2.imwrite("scaled.png", scaled)

scaled.png

5. Converting Image Color

  • You can convert image color from one color space to another, using the cv2.cvtColor() function, which also returns a NumPy array just like cv2.imread().
  • The function takes two parameters: the numpy array you want to convert, and the cv2 constant that corresponds to the target color space.
  • For instance, the cv2.COLOR_BGR2GRAY, constant is simply the integer 6.
print(cv2.COLOR_BGR2GRAY)

The usage of all of these will be explored next.

5.1. cv2.COLOR_BGR2GRAY

img_mono = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
cv2.imwrite("resized_mono.png", img_mono)

resized_mono.png

Do note that the dimensions reduce:

print(resized.shape)
print(img_mono.shape)

(500, 1000, 3)
(500, 1000)

img_mono.shape returns (500, 1000) only, demonstrating the lack of channels. If you do want channels, you’d have to read the saved image with color flag.

with_channels = cv2.imread('resized_mono.png', cv2.IMREAD_COLOR)
print(with_channels.shape)

(500, 1000, 3)

This is still a monochrome image, but it has 3 channels.

5.2. cv2.BGR2GRGB

rgb_img = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
cv2.imwrite("resized_rgb.png", rgb_img)

resized_rgb.png

5.3. cv2.RGB2BGR

The reverse also exists.

bgr_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR)
cv2.imwrite("resized_bgr.png", bgr_img)

resized_bgr.png

6. TODO Thresholding

  • Thresholding is where you convert a grayscale image into a purely black and white image.
  • This means that every pixel will either be black, or be white, and not some shade of gray.
  • If the pixel brightness is above a threshold value, you turn it into white, else you turn it into black.

    source = cv2.imread('resized.png', cv2.IMREAD_GRAYSCALE)
    retvalue, binary = cv2.threshold(source, 50, 255, cv2.THRESH_BINARY)
    cv2.imwrite("binary.png", binary)
    
    
    

7. TODO Drawing

Rectangle: cv2.rectangle()

8. TODO Object Detection

cv2.CascadeClassifier()

draw rectangle around faces