Image Data Analysis
Image is a special kind of data format where data is stored in the form of matrices.
When we have image in the form numbers arranged in a matrix, we can do all matrix operations.
In [1]:
import numpy as np
from matplotlib import pyplot as plt
List Matrix
In [2]:
l = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 1]]
In [3]:
print(l)
[[1, 2, 3, 4, 5], [3, 4, 5, 6, 1]]
In [4]:
type(l)
Out[4]: list
Scalar Multiplication list Matrix
In [5]:
l * 3
Out[5]: [[1, 2, 3, 4, 5],
[3, 4, 5, 6, 1],
[1, 2, 3, 4, 5],
[3, 4, 5, 6, 1],
[1, 2, 3, 4, 5],
[3, 4, 5, 6, 1]]
In [6]:
lm = []
for i in l:
r = []
for j in i:
[Link](j * 3)
[Link](r)
In [7]:
lm
Out[7]: [[3, 6, 9, 12, 15], [9, 12, 15, 18, 3]]
NumPy Matrix
In [8]:
mat = [Link](
[[1, 2, 3, 4, 5],
[3, 4, 5, 6, 1]]
)
In [9]:
print(mat)
[[1 2 3 4 5]
[3 4 5 6 1]]
In [10]:
type(mat)
Out[10]: [Link]
Scalar Multiplication of numpy Matrix
In [11]:
print(mat * 3)
[[ 3 6 9 12 15]
[ 9 12 15 18 3]]
In [12]:
[Link]
Out[12]: (2, 5)
Transpose Operation
In [13]:
print(mat)
[[1 2 3 4 5]
[3 4 5 6 1]]
In [14]:
print(mat.T)
[[1 3]
[2 4]
[3 5]
[4 6]
[5 1]]
Can we convert a matrix into an image ?
imshow() of plt
In [15]:
print(mat)
[[1 2 3 4 5]
[3 4 5 6 1]]
In [16]:
[Link](figsize=(10, 3))
image_mat = [Link](mat, cmap='gist_rainbow')
[Link](image_mat)
[Link]("off")
[Link]()
Convert large matrix into image
There should be 30 rows and 50 columns
Each row of the matrix should have 50 numbers in the range of 1 and 200
In [17]:
big_mat = [Link](low=1, high=200, size=(30, 50))
In [18]:
big_mat
Out[18]: array([[ 12, 128, 181, ..., 132, 65, 192],
[155, 179, 183, ..., 106, 151, 169],
[ 36, 188, 177, ..., 166, 11, 83],
...,
[142, 197, 110, ..., 199, 187, 30],
[126, 35, 84, ..., 124, 149, 131],
[ 65, 20, 52, ..., 197, 55, 125]])
In [19]:
big_mat.shape
Out[19]: (30, 50)
Matrix to Image
In [20]:
[Link](figsize=(10, 4))
image_mat = [Link](big_mat, cmap='gist_rainbow')
[Link](image_mat)
[Link]("off")
[Link]()
In [21]:
sorted_mat = [Link](big_mat)
In [22]:
sorted_mat
Out[22]: array([[ 8, 8, 12, ..., 181, 183, 192],
[ 1, 3, 7, ..., 195, 196, 196],
[ 4, 7, 11, ..., 188, 189, 196],
...,
[ 2, 4, 5, ..., 188, 197, 199],
[ 3, 10, 11, ..., 175, 180, 199],
[ 4, 20, 21, ..., 198, 198, 199]])
In [23]:
[Link](figsize=(10, 4))
image_mat = [Link](sorted_mat, cmap='gist_rainbow')
[Link](image_mat)
[Link]("off")
[Link]()
Transposed Matrix to Image
In [24]:
[Link](figsize=(10, 4))
trans_mat = big_mat.T
timage_mat = [Link](trans_mat, cmap='gist_rainbow')
[Link](timage_mat)
[Link]()
In [25]:
[Link](figsize=(10, 4))
trans_mat = sorted_mat.T
timage_mat = [Link](trans_mat, cmap='gist_rainbow')
[Link](timage_mat)
[Link]()
grayscale Image
In [26]:
[Link](figsize=(10, 4))
gray_image = [Link](big_mat, cmap='gray_r')
[Link](gray_image)
[Link]()
Can we convert an image into a matrix ?
We should use cv2 ( opencv-python ) package in python to compute matrix operations on images.
py -m pip install opencv-python --user
A typical colored image is comprised of pixels (which are represented as RGB pixels).
A pixel is simply a number in the range of 0 to 255 for all R , G , and B .
R → Red → 0 to 255
G → Green → 0 to 255
B → Blue → 0 to 255
Image by Author
Some important colors and their RGB values -
Pixel R G B
White 255 255 255
Red 255 0 0
Green 0 255 0
Blue 0 0 255
Black 0 0 0
Yellow 255 255 0
All colors → [Link]
Let's read the image as matrix
The image that we will read is -
Image Link → lena_image.png
Read the image in the form of matrix
In [27]:
import cv2
BGR
In [28]:
image_mat = [Link]('lena_image.png')
The image matrix would be like -
[[[159 183 255] ... [142 202 255]]
[[140 169 255] ... [ 87 74 159]]
[[118 164 255] ... [ 62 14 81]]
...
[[ 64 30 96] ... [ 61 33 119]]
[[ 61 27 92] ... [ 74 65 178]]
[[ 60 25 89] ... [ 80 72 202]]]
In [29]:
[Link](image_mat)
[Link]()
By default, the image is read in BGR format.
We need to convert it into RGB format for our convenience.
BGR → to → RGB format
In [30]:
image_mat = [Link](image_mat, cv2.COLOR_BGR2RGB)
The image matrix would be like -
[[[255 183 159] ... [255 202 142]]
[[255 169 140] ... [159 74 87]]
[[255 164 118] ... [ 81 14 62]]
...
[[ 96 30 64] ... [119 33 61]]
[[ 92 27 61] ... [178 65 74]]
[[ 89 25 60] ... [202 72 80]]]
In [31]:
[Link](image_mat)
[Link]()
Shape of the image matrix - rows and columns
In [32]:
image_mat.shape
Out[32]: (128, 128, 3)
In [33]:
rows, cols, channels = image_mat.shape
print(rows)
print(cols)
print(channels)
128
128
3
Separate R , G , and B from the Image
Image by Author
We make use of [Link]() method to separate the RGB pixels from the image.
In [34]:
rimage_mat, gimage_mat, bimage_mat = [Link](image_mat)
In [35]:
print("R → \n\n", rimage_mat)
print(rimage_mat.shape)
R →
[[255 255 255 ... 212 255 255]
[255 255 247 ... 227 227 159]
[255 246 232 ... 190 103 81]
...
[ 96 100 100 ... 105 100 119]
[ 92 97 95 ... 105 124 178]
[ 89 96 91 ... 115 156 202]]
(128, 128)
In [36]:
print("G → \n\n", gimage_mat)
print(gimage_mat.shape)
G →
[[183 174 167 ... 99 203 202]
[169 156 149 ... 117 126 74]
[164 142 136 ... 82 25 14]
...
[ 30 33 34 ... 32 26 33]
[ 27 30 28 ... 33 43 65]
[ 25 29 24 ... 42 64 72]]
(128, 128)
In [37]:
print("B → \n\n", bimage_mat)
print(bimage_mat.shape)
B →
[[159 148 142 ... 101 163 142]
[140 130 124 ... 107 113 87]
[118 115 113 ... 83 64 62]
...
[ 64 66 71 ... 63 58 61]
[ 61 63 67 ... 62 68 74]
[ 60 62 64 ... 72 82 80]]
(128, 128)
Plot R , G , and B separately
In [38]:
l = [1, 2, 3, 4]
g = [5, 6, 7, 8]
f = list(zip(l, g)) # [(1, 5), (2, 6), (3, 7), (4, 8)]
print(f)
[(1, 5), (2, 6), (3, 7), (4, 8)]
In [39]:
cmap_values = [None, 'Reds', 'Greens', 'Blues']
titles = ['Original', 'Red Lenna', 'Green Lenna', 'Blue Lenna']
image_matrices = [image_mat, rimage_mat, gimage_mat, bimage_mat]
fig, axes = [Link](nrows=1, ncols=4, figsize=(15, 10))
for i, ax in zip(range(4), axes):
[Link]("off")
ax.set_title(titles[i])
[Link](image_matrices[i], cmap=cmap_values[i])
[Link]()
Some Matrix Operations
Let's take grayscale matrix of original image
In [40]:
gray_image = [Link]('lena_image.png', 0)
In [41]:
gray_image.shape
Out[41]: (128, 128)
In [42]:
gray_image
Out[42]: array([[202, 195, 191, ..., 133, 214, 211],
[192, 183, 175, ..., 149, 155, 101],
[186, 170, 162, ..., 115, 52, 39],
...,
[ 53, 57, 58, ..., 57, 51, 62],
[ 50, 54, 52, ..., 58, 70, 100],
[ 48, 53, 48, ..., 67, 93, 112]], dtype=uint8)
In [43]:
[Link](gray_image, cmap='gray')
[Link]()
Tranpose gray lenna
In [44]:
trans_lenna = gray_image.T
In [45]:
[Link](trans_lenna, cmap='gray')
[Link]()
In [46]:
trans_lenna.shape
Out[46]: (128, 128)
How can we transpose a colored image?
Since each pixel is a combination of 3 values, we have to -
Algorithm
separate R , G , and B matrices
apply transpose or (any) operation to all the 3 matrices
merge R , G , and B matrices as a one single matrix
In [47]:
# separation of R, G, and B
rimage_mat, gimage_mat, bimage_mat = [Link](image_mat)
# transpose operation to all the 3 matrices
trans_r = rimage_mat.T
trans_g = gimage_mat.T
trans_b = bimage_mat.T
# merging R, G, and B matrices into one single matrix
trans_color_lenna = [Link]((trans_r, trans_g, trans_b))
# plotting the transposed colored image
[Link](trans_color_lenna)
[Link]()
Flip Operation
In [48]:
image_mat = [Link]('lena_image.png')
image_mat = [Link](image_mat, cv2.COLOR_BGR2RGB)
In [49]:
# split
r, g, b = [Link](image_mat)
In [50]:
# each flipud
fr = [Link](r)
fg = [Link](g)
fb = [Link](b)
In [51]:
# merge
fimage = [Link]((fr, fg, fb))
In [52]:
# original show
[Link](image_mat)
[Link]()
# flip show
[Link](fimage)
[Link]()
Other Operations
Image Flipping
Image Mirroring
Image Equalization (One of the mind blowing operations)
Used for enhancing the contrast of an image
Image Binarization
Image Inversion
Image Cropping
Image Bordering
Image Convolution with kernels (One of the mind blowing operations)
Used for Smoothening, Blurring, Edge detection etc
PS: You can find all of them in my blog website.
GitHub → [Link]
Hashnode → [Link]
Medium → [Link]
Image Processing Application → [Link]
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/[Link]