Jovian
⭐️
Sign In
In [1]:
import imutils
import cv2
 
# load the input image and show its dimensions, keeping in mind that
# images are represented as a multi-dimensional NumPy array with
# shape no. rows (height) x no. columns (width) x no. channels (depth)
image = cv2.imread("jp.png")
(h, w, d) = image.shape
print("width={}, height={}, depth={}".format(w, h, d))
 
# display the image to our screen -- we will need to click the window
# open by OpenCV and press a key on our keyboard to continue execution
cv2.imshow("Image", image)
cv2.waitKey(0)
width=600, height=322, depth=3
Out[1]:
-1
In [5]:
import numpy as np
d = np.arange(1, 10, 2)
print('Numpy array using arange() :\n{}'.format(d))
Numpy array using arange() : [1 3 5 7 9]
In [35]:
a = np.linspace(1,10,5)
print('line space : \n{}'.format(a))
line space : [ 1. 3.25 5.5 7.75 10. ]
In [36]:
s = np.random.randint(10 , 30 , 8) # 8 values between 10 and 30 , 30 excluded
print('random integer : \n{}'.format(s))
random integer : [29 16 13 18 23 14 25 27]
In [18]:
print(np.__version__)
1.17.4
In [31]:

g = np.random.random((2,3)) # 2 rows and 3 columns
print('Numpy array of specified size with random values : \n{}\n'.format(g))

Numpy array of specified size with random values : [[0.39093449 0.92456628 0.27908643] [0.20328086 0.45718799 0.75782331]]
In [43]:
print(np.random.random(4)) ; print('\n',np.random.randint(4))
[0.38410312 0.88601496 0.57599936 0.84247407] 3
In [44]:

arr1 = np.array([24, 10])
print("Data type of the numpy array : ",arr1, "\n", arr1.dtype)

Data type of the numpy array : [24 10] int32
In [45]:

array_1 = np.array([11, 22, 33, 44])
print("NumPy array: ",array_1, "\n", 'Data type : ', array_1.dtype, '\n')

NumPy array: [11 22 33 44] Data type : int32
In [47]:

array_2 = array_1.astype('float')
print("Array of data type integer converted to string : \n{} \n" .format(array_2))

Array of data type integer converted to string : [11. 22. 33. 44.]
In [50]:

array_1 = np.array([21, 11, 22])
print("Single dimension numpy array :\n{}\n".format(array_1))
print("Dimension: {} ".format(array_1.ndim))
print("Shape:{} ".format(array_1.shape))
print("Size: {} \n".format(array_1.size))
Single dimension numpy array : [21 11 22] Dimension: 1 Shape:(3,) Size: 3
In [ ]:
np.lookfor('str')
In [57]:

array_2 = np.array([[21, 11], [22, 12],[23, 13]])
print("Multi dimension numpy array :\n{}  \n".format(array_2))
print("Dimension: {} ".format(array_2.ndim))
print("Shape:{} ".format(array_2.shape))
print("Size: {} \n".format(array_2.size))

Multi dimension numpy array : [[21 11] [22 12] [23 13]] Dimension: 2 Shape:(3, 2) Size: 6
In [59]:

array_2 = np.array([[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]])
print("Numpy array before slicing :\n{}  \n".format(array_2))
# Creates a subarray by slicing the first 2 rows and columns 1 and 2
print("Numpy array after slicing :\n{}  \n".format(array_2[:2, 1:3]))
Numpy array before slicing : [[24 25 26 27] [28 29 30 31] [32 33 34 35]] Numpy array after slicing : [[25 26] [29 30]]
In [73]:

    array_2 = np.array([[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]])
    print("Numpy array before integer indexing :\n{}  \n".format(array_2))
    # Row indices are (0, 0) and (1, 1)
    rows_array = np.array([[0, 0], [1, 1]])
    columns_array = np.array([[1, 0], [0, 1]])
    indexed_array2 = array_2[rows_array, columns_array]

    print("Indexed array :\n{}\n".format(indexed_array2))
Numpy array before integer indexing : [[24 25 26] [27 28 29] [30 31 32] [33 34 35]] Indexed array : [[25 24] [27 28]]
In [77]:

