Basics of Computer Vision & OpenCV

Posted by Marco Pineda on June 24, 2017

This post covers the basics of Computer Vision and Open CV.

Topics Covered:

  • What are Images?
  • How Images Are Formed.
  • Storing Images on Computers
  • Getting Started with OpenCV: reading, writing, displaying images
  • Grayscaling
  • Color Spaces
  • Histograms
  • Drawing Images

Getting Started:


What Are Images?

  • 2-Dimensional representations of the visible light spectrum.

Examples:

  • photographs
  • picture on laptop screen

Vocabulary:

  • DPI - Dots Per Image
  • Visible Light Spectrum - wavelengths of light human eyes can detect

How are Images Formed??

  • Images are formed when light hits an object, relects off the object onto a film, sensor or retina.

To optimize images, we use a small opening called an aperature to block off excess light and focus the image; reducing blur. We can also use lenses to control the image formation.

Lenses

Both our eyes and cameras use an adaptive lens to control many aspects of the image formation such as:

Aperature Size

  • Controls the amount of light allowed through (f-stops in cameras)
  • Depth of field(Bokeh)

Length Width

  • Adjust focus distance (near or far)

How Humans See

Both our eyes and cameras use an adaptive lens to control many aspects of the image formation such as:

How Computers See - OpenCV

OpenCV uses RGB (Red, Green, Blue) color space by default.

Each pixel(x,y) in an image has 3 values ranging in 0 - 255(8-bit)

Those values are:

  • Red
  • Green
  • Blue

Mixing different color intensities of each color gives us the full color spectrum, example Yellow:

  • Red - 255
  • Green - 255
  • Blue - 0

Length Width

  • Adjust focus distance (near or far)

How A Computer Stores Images

Images are stored in multi-dimensional arrays

Think of an array as a table.

1 2 3 4

Black, White or Grayscale

Black and white image, images are saved in a 2-Dimensional array.

Two types of B&W images

  • Grayscale - ranges in shades of gray
  • Binary - pixels are either black or white

Getting Started With OpenCV

What is OpenCV?

  • OpenCV = Open Computer Vision
  • Launched in 1999 by Intel
  • Written in C++
  • 1st major release in 2006, second in 2009 and third in 2015
  • Latest stable version is OpeCV 2.4.13

Get Started - Reading, Writing & Displaying Images:

See tutorial on reading, writing and displaying images using OpenCV


Grayscaling

Why Grayscaling?

In OpenCV, many functions grayscale images before processing. This is done to simplify the image to reduce noise and increase processing time as there is less information in the image.

What is Grayscaling?

Converting an image from full color to shades of gray (black & white).

Getting Started: Grayscaling Tutorial

See tutorial on Grayscaling using OpenCV

In the tutorial you will learn:

  • How to convert color image to grayscale
#import packages
import cv2
import numpy as np

#read image using cv2.imread()
input = cv2.imread('./images/input.jpg', 0)

#output to grayscale using cv2.imshow()
cv2.imshow('Grayscale', input)
cv2.waitKey(0)
cv2.destroyAllWindows()

Color Spaces

What are Color Spaces?

Color spaces are a way to represent color.

The most common terms to represent color:

  • RGB - Red, Gree, Blue
  • HSV - Hue, Saturation, Value
  • CMYK - Cyan, Magenta, Yellow, Black(K)

Getting Started: Color Spaces Tutorial

See tutorial on Color Spaces using OpenCV


Histograms

Why Histograms?

Great way to visualize individual color components.

Getting Started: Histogram Tutorial

See tutorial on Histograms using OpenCV


Drawing Images

Getting Started: Drawing Tutorial

See tutorial on Drawing Images using OpenCV