How to use opencv

OpenCV is a computer vision library that is often used for image analysis and machine learning. It is a very easy to use library that provides not only basic image transformation, but also image filtering, face recognition, object recognition, object tracking, and other functions often used in practice. Whenever I work with image recognition in practice, I use this library.

Let’s start with the basic usage.

github

  • The file in jupyter notebook format is here

google colaboratory

  • To run it on google colaboratory here base_nb.ipynb)

environment

The author’s OS is macOS, and the options are different from those of Linux and Unix.

My environment

sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
Python -V
Python 3.5.5 :: Anaconda, Inc.
import cv2

print('opencv version :', cv2.__version__)
opencv version : 3.4.1

We also import matplotlib for image display. We will save the images as svg for better web appearance.

%matplotlib inline
%config InlineBackend.figure_format = 'svg'

import matplotlib.pyplot as plt

Suppose you have a file called lena.jpg in the upper level.

%%bash

ls -al . / | grep jpg
-rw-r--r--@ 1 hiroshi staff 8211 11 14 22:01 lena.jpg
filename = '. /lena.jpg'.

Load an image

Let’s load an image and display it, using matplotlib to display it in jupyter notebook.

img = cv2.imread(filename=filename)

# In OpenCV, the image is loaded in GBR preparation, but in JupyterNotebook, it is displayed in RGB.
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.imshow(rgb_img)
plt.show()

Get image information.

Check the height, width, and number of colors (usually 3 in RGB) of the image if it is in color.

def get_image_info(img):
  if len(img.shape) == 3:
    img_height, img_width, img_channels = img.shape[:3].
    print('img_channels :', img_channels)
  else:
    img_height, img_width = img.shape[:2].

  print('img_height :', img_height)
  print('img_width :', img_width)

get_image_info(img=img)
img_channels : 3
img_height : 225
img_width : 225

Save the image

Use the imwrite method.

out_filename = '. /lena_out.jpg'
cv2.imwrite(out_filename, img)
True

Grayscale it

Use cv2.COLOR_BGR2GRAY.

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

get_image_info(img=gray_img)

plt.imshow(gray_img)
plt.gray()
plt.show()

out_filename = '. /gray_out.jpg'
cv2.imwrite(out_filename, gray_img)
img_height : 225
img_width : 225
True

Invert black and white

Flips a grayscale image to black and white.

bitwise_gray_img = cv2.bitwise_not(gray_img)

get_image_info(img=bitwise_gray_img)

plt.imshow(bitwise_gray_img)
plt.gray()
plt.show()

out_filename = '. /bitwise_out.jpg'
cv2.imwrite(out_filename, bitwise_gray_img)
img_height : 225
img_width : 225
True

Binaryize

Convert a grayscale image to a binary image.

threshold = 120
ret, binary_img = cv2.threshold(gray_img, threshold, 255, cv2.THRESH_BINARY)

get_image_info(img=binary_img)

plt.imshow(binary_img)
plt.gray()
plt.show()

out_filename = '. /binary_out.jpg'
cv2.imwrite(out_filename, binary_img)
img_height : 225
img_width : 225
True