Skip to content

OpenCV basics with easy commands and Python scripts in our simple repository.

License

Notifications You must be signed in to change notification settings

kiarashrahmani/OpenCV-Essentials

Repository files navigation

OpenCV Essentials

Import Libraries

pip install opencv-python
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt

from zipfile import ZipFile
from urllib.request import urlretrieve

from IPython.display import Image

%matplotlib inline
Image(filename="images/myimage1.jpg")

Reading images using OpenCV

Function Syntax

retval = cv2.imread( filename[, flags] )

Flags

  1. cv2.IMREAD_GRAYSCALE or 0: Loads image in grayscale mode
  2. cv2.IMREAD_COLOR or 1: Loads a color image. Any transparency of image will be neglected. It is the default flag.
  3. cv2.IMREAD_UNCHANGED or -1: Loads image as such including alpha channel.

OpenCV Documentation

  1. Imread: Documentation link

  2. ImreadModes: Documentation link

# Read image as gray scale.
cb_img = cv2.imread("images/myimage1.jpg", 0)
# Print the image data
print(cb_img)
[[187 203 237 ... 227 233 215]
 [158 167 200 ... 217 226 197]
 [144 134 135 ... 213 224 191]
 ...
 [ 54  53  54 ...  40  40  41]
 [ 55  52  53 ...  39  39  40]
 [ 56  53  53 ...  38  39  40]]
# print the size  of image
print("Image size (H, W) is:", cb_img.shape)

# print data-type of image
print("Data type of image is:", cb_img.dtype)
Image size (H, W) is: (1525, 1526)
Data type of image is: uint8

Display Images using Matplotlib

plt.imshow(cb_img, cmap="gray")
<matplotlib.image.AxesImage at 0x2e781bd7ee0>

plt.imshow(cb_img)
<matplotlib.image.AxesImage at 0x2e7829adfa0>

# print the size  of image
print("matrix size (H, W) is:", cb_img.shape)
matrix size (H, W) is: (1525, 1526)

NP functions that are usefull for calculating with elements in matrix

np.sum:Computes the sum of array elements over a specified axis.

np.mean: Computes the arithmetic mean along the specified axis.
np.max: Computes the maximum of array elements along a specified axis.

np.min: Computes the minimum of array elements along a specified axis.
np.prod: Computes the product of array elements over a specified axis.
np.std: Computes the standard deviation along the specified axis.
np.var: Computes the variance along the specified axis.
np.argmax: Returns the indices of the maximum values along an axis.
np.argmin: Returns the indices of the minimum values along an axis.
np.median: Computes the median along the specified axis.
np.percentile: Computes the q-th percentile of the data along the specified axis.
np.cumsum: Computes the cumulative sum of array elements along a specified axis.
np.cumprod: Computes the cumulative product of array elements along a specified axis.
np.linalg.norm: Computes the vector or matrix norm.

Calculating Avreage

average = np.mean(cb_img)

print("Average of matrix elements:", average)
Average of matrix elements: 91.7139703070279

Calculating Variance

variance = np.var(cb_img)

print("Variance of matrix elements:", variance)
Variance of matrix elements: 4259.920145633649

Saving Images

# save the image
cv2.imwrite("images/myimage1_SAVED.png", cb_img )

Image(filename='images/myimage1_SAVED.png')

Thanks for your attention Kiarash Rahmani

About

OpenCV basics with easy commands and Python scripts in our simple repository.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published