back to writing

OpenCV Crash Course

4 min read
TL;DR

You're cooked if you need a tldr for a *crash course*

for starters, import opencv (duh)

import cv2

I'll be omitting this line moving forward.

Extracting the size of the image

img = cv2.imread('test.png')
 
print(img.shape)

Here img.shape is a tuple containing 3 values (assuming its an RGB image), for a grayscale image, it'll only have 2 values. These are the height, width and channels.

You can multiply these to get the size of an uncompressed image.

height, width, channels = img.shape
height*width*channels
Output

4800000

48000004800000 comes to be around 4.8 MB, while if we check the file size on disk

  du -h test.jpg
Output

532K test.jpg

532KB vs 4.8MB, JPEG Compression is goated.

Converting between color modes

tf are color modes?

Color Modes could be RGB, Grayscale, HSV

You might be familiar with RGB (why are u here if u dont?)

To convert a RGB image to a grayscale image.

img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', gray)

Referring to the documentation of the cvtColor function

(function) def cvtColor(
    src: MatLike,
    code: int,
    dst: MatLike | None = ...,
    dstCn: int = ...,
    hint: AlgorithmHint = ...
) -> MatLike

The parameters we're concerned with here are the src and code, the first being a matrix (MatLike) of the source image, which we get by the imread function, the latter and the arguably more important parameter being code which takes in a constant code which specifies which format to convert the image to. Some valid codes are:

  • BGR2HSV
  • BGR2GRAY
  • BGR2GRAY (the one we're using in this example)

We finally end up with another matrix (MatLike) that we can operate on with OpenCV

Displaying Images

Images can be displayed using either the OpenCV module or the Matplotlib module.

I prefer the latter as it works with my jup2typ script.

Both modules export an imshow function, so you can use them as:

import matplotlib.pyplot as plt
 
cv2.imshow(gray)
plt.imshow(gray, cmap='gray')

Matplotlib requires an additional cmap argument that specifies the color map of the image, otherwise it'll try rendering the grayscale image in RGB, resulting in a weird yellowish-purple image.

If you're interested, here's the documentation for the cmap argument:

(class) Colormap
───────────────────────────────────────────────────────────────────────────
Baseclass for all scalar to RGBA mappings.
 
Typically, Colormap instances are used to convert data values (floats)
from the interval `[0, 1]` to the RGBA color that the respective
Colormap represents. For scaling of data into the `[0, 1]` interval see
`matplotlib.colors.Normalize`. Subclasses of `matplotlib.cm.ScalarMappable`
make heavy use of this `data -> normalize -> map-to-color` processing
chain.

Useful imports

import cv2
import matplotlib.pyplot as plt
from skimage import calcHist

Some binary magic

Specifying 0 as the base or the 2nd argument of the int function infers the base from the string representation of the number

  • 0o \rightarrow Octal
  • 0x \rightarrow Hexadecimal
  • 0b \rightarrow Binary
int('0b11', 0)
int('0x32', base = 0)
int('0o32', 0)
Output

3 50 26

Column1Column2Column3
Item1Item1Item1
Item1Item1Item1
Item1Item1Item1
Item1Item1Item1
Did you know?

This is an info block with a custom title.

warning

This is a warning without a title.

Critical Error

Don't delete the production database.

2. Quotes Use standard markdown quotes:

"Simplicity is the ultimate sophistication." — Leonardo da Vinci