import numpy as np

array_1 = np.array([[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]])
print("Numpy array before boolean indexing print:\n{}  \n".format(array_1))
print("Numpy array with elements greater than 30 :\n{}  \n".format(array_1[array_1 > 30]))
Numpy array before boolean indexing print: [[24 25 26] [27 28 29] [30 31 32] [33 34 35]] Numpy array with elements greater than 30 : [31 32 33 34 35]
In [80]:
aa = np.array([45,67,5,4,3,2,2,1,3,4,5,6])
b = aa.reshape(6,2)
print(b)
[[45 67] [ 5 4] [ 3 2] [ 2 1] [ 3 4] [ 5 6]]
In [82]:

array_1 = np.array([[24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35]])
print("array_1 :\n{} \n".format(array_1))

# Flatten array_1 to a 1d array
print("array1 flattened to 1D :\n{} \n".format(array_1.flatten()))
# Changing the flattened array does not change parent
test1 = array_1.flatten()
test1[0] = 1 # changing b1 does not affect array_1

print(test1)


array_1 : [[24 25 26 27] [28 29 30 31] [32 33 34 35]] array1 flattened to 1D : [24 25 26 27 28 29 30 31 32 33 34 35] [ 1 25 26 27 28 29 30 31 32 33 34 35]
In [93]:
s = np.arange(9).reshape(3,3)
print(s)
print(s.ndim)
[[0 1 2] [3 4 5] [6 7 8]] 2
In [95]:
s = np.arange(18).reshape(2,3,3)
print(s)
print(s.ndim)
[[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]]] 3
In [97]:

import numpy as np

array_1 = np.arange(9)
print("Numpy array \n", array_1)

print("Applying average function on the array \n{}\n".format(np.average(array_1)))
print("Applying mean function on the array \n{}\n".format(np.mean(array_1)))
print("Applying median function on the array \n{}\n".format(np.median(array_1)))
print("Applying minimum function on the array \n{}\n".format(np.amin(array_1)))
print("Applying maximum function on the array \n{}\n".format(np.amax(array_1)))
print("Applying standard deviation function on the array\n{}\n".format(np.std(array_1)))
print("Applying variance function on the array \n{}\n".format(np.var(array_1)))
Numpy array [0 1 2 3 4 5 6 7 8] Applying average function on the array 4.0 Applying mean function on the array 4.0 Applying median function on the array 4.0 Applying minimum function on the array 0 Applying maximum function on the array 8 Applying standard deviation function on the array 2.581988897471611 Applying variance function on the array 6.666666666666667
In [98]:
0+1+2+3+4+5+6+7+8
Out[98]:
36
In [99]:
36/9
Out[99]:
4.0
In [6]:

import numpy as np
import matplotlib.pyplot as plt

from skimage import data
from skimage.color import rgb2gray

# Reading an image into a numpy array
original_image = data.astronaut()
print("Datatype of the image \n", original_image.dtype)
print("Shape of the image \n", original_image.shape)

#Convering image to grayscale
grayscale_image = rgb2gray(original_image)
fig, axes = plt.subplots(1, 2)

axes[0].imshow(original_image)
axes[0].set_title("Original")
axes[1].imshow(grayscale_image, cmap=plt.cm.gray)
axes[1].set_title("Grayscale")

fig.tight_layout()
plt.show()

#Extracting red channel by array multiplication
# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (512,512, 3);
# we multiply it by the array [1, 0, 0] of to extract the red channel
img_red = original_image * [1, 0, 0]
plt.imshow(img_red)
plt.title("Red channel image")
plt.show()


from skimage.transform import resize
# Resize the greyscale image to 300 by 300 pixels.
img_resized = resize(grayscale_image, (300, 300),anti_aliasing=True)
plt.imshow(img_resized,cmap='gray')
plt.title("Resized image")
plt.show()
print("Shape of the resized image \n", img_resized.shape)
Datatype of the image uint8 Shape of the image (512, 512, 3)
Notebook Image
Notebook Image
Notebook Image
Shape of the resized image (300, 300)
In [ ]:
 
In [ ]: