Image Maths

[Tutorial under development.........]

Image is an array of pixels

H x W x C

H = Height of Image

W = Width of Image

C = Color channels in image

Ecah pixel has a color value (in grayscale image), 3 color values (RGB images)

Pixel values can be between 0 to 255

Image that we will use here is given below

To read image in opencv

import cv2

img_path = "/home/Pictures/opensource_image.png"

img = cv2.imread(img_path)
cv2.imshow("test", img)
print(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

If you print img variable the output will be like

[[[204 204 204]
  [200 200 200]
  [194 194 194]
  ...
  [226 208 200]
  [226 208 200]
  [226 208 200]]

 [[127 127 127]
  [170 170 170]
  [194 194 194]
  ...
  [226 208 200]
  [226 208 200]
  [226 208 200]]]

As you can see images are just bunch of numbers , multidimensional matrix if you want to say so

Reading some image as Gray scale

There are two ways to do it

1. 

import cv2

img_path = "/home/chick/Pictures/opensource_image.png"

img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("test", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 

import cv2

img_path = "/home/chick/Pictures/opensource_image.png"

gray = cv2.imread(img_path, 0)
cv2.imshow("test", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Resize an image

import cv2

img_path = "/home/chick/Pictures/opensource_image.png"

gray = cv2.imread(img_path, 0)
resized = cv2.resize(gray,(50,50))
cv2.imshow("test", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Always remember OpenCV read colored image in BGR (Blue-Green-Red) format while other image processing libraries that you may be using understand images in RGB (Red-Green-Blue) format

How to crop some image

import cv2

img_path = "/home/chick/Pictures/opensource_image.png"

img = cv2.imread(img_path)
print(img.shape)
crop = img[0:100, 0:200, :]
cv2.imshow("test", crop)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output image will be like this